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