adding some basic copy tag and serial tables to the mig init function
[migration-tools.git] / mig-bin / mig-skip-clean
1 #!/usr/bin/perl -w
2 ###############################################################################
3 =pod
4
5 =head1 NAME
6
7 mig-skip-clean 
8
9 Allows you to either use an existing file named <file>.utf8.clean or a
10 named [cleaned file] as if it were the one created by mig-clean
11
12 Note that the clean file, however specified, should contain headers. The
13 remaining tools in the chain will expect this.
14
15 =head1 SYNOPSIS
16
17 B<mig-skip-clean> <file> [cleaned file]
18
19 =cut
20
21 ###############################################################################
22
23 use strict;
24 use Switch;
25 use Env qw(
26     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
27     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
28 );
29 use Pod::Usage;
30 use DBI;
31 use Cwd 'abs_path';
32 use FindBin;
33 my $mig_bin = "$FindBin::Bin/";
34 use lib "$FindBin::Bin/";
35 use Mig;
36
37 pod2usage(-verbose => 2) if ! ($ARGV[0]||$ARGV[1]) || $ARGV[0] eq '--help';
38
39 Mig::die_if_no_env_migschema();
40 Mig::die_if_mig_tracking_table_does_not_exist();
41
42 my $file = abs_path($ARGV[0]);
43 my $clean_file;
44 if ($ARGV[1]) {
45     $clean_file = abs_path($ARGV[1]);
46 }
47 if ($clean_file && ! $clean_file =~ /^$MIGBASEWORKDIR/) {
48     die "File falls outside of MIGWORKDIR ($MIGWORKDIR): $clean_file\n";
49 }
50
51 if ($file =~ /^$MIGBASEWORKDIR/) {
52     skip_clean($file,$clean_file);
53 } else {
54     die "File falls outside of MIGWORKDIR ($MIGWORKDIR): $file\n";
55 }
56
57 exit 0;
58
59 ###############################################################################
60
61 sub skip_clean {
62     my $file = shift;
63     my $clean_file = shift;
64
65     my $tracked_file_id = Mig::check_for_tracked_file($file);
66     if ($tracked_file_id) {
67         my $data = Mig::status_this_file($file);
68
69         if (! $data->{'utf8_filename'}) {
70             die "mig-iconv or mig-skip-iconv needed for UTF8 version of file: $file\n";
71         }
72
73         my $utf8_file = $data->{'utf8_filename'};
74         if (! -e $utf8_file) {
75             die "missing file: $utf8_file\n";
76         }
77
78         print "skipping cleaning of tracked file: $file\n";
79
80         my $dbh = Mig::db_connect();
81         if (! $clean_file) {
82             $clean_file = $utf8_file . '.clean';
83         }
84         if (! -e $clean_file) {
85             die "clean file does not exist: $clean_file\n";
86         }
87
88         my $rv = $dbh->do("
89             UPDATE $MIGSCHEMA.tracked_file
90             SET clean_filename = " . $dbh->quote($clean_file) . "
91             WHERE base_filename = " . $dbh->quote($file) . "
92             ;
93         ") || die "Error inserting into table $MIGSCHEMA.tracked_file: $!\n";
94         Mig::db_disconnect($dbh);
95     } else {
96         die "File not currently tracked: $file\n";
97     }
98 }