remove the migration-tools expectation for kmig
[migration-tools.git] / kmig.d / bin / KMig.pm
1 package KMig;
2
3 use strict;
4 use Exporter;
5 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
6
7 $VERSION        = 1.00;
8 @ISA            = qw(Exporter);
9 @EXPORT         = ();
10 @EXPORT_OK      = qw();
11 %EXPORT_TAGS    = (
12                      DEFAULT => []
13 );
14
15 use DBI;
16 use Env qw(
17     HOME MYSQL_HOST MYSQL_TCP_PORT MYSQL_USER MYSQL_DATABASE MYSQL_PW
18     MIGSCHEMA MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
19 );
20
21 sub db_connect {
22     my $dbh;
23     if ($MYSQL_HOST) {
24         $dbh = DBI->connect(
25          "dbi:mysql:host=$MYSQL_HOST;dbname=$MYSQL_DATABASE;port=$MYSQL_TCP_PORT"
26         ,$MYSQL_USER
27         ,$MYSQL_PW
28         ) || die "Unable to connect to $MYSQL_HOST:$MYSQL_TCP_PORT:$MYSQL_DATABASE:$MYSQL_USER : $!\n";
29     } else {
30         $dbh = DBI->connect("dbi:Pg:dbname=$MYSQL_DATABASE", "", "") || die "Unable to connect to $MYSQL_DATABASE : $!\n";
31     }
32     return $dbh;
33 }
34
35 sub db_disconnect {
36     my $dbh = shift;
37     $dbh->disconnect;
38 }
39
40 sub sql {
41     my $sql = shift;
42     chomp $sql;
43     $sql =~ s/\n//g;
44     print "\n$sql\n";
45     return $sql;
46 }
47
48 sub die_if_no_env_migschema {
49     die "MIGSCHEMA environment variable not set.  See 'mig env help'\n"
50         unless $MIGSCHEMA;
51 }
52
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
56     return 1;
57 }
58
59 sub check_db_migschema_for_migration_tables {
60     my $found = check_db_migschema_for_specific_table('m_borrowers');
61     if (!$found) {
62         print "Missing migration tables (such as m_borrowers)\n";
63     }
64     return $found;
65 }
66
67 sub check_db_migschema_for_specific_table {
68     my $table = shift;
69     my $dbh = db_connect();
70     my $sth = $dbh->prepare("
71         SELECT EXISTS(
72             SELECT 1
73             FROM information_schema.tables
74             WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
75             AND table_name = " . $dbh->quote( $table ) . "
76         );"
77     );
78     my $rv = $sth->execute()
79         || die "Error checking migration schema ($MIGSCHEMA) for table ($table): $!";
80     my @cols = $sth->fetchrow_array;
81     $sth->finish;
82     my $found;
83     if ($cols[0]) {
84         $found = 1;
85     } else {
86         $found = 0;
87     }
88     db_disconnect($dbh);
89     return $found;
90 }
91
92 sub check_for_mig_tracking_table {
93     my $dbh = db_connect();
94     my $sth = $dbh->prepare("
95         SELECT EXISTS(
96             SELECT 1
97             FROM information_schema.tables
98             WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
99             AND table_name = 'm_tracked_file'
100         );"
101     );
102     my $rv = $sth->execute()
103         || die "Error checking for table (m_tracked_file): $!";
104     my @cols = $sth->fetchrow_array;
105     $sth->finish;
106     db_disconnect($dbh);
107     return $cols[0];
108 }
109
110 sub die_if_mig_tracking_table_exists {
111     if (check_for_mig_tracking_table()) {
112         die "Table m_tracked_file already exists.  Bailing init...\n";
113     }
114 }
115
116 sub die_if_mig_tracking_table_does_not_exist {
117     if (!check_for_mig_tracking_table()) {
118         die "Table m_tracked_file does not exist.  Bailing...\n";
119     }
120 }
121
122 sub check_for_mig_column_tracking_table {
123     my $dbh = db_connect();
124     my $sth = $dbh->prepare("
125         SELECT EXISTS(
126             SELECT 1
127             FROM information_schema.tables
128             WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
129             AND table_name = 'm_tracked_column'
130         );"
131     );
132     my $rv = $sth->execute()
133         || die "Error checking for table (m_tracked_column): $!";
134     my @cols = $sth->fetchrow_array;
135     $sth->finish;
136     db_disconnect($dbh);
137     return $cols[0];
138 }
139
140 sub die_if_mig_column_tracking_table_exists {
141     if (check_for_mig_column_tracking_table()) {
142         die "Table m_tracked_column already exists.  Bailing init...\n";
143     }
144 }
145
146 sub die_if_mig_column_tracking_table_does_not_exist {
147     if (!check_for_mig_column_tracking_table()) {
148         die "Table m_tracked_column does not exist.  Bailing...\n";
149     }
150 }
151
152 sub check_for_tracked_file {
153     my $file = shift;
154     my $options = shift;
155     if (! -e $file) {
156         die "file not found: $file\n" unless $options && $options->{'allow_missing'};
157     }
158     my $dbh = db_connect();
159     my $sth = $dbh->prepare("
160         SELECT id
161         FROM m_tracked_file
162         WHERE base_filename = " . $dbh->quote( $file ) . ";"
163     );
164     my $rv = $sth->execute()
165         || die "Error checking table (m_tracked_file) for base_filename ($file): $!";
166     my @cols = $sth->fetchrow_array;
167     $sth->finish;
168     db_disconnect($dbh);
169     return $cols[0];
170 }
171
172 sub check_for_tracked_column {
173     my ($table,$column,$options) = (shift,shift,shift);
174     my $dbh = db_connect();
175     my $sth = $dbh->prepare("
176         SELECT id
177         FROM m_tracked_column
178         WHERE staged_table = " . $dbh->quote( $table ) . "
179         AND staged_column = " . $dbh->quote( $column ) . ";"
180     );
181     my $rv = $sth->execute()
182         || die "Error checking table (m_tracked_column) for $table.$column: $!";
183     my @cols = $sth->fetchrow_array;
184     $sth->finish;
185     db_disconnect($dbh);
186     return $cols[0];
187 }
188
189 sub status_this_file {
190     my $file = shift;
191     my $dbh = db_connect();
192     my $sth = $dbh->prepare("
193         SELECT *
194         FROM m_tracked_file
195         WHERE base_filename = " . $dbh->quote( $file ) . ";"
196     );
197     my $rv = $sth->execute()
198         || die "Error retrieving data from table (m_tracked_file) for base_filename ($file): $!";
199     my $data = $sth->fetchrow_hashref;
200     $sth->finish;
201     db_disconnect($dbh);
202     return $data;
203 }
204
205 sub status_this_column {
206     my ($table,$column) = (shift,shift);
207     my $dbh = db_connect();
208     my $sth = $dbh->prepare("
209         SELECT *
210         FROM m_tracked_column
211         WHERE staged_table = " . $dbh->quote( $table ) . "
212         AND staged_column = " . $dbh->quote( $column ) . ";"
213     );
214     my $rv = $sth->execute()
215         || die "Error checking table (m_tracked_column) for $table.$column: $!";
216     my $data = $sth->fetchrow_hashref;
217     $sth->finish;
218     db_disconnect($dbh);
219     return $data;
220 }
221
222 1;
223