change to the data directly automatically with mig env use
[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     $dbh->do('SET NAMES utf8mb4');
33     return $dbh;
34 }
35
36 sub db_disconnect {
37     my $dbh = shift;
38     $dbh->disconnect;
39 }
40
41 sub sql {
42     my $sql = shift;
43     chomp $sql;
44     $sql =~ s/\n//g;
45     print "\n$sql\n";
46     return $sql;
47 }
48
49 sub die_if_no_env_migschema {
50     die "MIGSCHEMA environment variable not set.  See 'mig env help'\n"
51         unless $MIGSCHEMA;
52 }
53
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
57     return 1;
58 }
59
60 sub check_db_migschema_for_migration_tables {
61     my $found = check_db_migschema_for_specific_table('m_borrowers');
62     if (!$found) {
63         print "Missing migration tables (such as m_borrowers)\n";
64     }
65     return $found;
66 }
67
68 sub check_db_migschema_for_specific_table {
69     my $table = shift;
70     my $dbh = db_connect();
71     my $sth = $dbh->prepare("
72         SELECT EXISTS(
73             SELECT 1
74             FROM information_schema.tables
75             WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
76             AND table_name = " . $dbh->quote( $table ) . "
77         );"
78     );
79     my $rv = $sth->execute()
80         || die "Error checking migration schema ($MIGSCHEMA) for table ($table): $!";
81     my @cols = $sth->fetchrow_array;
82     $sth->finish;
83     my $found;
84     if ($cols[0]) {
85         $found = 1;
86     } else {
87         $found = 0;
88     }
89     db_disconnect($dbh);
90     return $found;
91 }
92
93 sub check_for_mig_tracking_table {
94     my $dbh = db_connect();
95     my $sth = $dbh->prepare("
96         SELECT EXISTS(
97             SELECT 1
98             FROM information_schema.tables
99             WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
100             AND table_name = 'm_tracked_file'
101         );"
102     );
103     my $rv = $sth->execute()
104         || die "Error checking for table (m_tracked_file): $!";
105     my @cols = $sth->fetchrow_array;
106     $sth->finish;
107     db_disconnect($dbh);
108     return $cols[0];
109 }
110
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";
114     }
115 }
116
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";
120     }
121 }
122
123 sub check_for_mig_column_tracking_table {
124     my $dbh = db_connect();
125     my $sth = $dbh->prepare("
126         SELECT EXISTS(
127             SELECT 1
128             FROM information_schema.tables
129             WHERE table_schema = " . $dbh->quote( $MYSQL_DATABASE ) . "
130             AND table_name = 'm_tracked_column'
131         );"
132     );
133     my $rv = $sth->execute()
134         || die "Error checking for table (m_tracked_column): $!";
135     my @cols = $sth->fetchrow_array;
136     $sth->finish;
137     db_disconnect($dbh);
138     return $cols[0];
139 }
140
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";
144     }
145 }
146
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";
150     }
151 }
152
153 sub check_for_tracked_file {
154     my $file = shift;
155     my $options = shift;
156     if (! -e $file) {
157         die "file not found: $file\n" unless $options && $options->{'allow_missing'};
158     }
159     my $dbh = db_connect();
160     my $sth = $dbh->prepare("
161         SELECT id
162         FROM m_tracked_file
163         WHERE base_filename = " . $dbh->quote( $file ) . ";"
164     );
165     my $rv = $sth->execute()
166         || die "Error checking table (m_tracked_file) for base_filename ($file): $!";
167     my @cols = $sth->fetchrow_array;
168     $sth->finish;
169     db_disconnect($dbh);
170     return $cols[0];
171 }
172
173 sub check_for_tracked_column {
174     my ($table,$column,$options) = (shift,shift,shift);
175     my $dbh = db_connect();
176     my $sth = $dbh->prepare("
177         SELECT id
178         FROM m_tracked_column
179         WHERE staged_table = " . $dbh->quote( $table ) . "
180         AND staged_column = " . $dbh->quote( $column ) . ";"
181     );
182     my $rv = $sth->execute()
183         || die "Error checking table (m_tracked_column) for $table.$column: $!";
184     my @cols = $sth->fetchrow_array;
185     $sth->finish;
186     db_disconnect($dbh);
187     return $cols[0];
188 }
189
190 sub status_this_file {
191     my $file = shift;
192     my $dbh = db_connect();
193     my $sth = $dbh->prepare("
194         SELECT *
195         FROM m_tracked_file
196         WHERE base_filename = " . $dbh->quote( $file ) . ";"
197     );
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;
201     $sth->finish;
202     db_disconnect($dbh);
203     return $data;
204 }
205
206 sub status_this_column {
207     my ($table,$column) = (shift,shift);
208     my $dbh = db_connect();
209     my $sth = $dbh->prepare("
210         SELECT *
211         FROM m_tracked_column
212         WHERE staged_table = " . $dbh->quote( $table ) . "
213         AND staged_column = " . $dbh->quote( $column ) . ";"
214     );
215     my $rv = $sth->execute()
216         || die "Error checking table (m_tracked_column) for $table.$column: $!";
217     my $data = $sth->fetchrow_hashref;
218     $sth->finish;
219     db_disconnect($dbh);
220     return $data;
221 }
222
223 1;
224