#!/usr/bin/perl -w ############################################################################### =pod =head1 NAME mig-convert Attempts to invoke B on the .utf8.clean version of the specified tracked file, creating either [file].utf8.clean.stage.sql or _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 [--parent ] --outfile <[.utf8.clean.stage.sql]|[parent_table_stage.sql]> .utf8.clean =back otherwise, the arguments will be passed through like so =over 5 csv2sql [other arguments...] --schema [--parent ] --outfile <[.utf8.clean.stage.sql]|[parent_table_stage.sql]> .utf8.clean =back =head1 SYNOPSIS B [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 Mig; pod2usage(-verbose => 2) if ! $ARGV[0] || $ARGV[0] eq '--help'; Mig::die_if_no_env_migschema(); Mig::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 = Mig::check_for_tracked_file($file); if ($tracked_file_id) { my $data = Mig::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 = Mig::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"; Mig::db_disconnect($dbh); } else { print "File not currently tracked: $file\n"; } }