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";
32 $dbh->do('SET NAMES utf8mb4');
49 sub die_if_no_env_migschema {
50 die "MIGSCHEMA environment variable not set. See 'mig env help'\n"
54 sub check_for_db_migschema {
55 # the schema is the same as the database name, which is the same
56 # as the koha instance name prefixed with 'koha_', in most cases
60 sub check_db_migschema_for_migration_tables {
61 my $found = check_db_migschema_for_specific_table('m_borrowers');
63 print "Missing migration tables (such as m_borrowers)\n";
68 sub check_db_migschema_for_specific_table {
70 my $dbh = db_connect();
71 my $sth = $dbh->prepare("
74 FROM information_schema.tables
75 WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
76 AND table_name = " . $dbh->quote( $table ) . "
79 my $rv = $sth->execute()
80 || die "Error checking migration schema ($MIGSCHEMA) for table ($table): $!";
81 my @cols = $sth->fetchrow_array;
93 sub check_for_mig_tracking_table {
94 my $dbh = db_connect();
95 my $sth = $dbh->prepare("
98 FROM information_schema.tables
99 WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
100 AND table_name = 'm_tracked_file'
103 my $rv = $sth->execute()
104 || die "Error checking for table (m_tracked_file): $!";
105 my @cols = $sth->fetchrow_array;
111 sub die_if_mig_tracking_table_exists {
112 if (check_for_mig_tracking_table()) {
113 die "Table m_tracked_file already exists. Bailing init...\n";
117 sub die_if_mig_tracking_table_does_not_exist {
118 if (!check_for_mig_tracking_table()) {
119 die "Table m_tracked_file does not exist. Bailing...\n";
123 sub check_for_mig_column_tracking_table {
124 my $dbh = db_connect();
125 my $sth = $dbh->prepare("
128 FROM information_schema.tables
129 WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
130 AND table_name = 'm_tracked_column'
133 my $rv = $sth->execute()
134 || die "Error checking for table (m_tracked_column): $!";
135 my @cols = $sth->fetchrow_array;
141 sub die_if_mig_column_tracking_table_exists {
142 if (check_for_mig_column_tracking_table()) {
143 die "Table m_tracked_column already exists. Bailing init...\n";
147 sub die_if_mig_column_tracking_table_does_not_exist {
148 if (!check_for_mig_column_tracking_table()) {
149 die "Table m_tracked_column does not exist. Bailing...\n";
153 sub check_for_tracked_file {
157 die "file not found: $file\n" unless $options && $options->{'allow_missing'};
159 my $dbh = db_connect();
160 my $sth = $dbh->prepare("
163 WHERE base_filename = " . $dbh->quote( $file ) . ";"
165 my $rv = $sth->execute()
166 || die "Error checking table (m_tracked_file) for base_filename ($file): $!";
167 my @cols = $sth->fetchrow_array;
173 sub check_for_tracked_column {
174 my ($table,$column,$options) = (shift,shift,shift);
175 my $dbh = db_connect();
176 my $sth = $dbh->prepare("
178 FROM m_tracked_column
179 WHERE staged_table = " . $dbh->quote( $table ) . "
180 AND staged_column = " . $dbh->quote( $column ) . ";"
182 my $rv = $sth->execute()
183 || die "Error checking table (m_tracked_column) for $table.$column: $!";
184 my @cols = $sth->fetchrow_array;
190 sub status_this_file {
192 my $dbh = db_connect();
193 my $sth = $dbh->prepare("
196 WHERE base_filename = " . $dbh->quote( $file ) . ";"
198 my $rv = $sth->execute()
199 || die "Error retrieving data from table (m_tracked_file) for base_filename ($file): $!";
200 my $data = $sth->fetchrow_hashref;
206 sub status_this_column {
207 my ($table,$column) = (shift,shift);
208 my $dbh = db_connect();
209 my $sth = $dbh->prepare("
211 FROM m_tracked_column
212 WHERE staged_table = " . $dbh->quote( $table ) . "
213 AND staged_column = " . $dbh->quote( $column ) . ";"
215 my $rv = $sth->execute()
216 || die "Error checking table (m_tracked_column) for $table.$column: $!";
217 my $data = $sth->fetchrow_hashref;