2 ###############################################################################
9 Associate the specified file with a parent table within the migration schema.
13 B<mig-link> <file> <parent table>
17 ###############################################################################
22 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
23 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
29 my $mig_bin = "$FindBin::Bin/";
30 use lib "$FindBin::Bin/";
33 pod2usage(-verbose => 2) if ! $ARGV[0] || $ARGV[0] eq '--help';
35 Mig::die_if_no_env_migschema();
36 Mig::die_if_mig_tracking_table_does_not_exist();
38 my $file = abs_path($ARGV[0]);
39 if ($file =~ /^$MIGBASEWORKDIR/) {
42 print "File falls outside of MIGWORKDIR ($MIGWORKDIR): $file\n";
47 ###############################################################################
50 my $file = abs_path(shift);
53 if (! Mig::check_db_migschema_for_specific_table($table)) {
54 die "table not found in MIGSCHEMA ($MIGSCHEMA): $table\n";
57 my $tracked_file_id = Mig::check_for_tracked_file($file);
58 if ($tracked_file_id) {
59 my $data = Mig::status_this_file($file);
61 print "linking file to parent table: $file -> $table\n";
63 my $dbh = Mig::db_connect();
64 my $sth = $dbh->prepare("
66 FROM $MIGSCHEMA.tracked_file
67 WHERE parent_table = " . $dbh->quote($table) . "
68 AND base_filename <> " . $dbh->quote($file) . ";"
70 my $rv = $sth->execute()
71 || die "Error checking $MIGSCHEMA.tracked_file: $!";
72 my @cols = $sth->fetchrow_array;
74 if ($cols[0]) { # found
75 die "table ($table) already linked to a different file: $cols[0]\n";
78 UPDATE $MIGSCHEMA.tracked_file
79 SET parent_table = " . $dbh->quote($table) . "
80 WHERE base_filename = " . $dbh->quote($file) . "
82 ") || die "Error updating table $MIGSCHEMA.tracked_file: $!\n";
83 Mig::db_disconnect($dbh);
85 print "File not currently tracked: $file\n";