fix Experimental unshift on scalar is now forbidden with newer Perl
[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 } else {
47     $clean_file = $file;
48 }
49 if ($clean_file && ! $clean_file =~ /^$MIGBASEWORKDIR/) {
50     die "File falls outside of MIGWORKDIR ($MIGWORKDIR): $clean_file\n";
51 }
52
53 if ($file =~ /^$MIGBASEWORKDIR/) {
54     skip_clean($file,$clean_file);
55 } else {
56     die "File falls outside of MIGWORKDIR ($MIGWORKDIR): $file\n";
57 }
58
59 exit 0;
60
61 ###############################################################################
62
63 sub skip_clean {
64     my $file = shift;
65     my $clean_file = shift;
66
67     my $tracked_file_id = Mig::check_for_tracked_file($file);
68     if ($tracked_file_id) {
69         my $data = Mig::status_this_file($file);
70
71         if (! $data->{'utf8_filename'}) {
72             die "mig-iconv or mig-skip-iconv needed for UTF8 version of file: $file\n";
73         }
74
75         my $utf8_file = $data->{'utf8_filename'};
76         if (! -e $utf8_file) {
77             die "missing file: $utf8_file\n";
78         }
79
80         print "skipping cleaning of tracked file: $file\n";
81
82         my $dbh = Mig::db_connect();
83         if (! $clean_file) {
84             $clean_file = $utf8_file . '.clean';
85         }
86         if (! -e $clean_file) {
87             die "clean file does not exist: $clean_file\n";
88         }
89
90         my $rv = $dbh->do("
91             UPDATE $MIGSCHEMA.tracked_file
92             SET clean_filename = " . $dbh->quote($clean_file) . "
93             WHERE base_filename = " . $dbh->quote($file) . "
94             ;
95         ") || die "Error inserting into table $MIGSCHEMA.tracked_file: $!\n";
96         Mig::db_disconnect($dbh);
97     } else {
98         die "File not currently tracked: $file\n";
99     }
100 }