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