5 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
17 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
18 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
25 "dbi:Pg:host=$PGHOST;dbname=$PGDATABASE;port=$PGPORT"
28 ) || die "Unable to connect to $PGHOST:$PGPORT:$PGDATABASE:$PGUSER : $!\n";
30 $dbh = DBI->connect("dbi:Pg:dbname=$PGDATABASE", "", "") || die "Unable to connect to $PGDATABASE : $!\n";
32 $dbh->do("SET search_path TO $MIGSCHEMA, evergreen, pg_catalog, public");
49 sub die_if_no_env_migschema {
50 die "MIGSCHEMA environment variable not set. See 'mig env help'\n"
54 sub check_for_db_migschema {
55 my $dbh = db_connect();
56 my $sth = $dbh->prepare("
63 my $rv = $sth->execute($MIGSCHEMA)
64 || die "Error checking for migration schema ($MIGSCHEMA): $!";
65 my @cols = $sth->fetchrow_array;
69 print "Found migration schema ($MIGSCHEMA) at $PGHOST:$PGPORT:$PGDATABASE:$PGUSER\n";
72 print "Migration schema ($MIGSCHEMA) does not exist at $PGHOST:$PGPORT:$PGDATABASE:$PGUSER\n";
79 sub check_db_migschema_for_migration_tables {
80 my $found = check_db_migschema_for_specific_table('asset_copy');
82 print "Missing migration tables (such as $MIGSCHEMA.asset_copy)\n";
87 sub check_db_migschema_for_specific_table {
89 my $dbh = db_connect();
90 my $sth = $dbh->prepare("
93 FROM information_schema.tables
94 WHERE table_schema = " . $dbh->quote( $MIGSCHEMA ) . "
95 AND table_name = " . $dbh->quote( $table ) . "
98 my $rv = $sth->execute()
99 || die "Error checking migration schema ($MIGSCHEMA) for table ($table): $!";
100 my @cols = $sth->fetchrow_array;
112 sub check_for_migration_tools {
113 my $dbh = db_connect();
114 my $sth = $dbh->prepare("
118 WHERE nspname = 'migration_tools'
121 my $rv = $sth->execute()
122 || die "Error checking for migration_tools schema: $!";
123 my @cols = $sth->fetchrow_array;
129 sub die_if_no_migration_tools {
130 if (check_for_migration_tools()) {
131 print "Found migration_tools schema\n";
133 die "Missing migration_tools schema\n";
137 sub check_for_mig_tracking_table {
138 my $dbh = db_connect();
139 my $sth = $dbh->prepare("
142 FROM information_schema.tables
143 WHERE table_schema = " . $dbh->quote( $MIGSCHEMA ) . "
144 AND table_name = 'tracked_file'
147 my $rv = $sth->execute()
148 || die "Error checking for table (tracked_file): $!";
149 my @cols = $sth->fetchrow_array;
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";
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";
167 sub check_for_mig_column_tracking_table {
168 my $dbh = db_connect();
169 my $sth = $dbh->prepare("
172 FROM information_schema.tables
173 WHERE table_schema = " . $dbh->quote( $MIGSCHEMA ) . "
174 AND table_name = 'tracked_column'
177 my $rv = $sth->execute()
178 || die "Error checking for table (tracked_column): $!";
179 my @cols = $sth->fetchrow_array;
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";
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";
197 sub check_for_tracked_file {
201 die "file not found: $file\n" unless $options && $options->{'allow_missing'};
203 my $dbh = db_connect();
204 my $sth = $dbh->prepare("
206 FROM $MIGSCHEMA.tracked_file
207 WHERE base_filename = " . $dbh->quote( $file ) . ";"
209 my $rv = $sth->execute()
210 || die "Error checking table (tracked_file) for base_filename ($file): $!";
211 my @cols = $sth->fetchrow_array;
217 sub check_for_tracked_column {
218 my ($table,$column,$options) = (shift,shift,shift);
219 my $dbh = db_connect();
220 my $sth = $dbh->prepare("
222 FROM $MIGSCHEMA.tracked_column
223 WHERE staged_table = " . $dbh->quote( $table ) . "
224 AND staged_column = " . $dbh->quote( $column ) . ";"
226 my $rv = $sth->execute()
227 || die "Error checking table (tracked_column) for $table.$column: $!";
228 my @cols = $sth->fetchrow_array;
234 sub status_this_file {
236 my $dbh = db_connect();
237 my $sth = $dbh->prepare("
239 FROM $MIGSCHEMA.tracked_file
240 WHERE base_filename = " . $dbh->quote( $file ) . ";"
242 my $rv = $sth->execute()
243 || die "Error retrieving data from table (tracked_file) for base_filename ($file): $!";
244 my $data = $sth->fetchrow_hashref;
250 sub status_this_column {
251 my ($table,$column) = (shift,shift);
252 my $dbh = db_connect();
253 my $sth = $dbh->prepare("
255 FROM $MIGSCHEMA.tracked_column
256 WHERE staged_table = " . $dbh->quote( $table ) . "
257 AND staged_column = " . $dbh->quote( $column ) . ";"
259 my $rv = $sth->execute()
260 || die "Error checking table (tracked_column) for $table.$column: $!";
261 my $data = $sth->fetchrow_hashref;