first cut of KMig.pm
[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     return 1; # the schema is the same as the database name, which is the same as the koha instance name, in most cases
55 }
56
57 sub check_db_migschema_for_migration_tables {
58     my $found = check_db_migschema_for_specific_table('m_borrowers');
59     if (!$found) {
60         print "Missing migration tables (such as m_borrowers)\n";
61     }
62     return $found;
63 }
64
65 sub check_db_migschema_for_specific_table {
66     my $table = shift;
67     my $dbh = db_connect();
68     my $sth = $dbh->prepare("
69         SELECT EXISTS(
70             SELECT 1
71             FROM information_schema.tables
72             WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
73             AND table_name = " . $dbh->quote( $table ) . "
74         );"
75     );
76     my $rv = $sth->execute()
77         || die "Error checking migration schema ($MIGSCHEMA) for table ($table): $!";
78     my @cols = $sth->fetchrow_array;
79     $sth->finish;
80     my $found;
81     if ($cols[0]) {
82         $found = 1;
83     } else {
84         $found = 0;
85     }
86     db_disconnect($dbh);
87     return $found;
88 }
89
90 sub check_for_migration_tools {
91     my $dbh = db_connect();
92     my $sth = $dbh->prepare("
93         SELECT EXISTS(
94             SELECT 1
95             FROM information_schema.tables
96             WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
97             AND table_name = " . $dbh->quote( 'mt_init' ) . "
98         );"
99     );
100     my $rv = $sth->execute()
101         || die "Error checking for migration_tools: $!";
102     my @cols = $sth->fetchrow_array;
103     $sth->finish;
104     db_disconnect($dbh);
105     return $cols[0];
106 }
107
108 sub die_if_no_migration_tools {
109     if (check_for_migration_tools()) {
110         print "Found migration_tools\n";
111     } else {
112         die "Missing migration_tools\n";
113     }
114 }
115
116 sub check_for_mig_tracking_table {
117     my $dbh = db_connect();
118     my $sth = $dbh->prepare("
119         SELECT EXISTS(
120             SELECT 1
121             FROM information_schema.tables
122             WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
123             AND table_name = 'm_tracked_file'
124         );"
125     );
126     my $rv = $sth->execute()
127         || die "Error checking for table (m_tracked_file): $!";
128     my @cols = $sth->fetchrow_array;
129     $sth->finish;
130     db_disconnect($dbh);
131     return $cols[0];
132 }
133
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";
137     }
138 }
139
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";
143     }
144 }
145
146 sub check_for_mig_column_tracking_table {
147     my $dbh = db_connect();
148     my $sth = $dbh->prepare("
149         SELECT EXISTS(
150             SELECT 1
151             FROM information_schema.tables
152             WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
153             AND table_name = 'm_tracked_column'
154         );"
155     );
156     my $rv = $sth->execute()
157         || die "Error checking for table (m_tracked_column): $!";
158     my @cols = $sth->fetchrow_array;
159     $sth->finish;
160     db_disconnect($dbh);
161     return $cols[0];
162 }
163
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";
167     }
168 }
169
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";
173     }
174 }
175
176 sub check_for_tracked_file {
177     my $file = shift;
178     my $options = shift;
179     if (! -e $file) {
180         die "file not found: $file\n" unless $options && $options->{'allow_missing'};
181     }
182     my $dbh = db_connect();
183     my $sth = $dbh->prepare("
184         SELECT id
185         FROM m_tracked_file
186         WHERE base_filename = " . $dbh->quote( $file ) . ";"
187     );
188     my $rv = $sth->execute()
189         || die "Error checking table (m_tracked_file) for base_filename ($file): $!";
190     my @cols = $sth->fetchrow_array;
191     $sth->finish;
192     db_disconnect($dbh);
193     return $cols[0];
194 }
195
196 sub check_for_tracked_column {
197     my ($table,$column,$options) = (shift,shift,shift);
198     my $dbh = db_connect();
199     my $sth = $dbh->prepare("
200         SELECT id
201         FROM m_tracked_column
202         WHERE staged_table = " . $dbh->quote( $table ) . "
203         AND staged_column = " . $dbh->quote( $column ) . ";"
204     );
205     my $rv = $sth->execute()
206         || die "Error checking table (m_tracked_column) for $table.$column: $!";
207     my @cols = $sth->fetchrow_array;
208     $sth->finish;
209     db_disconnect($dbh);
210     return $cols[0];
211 }
212
213 sub status_this_file {
214     my $file = shift;
215     my $dbh = db_connect();
216     my $sth = $dbh->prepare("
217         SELECT *
218         FROM m_tracked_file
219         WHERE base_filename = " . $dbh->quote( $file ) . ";"
220     );
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;
224     $sth->finish;
225     db_disconnect($dbh);
226     return $data;
227 }
228
229 sub status_this_column {
230     my ($table,$column) = (shift,shift);
231     my $dbh = db_connect();
232     my $sth = $dbh->prepare("
233         SELECT *
234         FROM m_tracked_column
235         WHERE staged_table = " . $dbh->quote( $table ) . "
236         AND staged_column = " . $dbh->quote( $column ) . ";"
237     );
238     my $rv = $sth->execute()
239         || die "Error checking table (m_tracked_column) for $table.$column: $!";
240     my $data = $sth->fetchrow_hashref;
241     $sth->finish;
242     db_disconnect($dbh);
243     return $data;
244 }
245
246 1;
247