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