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 # the schema is the same as the database name, which is the same
55 # as the koha instance name prefixed with 'koha_', in most cases
59 sub check_db_migschema_for_migration_tables {
60 my $found = check_db_migschema_for_specific_table('m_borrowers');
62 print "Missing migration tables (such as m_borrowers)\n";
67 sub check_db_migschema_for_specific_table {
69 my $dbh = db_connect();
70 my $sth = $dbh->prepare("
73 FROM information_schema.tables
74 WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
75 AND table_name = " . $dbh->quote( $table ) . "
78 my $rv = $sth->execute()
79 || die "Error checking migration schema ($MIGSCHEMA) for table ($table): $!";
80 my @cols = $sth->fetchrow_array;
92 sub check_for_migration_tools {
93 my $dbh = db_connect();
94 my $sth = $dbh->prepare("
97 FROM information_schema.tables
98 WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
99 AND table_name = " . $dbh->quote( 'mt_init' ) . "
102 my $rv = $sth->execute()
103 || die "Error checking for migration_tools: $!";
104 my @cols = $sth->fetchrow_array;
110 sub die_if_no_migration_tools {
111 if (check_for_migration_tools()) {
112 print "Found migration_tools\n";
114 die "Missing migration_tools\n";
118 sub check_for_mig_tracking_table {
119 my $dbh = db_connect();
120 my $sth = $dbh->prepare("
123 FROM information_schema.tables
124 WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
125 AND table_name = 'm_tracked_file'
128 my $rv = $sth->execute()
129 || die "Error checking for table (m_tracked_file): $!";
130 my @cols = $sth->fetchrow_array;
136 sub die_if_mig_tracking_table_exists {
137 if (check_for_mig_tracking_table()) {
138 die "Table m_tracked_file already exists. Bailing init...\n";
142 sub die_if_mig_tracking_table_does_not_exist {
143 if (!check_for_mig_tracking_table()) {
144 die "Table m_tracked_file does not exist. Bailing...\n";
148 sub check_for_mig_column_tracking_table {
149 my $dbh = db_connect();
150 my $sth = $dbh->prepare("
153 FROM information_schema.tables
154 WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
155 AND table_name = 'm_tracked_column'
158 my $rv = $sth->execute()
159 || die "Error checking for table (m_tracked_column): $!";
160 my @cols = $sth->fetchrow_array;
166 sub die_if_mig_column_tracking_table_exists {
167 if (check_for_mig_column_tracking_table()) {
168 die "Table m_tracked_column already exists. Bailing init...\n";
172 sub die_if_mig_column_tracking_table_does_not_exist {
173 if (!check_for_mig_column_tracking_table()) {
174 die "Table m_tracked_column does not exist. Bailing...\n";
178 sub check_for_tracked_file {
182 die "file not found: $file\n" unless $options && $options->{'allow_missing'};
184 my $dbh = db_connect();
185 my $sth = $dbh->prepare("
188 WHERE base_filename = " . $dbh->quote( $file ) . ";"
190 my $rv = $sth->execute()
191 || die "Error checking table (m_tracked_file) for base_filename ($file): $!";
192 my @cols = $sth->fetchrow_array;
198 sub check_for_tracked_column {
199 my ($table,$column,$options) = (shift,shift,shift);
200 my $dbh = db_connect();
201 my $sth = $dbh->prepare("
203 FROM m_tracked_column
204 WHERE staged_table = " . $dbh->quote( $table ) . "
205 AND staged_column = " . $dbh->quote( $column ) . ";"
207 my $rv = $sth->execute()
208 || die "Error checking table (m_tracked_column) for $table.$column: $!";
209 my @cols = $sth->fetchrow_array;
215 sub status_this_file {
217 my $dbh = db_connect();
218 my $sth = $dbh->prepare("
221 WHERE base_filename = " . $dbh->quote( $file ) . ";"
223 my $rv = $sth->execute()
224 || die "Error retrieving data from table (m_tracked_file) for base_filename ($file): $!";
225 my $data = $sth->fetchrow_hashref;
231 sub status_this_column {
232 my ($table,$column) = (shift,shift);
233 my $dbh = db_connect();
234 my $sth = $dbh->prepare("
236 FROM m_tracked_column
237 WHERE staged_table = " . $dbh->quote( $table ) . "
238 AND staged_column = " . $dbh->quote( $column ) . ";"
240 my $rv = $sth->execute()
241 || die "Error checking table (m_tracked_column) for $table.$column: $!";
242 my $data = $sth->fetchrow_hashref;