5 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
17 HOME MYSQL_HOST MYSQL_TCP_PORT MYSQL_USER MYSQL_DATABASE MYSQL_PW
18 MIGSCHEMA MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
25 "dbi:mysql:host=$MYSQL_HOST;dbname=$MYSQL_DATABASE;port=$MYSQL_TCP_PORT"
28 ) || die "Unable to connect to $MYSQL_HOST:$MYSQL_TCP_PORT:$MYSQL_DATABASE:$MYSQL_USER : $!\n";
30 $dbh = DBI->connect("dbi:Pg:dbname=$MYSQL_DATABASE", "", "") || die "Unable to connect to $MYSQL_DATABASE : $!\n";
48 sub die_if_no_env_migschema {
49 die "MIGSCHEMA environment variable not set. See 'mig env help'\n"
53 sub check_for_db_migschema {
54 return 1; # the schema is the same as the database name, which is the same as the koha instance name, in most cases
57 sub check_db_migschema_for_migration_tables {
58 my $found = check_db_migschema_for_specific_table('m_borrowers');
60 print "Missing migration tables (such as m_borrowers)\n";
65 sub check_db_migschema_for_specific_table {
67 my $dbh = db_connect();
68 my $sth = $dbh->prepare("
71 FROM information_schema.tables
72 WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
73 AND table_name = " . $dbh->quote( $table ) . "
76 my $rv = $sth->execute()
77 || die "Error checking migration schema ($MIGSCHEMA) for table ($table): $!";
78 my @cols = $sth->fetchrow_array;
90 sub check_for_migration_tools {
91 my $dbh = db_connect();
92 my $sth = $dbh->prepare("
95 FROM information_schema.tables
96 WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
97 AND table_name = " . $dbh->quote( 'mt_init' ) . "
100 my $rv = $sth->execute()
101 || die "Error checking for migration_tools: $!";
102 my @cols = $sth->fetchrow_array;
108 sub die_if_no_migration_tools {
109 if (check_for_migration_tools()) {
110 print "Found migration_tools\n";
112 die "Missing migration_tools\n";
116 sub check_for_mig_tracking_table {
117 my $dbh = db_connect();
118 my $sth = $dbh->prepare("
121 FROM information_schema.tables
122 WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
123 AND table_name = 'm_tracked_file'
126 my $rv = $sth->execute()
127 || die "Error checking for table (m_tracked_file): $!";
128 my @cols = $sth->fetchrow_array;
134 sub die_if_mig_tracking_table_exists {
135 if (check_for_mig_tracking_table()) {
136 die "Table m_tracked_file already exists. Bailing init...\n";
140 sub die_if_mig_tracking_table_does_not_exist {
141 if (!check_for_mig_tracking_table()) {
142 die "Table m_tracked_file does not exist. Bailing...\n";
146 sub check_for_mig_column_tracking_table {
147 my $dbh = db_connect();
148 my $sth = $dbh->prepare("
151 FROM information_schema.tables
152 WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
153 AND table_name = 'm_tracked_column'
156 my $rv = $sth->execute()
157 || die "Error checking for table (m_tracked_column): $!";
158 my @cols = $sth->fetchrow_array;
164 sub die_if_mig_column_tracking_table_exists {
165 if (check_for_mig_column_tracking_table()) {
166 die "Table m_tracked_column already exists. Bailing init...\n";
170 sub die_if_mig_column_tracking_table_does_not_exist {
171 if (!check_for_mig_column_tracking_table()) {
172 die "Table m_tracked_column does not exist. Bailing...\n";
176 sub check_for_tracked_file {
180 die "file not found: $file\n" unless $options && $options->{'allow_missing'};
182 my $dbh = db_connect();
183 my $sth = $dbh->prepare("
186 WHERE base_filename = " . $dbh->quote( $file ) . ";"
188 my $rv = $sth->execute()
189 || die "Error checking table (m_tracked_file) for base_filename ($file): $!";
190 my @cols = $sth->fetchrow_array;
196 sub check_for_tracked_column {
197 my ($table,$column,$options) = (shift,shift,shift);
198 my $dbh = db_connect();
199 my $sth = $dbh->prepare("
201 FROM m_tracked_column
202 WHERE staged_table = " . $dbh->quote( $table ) . "
203 AND staged_column = " . $dbh->quote( $column ) . ";"
205 my $rv = $sth->execute()
206 || die "Error checking table (m_tracked_column) for $table.$column: $!";
207 my @cols = $sth->fetchrow_array;
213 sub status_this_file {
215 my $dbh = db_connect();
216 my $sth = $dbh->prepare("
219 WHERE base_filename = " . $dbh->quote( $file ) . ";"
221 my $rv = $sth->execute()
222 || die "Error retrieving data from table (m_tracked_file) for base_filename ($file): $!";
223 my $data = $sth->fetchrow_hashref;
229 sub status_this_column {
230 my ($table,$column) = (shift,shift);
231 my $dbh = db_connect();
232 my $sth = $dbh->prepare("
234 FROM m_tracked_column
235 WHERE staged_table = " . $dbh->quote( $table ) . "
236 AND staged_column = " . $dbh->quote( $column ) . ";"
238 my $rv = $sth->execute()
239 || die "Error checking table (m_tracked_column) for $table.$column: $!";
240 my $data = $sth->fetchrow_hashref;