mig env clone
[migration-tools.git] / mig-bin / mig-add
1 #!/usr/bin/perl -w
2 ###############################################################################
3 =pod
4
5 =head1 NAME
6
7 mig-add - This will add the specified files to the mig tracking table for the
8 schema pointed to by the MIGSCHEMA environment variable in the PostgreSQL
9 database specified by various PG environment variables. <inhales, exhales>
10
11 --headers (the default) and --no-headers are repeatable, and indicate whether
12 subsequent files have headers or not
13
14 --headers-file specifies a text file <hfile> defining the column headers for
15 the next added <file>, which should contain one line per header
16
17 --headers-file will automatically invoke --no-headers
18
19 You'll need to invoke B<mig-init> prior to using commands like B<mig-add>
20
21 =head1 SYNOPSIS
22
23 B<mig-add> [--no-headers|--headers|--headers-file <hfile>] <file> [file|--no-headers|--headers|--headers-file <hfile>] [...]
24
25 =cut
26
27 ###############################################################################
28
29 use strict;
30 use Switch;
31 use Env qw(
32     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
33     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
34 );
35 use Pod::Usage;
36 use DBI;
37 use Cwd 'abs_path';
38 use FindBin;
39 my $mig_bin = "$FindBin::Bin/";
40 use lib "$FindBin::Bin/";
41 use Mig;
42
43 pod2usage(-verbose => 2) if ! $ARGV[0] || $ARGV[0] eq '--help';
44
45 Mig::die_if_no_env_migschema();
46 Mig::die_if_mig_tracking_table_does_not_exist();
47
48 my $has_headers = 1;
49 my $headers_file;
50 my $next_arg_is_headers_file = 0;
51
52 foreach my $arg (@ARGV) {
53     if ($next_arg_is_headers_file) {
54         $next_arg_is_headers_file = 0;
55         $headers_file = abs_path($arg);
56         next;
57     }
58     if ($arg eq '--headers') {
59         $has_headers = 1;
60         next;
61     }
62     if ($arg eq '--no-headers') {
63         $has_headers = 0;
64         next;
65     }
66     if ($arg eq '--headers-file') {
67         $next_arg_is_headers_file = 1;
68         $has_headers = 0;
69         next;
70     }
71     my $file = abs_path($arg);
72     if ($file =~ /^$MIGBASEWORKDIR/) {
73         if (-e $file) {
74             if (-f $file) {
75                 add_this_file($file,$has_headers,$headers_file);
76                 $headers_file = ''; # clear after applying to just one file
77             } else {
78                 print "Not a real file: $file\n";
79             }
80         } else {
81             print "Could not find file: $file\n";
82         }
83     } else {
84         print "File falls outside of MIGWORKDIR ($MIGWORKDIR): $file\n";
85     }
86 }
87
88 exit 0;
89
90 ###############################################################################
91
92 sub add_this_file {
93     my $file = shift;
94     my $headers = shift;
95     my $headers_file = shift;
96     if ($headers_file) {
97         if (! (-e $headers_file && -f $headers_file)) {
98             print "Could not find headers file $headers_file, skipping $file\n";
99             return;
100         }
101     }
102     if (Mig::check_for_tracked_file($file)) {
103         print "File already tracked: $file\n";
104     } else {
105         print 'Adding (';
106         if ($headers_file) {
107             print "with headers file = $headers_file";
108         } else {
109             print ($headers ? '   with headers' : 'without headers');
110         }
111         print '): ' . "$file\n";
112         my $dbh = Mig::db_connect();
113         my $rv = $dbh->do("
114             INSERT INTO $MIGSCHEMA.tracked_file (
115                  base_filename
116                 ,has_headers
117                 ,headers_file
118             ) VALUES (
119                  " . $dbh->quote($file) . "
120                 ," . $dbh->quote($headers) . "
121                 ," . $dbh->quote($headers_file) . "
122             );
123         ") || die "Error inserting into table $MIGSCHEMA.tracked_file: $!\n";
124         Mig::db_disconnect($dbh);
125     }
126 }
127