2 ###############################################################################
9 Attempts to invoke B<csv2sql> on the .utf8.clean version of the specified
10 tracked file, creating either [file].utf8.clean.stage.sql or
11 <parent table>_stage.sql depending on whether the file has been linked to a
12 parent table within the migration schema or not.
14 If given no other arguments, the invocation will lool like
18 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
22 otherwise, the arguments will be passed through like so
26 csv2sql [other arguments...] --schema <MIGSCHEMA> [--parent <PARENT TABLE>] --outfile <[<FILE>.utf8.clean.stage.sql]|[parent_table_stage.sql]> <FILE>.utf8.clean
32 B<mig-convert> <file> [other arguments...]
36 ###############################################################################
41 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
42 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
48 my $mig_bin = "$FindBin::Bin/";
49 use lib "$FindBin::Bin/";
52 pod2usage(-verbose => 2) if ! $ARGV[0] || $ARGV[0] eq '--help';
54 Mig::die_if_no_env_migschema();
55 Mig::die_if_mig_tracking_table_does_not_exist();
57 my $file = abs_path($ARGV[0]);
58 if ($file =~ /^$MIGBASEWORKDIR/) {
59 call_convert_csv(@ARGV);
61 print "File falls outside of MIGWORKDIR ($MIGWORKDIR): $file\n";
66 ###############################################################################
68 sub call_convert_csv {
69 my $file = abs_path(shift);
72 my $stage_sql_filename;
73 my $tracked_file_id = Mig::check_for_tracked_file($file);
74 if ($tracked_file_id) {
75 my $data = Mig::status_this_file($file);
77 if (! $data->{'utf8_filename'}) {
78 die "mig-iconv or mig-skip-iconv needed for UTF8 version of file: $file\n";
81 if (! $data->{'clean_filename'}) {
82 die "mig-clean or mig-skip-clean needed for .clean version of file: $file\n";
85 my $clean_file = $data->{'clean_filename'};
86 if (! -e $clean_file) {
87 die "missing file: $clean_file\n";
90 print "converting tracked file: $file\n";
92 if (scalar(@args) == 0) {
99 push @args, '--use-no-headers-file';
100 push @args, '--schema';
101 push @args, $MIGSCHEMA;
102 if ($data->{'parent_table'}) {
103 push @args, '--parent';
104 push @args, $data->{'parent_table'};
105 $stage_sql_filename = $data->{'parent_table'} . '.stage.sql';
107 $stage_sql_filename = "$clean_file.stage.sql";
109 push @args, '--outfile';
110 push @args, $stage_sql_filename;
112 print "args: " . join(',',@args) . "\n";
113 system('csv2sql', @args, $clean_file);
115 my $dbh = Mig::db_connect();
116 if (! -e $stage_sql_filename) {
117 print "SQL converted file does not exist: $stage_sql_filename\n";
118 $stage_sql_filename = '';
122 UPDATE $MIGSCHEMA.tracked_file
123 SET stage_sql_filename = " . $dbh->quote($stage_sql_filename) . "
124 WHERE base_filename = " . $dbh->quote($file) . "
126 ") || die "Error updating table $MIGSCHEMA.tracked_file: $!\n";
127 Mig::db_disconnect($dbh);
129 print "File not currently tracked: $file\n";