46aa1d1866fb2f154b83abeaae543c63d6fa8417
[migration-tools.git] / mig-bin / Mig.pm
1 package Mig;
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 PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
18     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
19 );
20
21 sub db_connect {
22     my $dbh = DBI->connect(
23          "dbi:Pg:host=$PGHOST;dbname=$PGDATABASE;port=$PGPORT"
24         ,$PGUSER
25         ,undef
26     ) || die "Unable to connect to $PGHOST:$PGPORT:$PGDATABASE:$PGUSER : $!\n";
27     $dbh->do("SET search_path TO $MIGSCHEMA, evergreen, pg_catalog, public");
28     return $dbh;
29 }
30 sub db_disconnect {
31     my $dbh = shift;
32     $dbh->disconnect;
33 }
34
35 sub sql {
36     my $sql = shift;
37     chomp $sql;
38     $sql =~ s/\n//g;
39     print "\n$sql\n";
40     return $sql;
41 }
42
43 sub die_if_no_env_migschema {
44     die "MIGSCHEMA environment variable not set.  See 'mig env help'\n"
45         unless $MIGSCHEMA;
46 }
47
48 sub check_for_db_migschema {
49     my $dbh = db_connect();
50     my $sth = $dbh->prepare("
51         SELECT EXISTS(
52             SELECT 1
53             FROM pg_namespace 
54             WHERE nspname = ?
55         );"
56     );
57     my $rv = $sth->execute($MIGSCHEMA)
58         || die "Error checking for migration schema ($MIGSCHEMA): $!";
59     my @cols = $sth->fetchrow_array;
60     $sth->finish;
61     my $found;
62     if ($cols[0]) {
63         print "Found migration schema ($MIGSCHEMA) at $PGHOST:$PGPORT:$PGDATABASE:$PGUSER\n";
64         $found = 1;
65     } else {
66         print "Migration schema ($MIGSCHEMA) does not exist at $PGHOST:$PGPORT:$PGDATABASE:$PGUSER\n";
67         $found = 0;
68     }
69     db_disconnect($dbh);
70     return $found;
71 }
72
73 sub check_db_migschema_for_migration_tables {
74     my $found = check_db_migschema_for_specific_table('asset_copy');
75     if (!$found) {
76         print "Missing migration tables (such as $MIGSCHEMA.asset_copy)\n";
77     }
78     return $found;
79 }
80
81 sub check_db_migschema_for_specific_table {
82     my $table = shift;
83     my $dbh = db_connect();
84     my $sth = $dbh->prepare("
85         SELECT EXISTS(
86             SELECT 1
87             FROM information_schema.tables
88             WHERE table_schema = " . $dbh->quote( $MIGSCHEMA ) . "
89             AND table_name = " . $dbh->quote( $table ) . "
90         );"
91     );
92     my $rv = $sth->execute()
93         || die "Error checking migration schema ($MIGSCHEMA) for table ($table): $!";
94     my @cols = $sth->fetchrow_array;
95     $sth->finish;
96     my $found;
97     if ($cols[0]) {
98         $found = 1;
99     } else {
100         $found = 0;
101     }
102     db_disconnect($dbh);
103     return $found;
104 }
105
106 sub check_for_migration_tools {
107     my $dbh = db_connect();
108     my $sth = $dbh->prepare("
109         SELECT EXISTS(
110             SELECT 1
111             FROM pg_namespace
112             WHERE nspname = 'migration_tools'
113         );"
114     );
115     my $rv = $sth->execute()
116         || die "Error checking for migration_tools schema: $!";
117     my @cols = $sth->fetchrow_array;
118     $sth->finish;
119     db_disconnect($dbh);
120     return $cols[0];
121 }
122
123 sub die_if_no_migration_tools {
124     if (check_for_migration_tools()) {
125         print "Found migration_tools schema\n";
126     } else {
127         die "Missing migration_tools schema\n";
128     }
129 }
130
131 sub check_for_mig_tracking_table {
132     my $dbh = db_connect();
133     my $sth = $dbh->prepare("
134         SELECT EXISTS(
135             SELECT 1
136             FROM information_schema.tables
137             WHERE table_schema = " . $dbh->quote( $MIGSCHEMA ) . "
138             AND table_name = 'tracked_file'
139         );"
140     );
141     my $rv = $sth->execute()
142         || die "Error checking for table (tracked_file): $!";
143     my @cols = $sth->fetchrow_array;
144     $sth->finish;
145     db_disconnect($dbh);
146     return $cols[0];
147 }
148
149 sub die_if_mig_tracking_table_exists {
150     if (check_for_mig_tracking_table()) {
151         die "Table $MIGSCHEMA.tracked_file already exists.  Bailing init...\n";
152     }
153 }
154
155 sub die_if_mig_tracking_table_does_not_exist {
156     if (!check_for_mig_tracking_table()) {
157         die "Table $MIGSCHEMA.tracked_file does not exist.  Bailing...\n";
158     }
159 }
160
161 sub check_for_mig_column_tracking_table {
162     my $dbh = db_connect();
163     my $sth = $dbh->prepare("
164         SELECT EXISTS(
165             SELECT 1
166             FROM information_schema.tables
167             WHERE table_schema = " . $dbh->quote( $MIGSCHEMA ) . "
168             AND table_name = 'tracked_column'
169         );"
170     );
171     my $rv = $sth->execute()
172         || die "Error checking for table (tracked_column): $!";
173     my @cols = $sth->fetchrow_array;
174     $sth->finish;
175     db_disconnect($dbh);
176     return $cols[0];
177 }
178
179 sub die_if_mig_column_tracking_table_exists {
180     if (check_for_mig_column_tracking_table()) {
181         die "Table $MIGSCHEMA.tracked_column already exists.  Bailing init...\n";
182     }
183 }
184
185 sub die_if_mig_column_tracking_table_does_not_exist {
186     if (!check_for_mig_column_tracking_table()) {
187         die "Table $MIGSCHEMA.tracked_column does not exist.  Bailing...\n";
188     }
189 }
190
191 sub check_for_tracked_file {
192     my $file = shift;
193     my $options = shift;
194     if (! -e $file) {
195         die "file not found: $file\n" unless $options && $options->{'allow_missing'};
196     }
197     my $dbh = db_connect();
198     my $sth = $dbh->prepare("
199         SELECT id
200         FROM $MIGSCHEMA.tracked_file
201         WHERE base_filename = " . $dbh->quote( $file ) . ";"
202     );
203     my $rv = $sth->execute()
204         || die "Error checking table (tracked_file) for base_filename ($file): $!";
205     my @cols = $sth->fetchrow_array;
206     $sth->finish;
207     db_disconnect($dbh);
208     return $cols[0];
209 }
210
211 sub check_for_tracked_column {
212     my ($table,$column,$options) = (shift,shift,shift);
213     my $dbh = db_connect();
214     my $sth = $dbh->prepare("
215         SELECT id
216         FROM $MIGSCHEMA.tracked_column
217         WHERE staged_table = " . $dbh->quote( $table ) . "
218         AND staged_column = " . $dbh->quote( $column ) . ";"
219     );
220     my $rv = $sth->execute()
221         || die "Error checking table (tracked_column) for $table.$column: $!";
222     my @cols = $sth->fetchrow_array;
223     $sth->finish;
224     db_disconnect($dbh);
225     return $cols[0];
226 }
227
228 sub status_this_file {
229     my $file = shift;
230     my $dbh = db_connect();
231     my $sth = $dbh->prepare("
232         SELECT *
233         FROM $MIGSCHEMA.tracked_file
234         WHERE base_filename = " . $dbh->quote( $file ) . ";"
235     );
236     my $rv = $sth->execute()
237         || die "Error retrieving data from table (tracked_file) for base_filename ($file): $!";
238     my $data = $sth->fetchrow_hashref;
239     $sth->finish;
240     db_disconnect($dbh);
241     return $data;
242 }
243
244 sub status_this_column {
245     my ($table,$column) = (shift,shift);
246     my $dbh = db_connect();
247     my $sth = $dbh->prepare("
248         SELECT *
249         FROM $MIGSCHEMA.tracked_column
250         WHERE staged_table = " . $dbh->quote( $table ) . "
251         AND staged_column = " . $dbh->quote( $column ) . ";"
252     );
253     my $rv = $sth->execute()
254         || die "Error checking table (tracked_column) for $table.$column: $!";
255     my $data = $sth->fetchrow_hashref;
256     $sth->finish;
257     db_disconnect($dbh);
258     return $data;
259 }
260
261 1;
262