summary table and subtables as an output option for quicksheet
[migration-tools.git] / mig-bin / mig-convert
1 #!/usr/bin/perl -w
2 ###############################################################################
3 =pod
4
5 =head1 NAME
6
7 mig-convert 
8
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.
13
14 If given no other arguments, the invocation will lool like
15
16 =over 5
17
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
19
20 =back
21
22 otherwise, the arguments will be passed through like so
23
24 =over 5
25
26 csv2sql [other arguments...] --schema <MIGSCHEMA> [--parent <PARENT TABLE>] --outfile <[<FILE>.utf8.clean.stage.sql]|[parent_table_stage.sql]> <FILE>.utf8.clean
27
28 =back
29
30 =head1 SYNOPSIS
31
32 B<mig-convert> <file> [other arguments...]
33
34 =cut
35
36 ###############################################################################
37
38 use strict;
39 use Switch;
40 use Env qw(
41     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
42     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
43 );
44 use Pod::Usage;
45 use DBI;
46 use Cwd 'abs_path';
47 use FindBin;
48 my $mig_bin = "$FindBin::Bin/";
49 use lib "$FindBin::Bin/";
50 use Mig;
51
52 pod2usage(-verbose => 2) if ! $ARGV[0] || $ARGV[0] eq '--help';
53
54 Mig::die_if_no_env_migschema();
55 Mig::die_if_mig_tracking_table_does_not_exist();
56
57 my $file = abs_path($ARGV[0]);
58 if ($file =~ /^$MIGBASEWORKDIR/) {
59     call_convert_csv(@ARGV);
60 } else {
61     print "File falls outside of MIGWORKDIR ($MIGWORKDIR): $file\n";
62 }
63
64 exit 0;
65
66 ###############################################################################
67
68 sub call_convert_csv {
69     my $file = abs_path(shift);
70     my @args = @_;
71
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);
76
77         if (! $data->{'utf8_filename'}) {
78             die "mig-iconv or mig-skip-iconv needed for UTF8 version of file: $file\n";
79         }
80
81         if (! $data->{'clean_filename'}) {
82             die "mig-clean or mig-skip-clean needed for .clean version of file: $file\n";
83         }
84
85         my $clean_file = $data->{'clean_filename'};
86         if (! -e $clean_file) {
87             die "missing file: $clean_file\n";
88         }
89
90         print "converting tracked file: $file\n";
91
92         if (scalar(@args) == 0) {
93             @args = (
94                  '--config'
95                 ,'scripts/clean.conf'
96                 ,'--add-x-migrate'
97             );
98         }
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';
106         } else {
107             $stage_sql_filename = "$clean_file.stage.sql";
108         }
109         push @args, '--outfile';
110         push @args, $stage_sql_filename;
111
112         print "args: " . join(',',@args) . "\n";
113         system('csv2sql', @args, $clean_file);
114
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 = '';
119         }
120
121         my $rv = $dbh->do("
122             UPDATE $MIGSCHEMA.tracked_file
123             SET stage_sql_filename = " . $dbh->quote($stage_sql_filename) . "
124             WHERE base_filename = " . $dbh->quote($file) . "
125             ;
126         ") || die "Error updating table $MIGSCHEMA.tracked_file: $!\n";
127         Mig::db_disconnect($dbh);
128     } else {
129         print "File not currently tracked: $file\n";
130     }
131 }