fixing doc
[migration-tools.git] / mig-bin / mig-remove
1 #!/usr/bin/perl -w
2 ###############################################################################
3 =pod
4
5 =head1 NAME
6
7 mig-remove - This will remove the specified files from the mig tracking table
8 for the schema pointed to by the MIGSCHEMA environment variable in the
9 PostgreSQL database specified by various PG environment variables. <inhales,
10 exhales, phew>
11
12 You'll need to invoke B<mig-init> prior to using commands like B<mig-remove>
13
14 =head1 SYNOPSIS
15
16 B<mig-remove> <file> [file] [...]
17
18 =cut
19
20 ###############################################################################
21
22 use strict;
23 use Switch;
24 use Env qw(
25     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
26     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
27 );
28 use Pod::Usage;
29 use DBI;
30 use Cwd 'abs_path';
31 use FindBin;
32 my $mig_bin = "$FindBin::Bin/";
33 use lib "$FindBin::Bin/";
34 use Mig;
35
36 pod2usage(-verbose => 2) if ! $ARGV[0] || $ARGV[0] eq '--help';
37
38 Mig::die_if_no_env_migschema();
39 Mig::die_if_mig_tracking_table_does_not_exist();
40
41 foreach my $arg (@ARGV) {
42     my $file = abs_path($arg);
43     if ($file =~ /^$MIGBASEWORKDIR/) {
44         remove_this_file($file);
45     } else {
46         print "File falls outside of MIGWORKDIR ($MIGWORKDIR): $file\n";
47     }
48 }
49
50 exit 0;
51
52 ###############################################################################
53
54 sub remove_this_file {
55     my $file = shift;
56     my $tracked_file_id = Mig::check_for_tracked_file($file,{'allow_missing'=>1});
57     if ($tracked_file_id) {
58         print "removing tracked file: $file\n";
59         my $dbh = Mig::db_connect();
60         my $rv = $dbh->do("
61             DELETE FROM $MIGSCHEMA.tracked_file WHERE id = $tracked_file_id;
62         ") || die "Error deleting from table $MIGSCHEMA.tracked_file (id = $tracked_file_id): $!\n";
63         Mig::db_disconnect($dbh);
64     } else {
65         print "File not currently tracked: $file\n";
66     }
67 }