5 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
17 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
18 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
22 my $dbh = DBI->connect(
23 "dbi:Pg:host=$PGHOST;dbname=$PGDATABASE;port=$PGPORT"
26 ) || die "Unable to connect to $PGHOST:$PGPORT:$PGDATABASE:$PGUSER : $!\n";
27 $dbh->do("SET search_path TO $MIGSCHEMA, evergreen, pg_catalog, public");
43 sub die_if_no_env_migschema {
44 die "MIGSCHEMA environment variable not set. See 'mig env help'\n"
48 sub check_for_db_migschema {
49 my $dbh = db_connect();
50 my $sth = $dbh->prepare("
57 my $rv = $sth->execute($MIGSCHEMA)
58 || die "Error checking for migration schema ($MIGSCHEMA): $!";
59 my @cols = $sth->fetchrow_array;
63 print "Found migration schema ($MIGSCHEMA) at $PGHOST:$PGPORT:$PGDATABASE:$PGUSER\n";
66 print "Migration schema ($MIGSCHEMA) does not exist at $PGHOST:$PGPORT:$PGDATABASE:$PGUSER\n";
73 sub check_db_migschema_for_migration_tables {
74 my $found = check_db_migschema_for_specific_table('asset_copy');
76 print "Missing migration tables (such as $MIGSCHEMA.asset_copy)\n";
81 sub check_db_migschema_for_specific_table {
83 my $dbh = db_connect();
84 my $sth = $dbh->prepare("
87 FROM information_schema.tables
88 WHERE table_schema = " . $dbh->quote( $MIGSCHEMA ) . "
89 AND table_name = " . $dbh->quote( $table ) . "
92 my $rv = $sth->execute()
93 || die "Error checking migration schema ($MIGSCHEMA) for table ($table): $!";
94 my @cols = $sth->fetchrow_array;
106 sub check_for_migration_tools {
107 my $dbh = db_connect();
108 my $sth = $dbh->prepare("
112 WHERE nspname = 'migration_tools'
115 my $rv = $sth->execute()
116 || die "Error checking for migration_tools schema: $!";
117 my @cols = $sth->fetchrow_array;
123 sub die_if_no_migration_tools {
124 if (check_for_migration_tools()) {
125 print "Found migration_tools schema\n";
127 die "Missing migration_tools schema\n";
131 sub check_for_mig_tracking_table {
132 my $dbh = db_connect();
133 my $sth = $dbh->prepare("
136 FROM information_schema.tables
137 WHERE table_schema = " . $dbh->quote( $MIGSCHEMA ) . "
138 AND table_name = 'tracked_file'
141 my $rv = $sth->execute()
142 || die "Error checking for table (tracked_file): $!";
143 my @cols = $sth->fetchrow_array;
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";
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";
161 sub check_for_mig_column_tracking_table {
162 my $dbh = db_connect();
163 my $sth = $dbh->prepare("
166 FROM information_schema.tables
167 WHERE table_schema = " . $dbh->quote( $MIGSCHEMA ) . "
168 AND table_name = 'tracked_column'
171 my $rv = $sth->execute()
172 || die "Error checking for table (tracked_column): $!";
173 my @cols = $sth->fetchrow_array;
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";
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";
191 sub check_for_tracked_file {
195 die "file not found: $file\n" unless $options && $options->{'allow_missing'};
197 my $dbh = db_connect();
198 my $sth = $dbh->prepare("
200 FROM $MIGSCHEMA.tracked_file
201 WHERE base_filename = " . $dbh->quote( $file ) . ";"
203 my $rv = $sth->execute()
204 || die "Error checking table (tracked_file) for base_filename ($file): $!";
205 my @cols = $sth->fetchrow_array;
211 sub check_for_tracked_column {
212 my ($table,$column,$options) = (shift,shift,shift);
213 my $dbh = db_connect();
214 my $sth = $dbh->prepare("
216 FROM $MIGSCHEMA.tracked_column
217 WHERE staged_table = " . $dbh->quote( $table ) . "
218 AND staged_column = " . $dbh->quote( $column ) . ";"
220 my $rv = $sth->execute()
221 || die "Error checking table (tracked_column) for $table.$column: $!";
222 my @cols = $sth->fetchrow_array;
228 sub status_this_file {
230 my $dbh = db_connect();
231 my $sth = $dbh->prepare("
233 FROM $MIGSCHEMA.tracked_file
234 WHERE base_filename = " . $dbh->quote( $file ) . ";"
236 my $rv = $sth->execute()
237 || die "Error retrieving data from table (tracked_file) for base_filename ($file): $!";
238 my $data = $sth->fetchrow_hashref;
244 sub status_this_column {
245 my ($table,$column) = (shift,shift);
246 my $dbh = db_connect();
247 my $sth = $dbh->prepare("
249 FROM $MIGSCHEMA.tracked_column
250 WHERE staged_table = " . $dbh->quote( $table ) . "
251 AND staged_column = " . $dbh->quote( $column ) . ";"
253 my $rv = $sth->execute()
254 || die "Error checking table (tracked_column) for $table.$column: $!";
255 my $data = $sth->fetchrow_hashref;