2 ###############################################################################
9 Load the SQL-converted version of the specified file into the migration schema.
11 Extra arguments are passed to the underlying call to psql
13 If the tracked file was previously staged with a different table, drop that
19 B<mig-stage> <file> [other arguments...]
23 ###############################################################################
28 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
29 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
35 my $mig_bin = "$FindBin::Bin/";
36 use lib "$FindBin::Bin/";
39 pod2usage(-verbose => 2) if ! $ARGV[0] || $ARGV[0] eq '--help';
41 Mig::die_if_no_env_migschema();
42 Mig::die_if_mig_tracking_table_does_not_exist();
44 my $file = abs_path($ARGV[0]);
45 if ($file =~ /^$MIGBASEWORKDIR/) {
48 print "File falls outside of MIGWORKDIR ($MIGWORKDIR): $file\n";
53 ###############################################################################
56 my $file = abs_path(shift);
59 my $tracked_file_id = Mig::check_for_tracked_file($file);
60 if ($tracked_file_id) {
61 my $data = Mig::status_this_file($file);
63 if (! $data->{'utf8_filename'}) {
64 die "mig-iconv or mig-skip-iconv needed for UTF8 version of file: $file\n";
67 if (! $data->{'clean_filename'}) {
68 die "mig-clean or mig-skip-clean needed for .clean version of file: $file\n";
71 if (! $data->{'stage_sql_filename'}) {
72 die "mig-convert needed for .stage.sql version of file: $file\n";
75 my $stage_sql_filename = $data->{'stage_sql_filename'};
76 if (! -e $stage_sql_filename) {
77 die "missing file: $stage_sql_filename\n";
80 my $schema_table = `grep 'CREATE UNLOGGED TABLE' $stage_sql_filename | cut -f4 -d\\ | head -1`;
82 my ($schema,$table) = split /\./, $schema_table;
84 if (defined $data->{'staged_table'} && $data->{'staged_table'} ne $table) {
85 my $dbh2 = Mig::db_connect();
86 print "dropping previously staged table: $MIGSCHEMA.$data->{staged_table}\n";
88 DROP TABLE $MIGSCHEMA.$data->{staged_table};
89 ") || die "Error dropping table $data->{staged_table}: $!\n";
90 print "changing references to old tables\n";
92 UPDATE $MIGSCHEMA.tracked_column
93 SET staged_table = " . $dbh2->quote($table) . "
94 WHERE staged_table = " . $dbh2->quote($data->{staged_table}) . "
95 ") || die "Error changing references to $data->{staged_table}: $!\n";
97 UPDATE $MIGSCHEMA.tracked_column
98 SET target_table = " . $dbh2->quote($table) . "
99 WHERE target_table = " . $dbh2->quote($data->{staged_table}) . "
100 ") || die "Error changing references to $data->{staged_table}: $!\n";
101 Mig::db_disconnect($dbh2);
104 print "running staging SQL: $stage_sql_filename\n";
106 system('psql', @args, '-f', $stage_sql_filename);
108 if ($schema ne $MIGSCHEMA) {
109 die "Schema mismatch: env => $MIGSCHEMA sql => $schema\n";
111 if (! Mig::check_db_migschema_for_specific_table($table)) {
112 die "Missing staged table: $schema_table\n";
114 print "table staged: $schema_table\n";
117 my $dbh = Mig::db_connect();
119 UPDATE $MIGSCHEMA.tracked_file
120 SET staged_table = " . $dbh->quote($table) . "
121 WHERE base_filename = " . $dbh->quote($file) . "
123 ") || die "Error updating table $MIGSCHEMA.tracked_file: $!\n";
124 Mig::db_disconnect($dbh);
126 print "File not currently tracked: $file\n";