let's not name these differently than the emig.d/ counterparts
[migration-tools.git] / kmig.d / 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
9 You'll need to invoke B<mig-init> prior to using commands like B<mig-remove>
10
11 =head1 SYNOPSIS
12
13 B<mig-remove> <file> [file] [...]
14
15 =cut
16
17 ###############################################################################
18
19 use strict;
20 use Switch;
21 use Env qw(
22     HOME MYSQL_HOST MYSQL_TCP_PORT MYSQL_USER MYSQL_DATABASE MYSQL_PW
23     MIGSCHEMA MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
24 );
25 use Pod::Usage;
26 use DBI;
27 use Cwd 'abs_path';
28 use FindBin;
29 my $mig_bin = "$FindBin::Bin/";
30 use lib "$FindBin::Bin/";
31 use KMig;
32
33 pod2usage(-verbose => 2) if ! $ARGV[0] || $ARGV[0] eq '--help';
34
35 KMig::die_if_no_env_migschema();
36 KMig::die_if_mig_tracking_table_does_not_exist();
37
38 foreach my $arg (@ARGV) {
39     my $file = abs_path($arg);
40     if ($file =~ /^$MIGBASEWORKDIR/) {
41         remove_this_file($file);
42     } else {
43         print "File falls outside of MIGWORKDIR ($MIGWORKDIR): $file\n";
44     }
45 }
46
47 exit 0;
48
49 ###############################################################################
50
51 sub remove_this_file {
52     my $file = shift;
53     my $tracked_file_id = KMig::check_for_tracked_file($file,{'allow_missing'=>1});
54     if ($tracked_file_id) {
55         print "removing tracked file: $file\n";
56         my $dbh = KMig::db_connect();
57         my $rv = $dbh->do("
58             DELETE FROM m_tracked_file WHERE id = $tracked_file_id;
59         ") || die "Error deleting from table m_tracked_file (id = $tracked_file_id): $!\n";
60         KMig::db_disconnect($dbh);
61     } else {
62         print "File not currently tracked: $file\n";
63     }
64 }