first cut of kmig-env, and KMig.pm comment tweak
[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_migration_tools {
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 = " . $dbh->quote( 'mt_init' ) . "
100         );"
101     );
102     my $rv = $sth->execute()
103         || die "Error checking for migration_tools: $!";
104     my @cols = $sth->fetchrow_array;
105     $sth->finish;
106     db_disconnect($dbh);
107     return $cols[0];
108 }
109
110 sub die_if_no_migration_tools {
111     if (check_for_migration_tools()) {
112         print "Found migration_tools\n";
113     } else {
114         die "Missing migration_tools\n";
115     }
116 }
117
118 sub check_for_mig_tracking_table {
119     my $dbh = db_connect();
120     my $sth = $dbh->prepare("
121         SELECT EXISTS(
122             SELECT 1
123             FROM information_schema.tables
124             WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
125             AND table_name = 'm_tracked_file'
126         );"
127     );
128     my $rv = $sth->execute()
129         || die "Error checking for table (m_tracked_file): $!";
130     my @cols = $sth->fetchrow_array;
131     $sth->finish;
132     db_disconnect($dbh);
133     return $cols[0];
134 }
135
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";
139     }
140 }
141
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";
145     }
146 }
147
148 sub check_for_mig_column_tracking_table {
149     my $dbh = db_connect();
150     my $sth = $dbh->prepare("
151         SELECT EXISTS(
152             SELECT 1
153             FROM information_schema.tables
154             WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
155             AND table_name = 'm_tracked_column'
156         );"
157     );
158     my $rv = $sth->execute()
159         || die "Error checking for table (m_tracked_column): $!";
160     my @cols = $sth->fetchrow_array;
161     $sth->finish;
162     db_disconnect($dbh);
163     return $cols[0];
164 }
165
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";
169     }
170 }
171
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";
175     }
176 }
177
178 sub check_for_tracked_file {
179     my $file = shift;
180     my $options = shift;
181     if (! -e $file) {
182         die "file not found: $file\n" unless $options && $options->{'allow_missing'};
183     }
184     my $dbh = db_connect();
185     my $sth = $dbh->prepare("
186         SELECT id
187         FROM m_tracked_file
188         WHERE base_filename = " . $dbh->quote( $file ) . ";"
189     );
190     my $rv = $sth->execute()
191         || die "Error checking table (m_tracked_file) for base_filename ($file): $!";
192     my @cols = $sth->fetchrow_array;
193     $sth->finish;
194     db_disconnect($dbh);
195     return $cols[0];
196 }
197
198 sub check_for_tracked_column {
199     my ($table,$column,$options) = (shift,shift,shift);
200     my $dbh = db_connect();
201     my $sth = $dbh->prepare("
202         SELECT id
203         FROM m_tracked_column
204         WHERE staged_table = " . $dbh->quote( $table ) . "
205         AND staged_column = " . $dbh->quote( $column ) . ";"
206     );
207     my $rv = $sth->execute()
208         || die "Error checking table (m_tracked_column) for $table.$column: $!";
209     my @cols = $sth->fetchrow_array;
210     $sth->finish;
211     db_disconnect($dbh);
212     return $cols[0];
213 }
214
215 sub status_this_file {
216     my $file = shift;
217     my $dbh = db_connect();
218     my $sth = $dbh->prepare("
219         SELECT *
220         FROM m_tracked_file
221         WHERE base_filename = " . $dbh->quote( $file ) . ";"
222     );
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;
226     $sth->finish;
227     db_disconnect($dbh);
228     return $data;
229 }
230
231 sub status_this_column {
232     my ($table,$column) = (shift,shift);
233     my $dbh = db_connect();
234     my $sth = $dbh->prepare("
235         SELECT *
236         FROM m_tracked_column
237         WHERE staged_table = " . $dbh->quote( $table ) . "
238         AND staged_column = " . $dbh->quote( $column ) . ";"
239     );
240     my $rv = $sth->execute()
241         || die "Error checking table (m_tracked_column) for $table.$column: $!";
242     my $data = $sth->fetchrow_hashref;
243     $sth->finish;
244     db_disconnect($dbh);
245     return $data;
246 }
247
248 1;
249