2 ###############################################################################
9 Associate the specified file with a parent table within the migration schema.
13 B<mig-link> <file> <parent table>
17 ###############################################################################
22 HOME MYSQL_HOST MYSQL_TCP_PORT MYSQL_USER MYSQL_DATABASE MYSQL_PW
23 MIGSCHEMA 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 KMig::die_if_no_env_migschema();
36 KMig::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 ($table !~ /^m_/) {
54 die "for safety, since kmig 'link'ing really just adds columns to the parent table, this action is restricted to tables prefixed with m_";
57 if (! KMig::check_db_migschema_for_specific_table($table)) {
58 die "table not found in MIGSCHEMA ($MIGSCHEMA): $table\n";
61 my $tracked_file_id = KMig::check_for_tracked_file($file);
62 if ($tracked_file_id) {
63 my $data = KMig::status_this_file($file);
65 print "linking file to parent table: $file -> $table\n";
67 my $dbh = KMig::db_connect();
68 my $sth = $dbh->prepare("
71 WHERE parent_table = " . $dbh->quote($table) . "
72 AND base_filename <> " . $dbh->quote($file) . ";"
74 my $rv = $sth->execute()
75 || die "Error checking $MIGSCHEMA.tracked_file: $!";
76 my @cols = $sth->fetchrow_array;
78 if ($cols[0]) { # found
79 die "table ($table) already linked to a different file: $cols[0]\n";
83 SET parent_table = " . $dbh->quote($table) . "
84 WHERE base_filename = " . $dbh->quote($file) . "
86 ") || die "Error updating table m_tracked_file: $!\n";
87 KMig::db_disconnect($dbh);
89 print "File not currently tracked: $file\n";