seed kmig-convert with emig-convert
authorJason Etheridge <jason@equinoxinitiative.org>
Fri, 10 Apr 2020 19:53:43 +0000 (15:53 -0400)
committerJason Etheridge <jason@equinoxinitiative.org>
Fri, 10 Apr 2020 19:53:43 +0000 (15:53 -0400)
kmig.d/bin/mig-convert [new file with mode: 0755]

diff --git a/kmig.d/bin/mig-convert b/kmig.d/bin/mig-convert
new file mode 100755 (executable)
index 0000000..215cd3f
--- /dev/null
@@ -0,0 +1,131 @@
+#!/usr/bin/perl -w
+###############################################################################
+=pod
+
+=head1 NAME
+
+mig-convert 
+
+Attempts to invoke B<csv2sql> on the .utf8.clean version of the specified
+tracked file, creating either [file].utf8.clean.stage.sql or
+<parent table>_stage.sql depending on whether the file has been linked to a
+parent table within the migration schema or not.
+
+If given no other arguments, the invocation will lool like
+
+=over 5
+
+csv2sql --config scripts/clean.conf --add-x-migrate --schema <MIGSCHEMA> [--parent <PARENT TABLE>] --outfile <[<FILE>.utf8.clean.stage.sql]|[parent_table_stage.sql]> <FILE>.utf8.clean
+
+=back
+
+otherwise, the arguments will be passed through like so
+
+=over 5
+
+csv2sql [other arguments...] --schema <MIGSCHEMA> [--parent <PARENT TABLE>] --outfile <[<FILE>.utf8.clean.stage.sql]|[parent_table_stage.sql]> <FILE>.utf8.clean
+
+=back
+
+=head1 SYNOPSIS
+
+B<mig-convert> <file> [other arguments...]
+
+=cut
+
+###############################################################################
+
+use strict;
+use Switch;
+use Env qw(
+    HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
+    MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
+);
+use Pod::Usage;
+use DBI;
+use Cwd 'abs_path';
+use FindBin;
+my $mig_bin = "$FindBin::Bin/";
+use lib "$FindBin::Bin/";
+use EMig;
+
+pod2usage(-verbose => 2) if ! $ARGV[0] || $ARGV[0] eq '--help';
+
+EMig::die_if_no_env_migschema();
+EMig::die_if_mig_tracking_table_does_not_exist();
+
+my $file = abs_path($ARGV[0]);
+if ($file =~ /^$MIGBASEWORKDIR/) {
+    call_convert_csv(@ARGV);
+} else {
+    print "File falls outside of MIGWORKDIR ($MIGWORKDIR): $file\n";
+}
+
+exit 0;
+
+###############################################################################
+
+sub call_convert_csv {
+    my $file = abs_path(shift);
+    my @args = @_;
+
+    my $stage_sql_filename;
+    my $tracked_file_id = EMig::check_for_tracked_file($file);
+    if ($tracked_file_id) {
+        my $data = EMig::status_this_file($file);
+
+        if (! $data->{'utf8_filename'}) {
+            die "mig-iconv or mig-skip-iconv needed for UTF8 version of file: $file\n";
+        }
+
+        if (! $data->{'clean_filename'}) {
+            die "mig-clean or mig-skip-clean needed for .clean version of file: $file\n";
+        }
+
+        my $clean_file = $data->{'clean_filename'};
+        if (! -e $clean_file) {
+            die "missing file: $clean_file\n";
+        }
+
+        print "converting tracked file: $file\n";
+
+        if (scalar(@args) == 0) {
+            @args = (
+                 '--config'
+                ,'scripts/clean.conf'
+                ,'--add-x-migrate'
+            );
+        }
+        push @args, '--use-no-headers-file';
+        push @args, '--schema';
+        push @args, $MIGSCHEMA;
+        if ($data->{'parent_table'}) {
+            push @args, '--parent';
+            push @args, $data->{'parent_table'};
+            $stage_sql_filename = $data->{'parent_table'} . '.stage.sql';
+        } else {
+            $stage_sql_filename = "$clean_file.stage.sql";
+        }
+        push @args, '--outfile';
+        push @args, $stage_sql_filename;
+
+        print "args: " . join(',',@args) . "\n";
+        system('csv2sql', @args, $clean_file);
+
+        my $dbh = EMig::db_connect();
+        if (! -e $stage_sql_filename) {
+            print "SQL converted file does not exist: $stage_sql_filename\n";
+            $stage_sql_filename = '';
+        }
+
+        my $rv = $dbh->do("
+            UPDATE $MIGSCHEMA.tracked_file
+            SET stage_sql_filename = " . $dbh->quote($stage_sql_filename) . "
+            WHERE base_filename = " . $dbh->quote($file) . "
+            ;
+        ") || die "Error updating table $MIGSCHEMA.tracked_file: $!\n";
+        EMig::db_disconnect($dbh);
+    } else {
+        print "File not currently tracked: $file\n";
+    }
+}