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";
42 sub die_if_no_env_migschema {
43 die "MIGSCHEMA environment variable not set. See 'mig env help'\n"
47 sub check_for_db_migschema {
48 my $dbh = db_connect();
49 my $sth = $dbh->prepare("
56 my $rv = $sth->execute($MIGSCHEMA)
57 || die "Error checking for migration schema ($MIGSCHEMA): $!";
58 my @cols = $sth->fetchrow_array;
62 print "Found migration schema ($MIGSCHEMA) at $PGHOST:$PGPORT:$PGDATABASE:$PGUSER\n";
65 print "Migration schema ($MIGSCHEMA) does not exist at $PGHOST:$PGPORT:$PGDATABASE:$PGUSER\n";
72 sub check_db_migschema_for_migration_tables {
73 my $found = check_db_migschema_for_specific_table('asset_copy');
75 print "Missing migration tables (such as $MIGSCHEMA.asset_copy)\n";
80 sub check_db_migschema_for_specific_table {
82 my $dbh = db_connect();
83 my $sth = $dbh->prepare("
86 FROM information_schema.tables
87 WHERE table_schema = " . $dbh->quote( $MIGSCHEMA ) . "
88 AND table_name = " . $dbh->quote( $table ) . "
91 my $rv = $sth->execute()
92 || die "Error checking migration schema ($MIGSCHEMA) for table ($table): $!";
93 my @cols = $sth->fetchrow_array;
105 sub check_for_migration_tools {
106 my $dbh = db_connect();
107 my $sth = $dbh->prepare("
111 WHERE nspname = 'migration_tools'
114 my $rv = $sth->execute()
115 || die "Error checking for migration_tools schema: $!";
116 my @cols = $sth->fetchrow_array;
122 sub die_if_no_migration_tools {
123 if (check_for_migration_tools()) {
124 print "Found migration_tools schema\n";
126 die "Missing migration_tools schema\n";
130 sub check_for_mig_tracking_table {
131 my $dbh = db_connect();
132 my $sth = $dbh->prepare("
135 FROM information_schema.tables
136 WHERE table_schema = " . $dbh->quote( $MIGSCHEMA ) . "
137 AND table_name = 'tracked_file'
140 my $rv = $sth->execute()
141 || die "Error checking for table (tracked_file): $!";
142 my @cols = $sth->fetchrow_array;
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";
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";
160 sub check_for_mig_column_tracking_table {
161 my $dbh = db_connect();
162 my $sth = $dbh->prepare("
165 FROM information_schema.tables
166 WHERE table_schema = " . $dbh->quote( $MIGSCHEMA ) . "
167 AND table_name = 'tracked_column'
170 my $rv = $sth->execute()
171 || die "Error checking for table (tracked_column): $!";
172 my @cols = $sth->fetchrow_array;
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";
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";
190 sub check_for_tracked_file {
194 die "file not found: $file\n" unless $options && $options->{'allow_missing'};
196 my $dbh = db_connect();
197 my $sth = $dbh->prepare("
199 FROM $MIGSCHEMA.tracked_file
200 WHERE base_filename = " . $dbh->quote( $file ) . ";"
202 my $rv = $sth->execute()
203 || die "Error checking table (tracked_file) for base_filename ($file): $!";
204 my @cols = $sth->fetchrow_array;
210 sub check_for_tracked_column {
211 my ($table,$column,$options) = (shift,shift,shift);
212 my $dbh = db_connect();
213 my $sth = $dbh->prepare("
215 FROM $MIGSCHEMA.tracked_column
216 WHERE staged_table = " . $dbh->quote( $table ) . "
217 AND staged_column = " . $dbh->quote( $column ) . ";"
219 my $rv = $sth->execute()
220 || die "Error checking table (tracked_column) for $table.$column: $!";
221 my @cols = $sth->fetchrow_array;
227 sub status_this_file {
229 my $dbh = db_connect();
230 my $sth = $dbh->prepare("
232 FROM $MIGSCHEMA.tracked_file
233 WHERE base_filename = " . $dbh->quote( $file ) . ";"
235 my $rv = $sth->execute()
236 || die "Error retrieving data from table (tracked_file) for base_filename ($file): $!";
237 my $data = $sth->fetchrow_hashref;
243 sub status_this_column {
244 my ($table,$column) = (shift,shift);
245 my $dbh = db_connect();
246 my $sth = $dbh->prepare("
248 FROM $MIGSCHEMA.tracked_column
249 WHERE staged_table = " . $dbh->quote( $table ) . "
250 AND staged_column = " . $dbh->quote( $column ) . ";"
252 my $rv = $sth->execute()
253 || die "Error checking table (tracked_column) for $table.$column: $!";
254 my $data = $sth->fetchrow_hashref;