seed kmig-remove from mig-remove
authorJason Etheridge <jason@equinoxinitiative.org>
Fri, 10 Apr 2020 16:15:55 +0000 (12:15 -0400)
committerJason Etheridge <jason@equinoxinitiative.org>
Fri, 10 Apr 2020 16:15:55 +0000 (12:15 -0400)
kmig.d/bin/kmig-remove [new file with mode: 0755]

diff --git a/kmig.d/bin/kmig-remove b/kmig.d/bin/kmig-remove
new file mode 100755 (executable)
index 0000000..cf70eda
--- /dev/null
@@ -0,0 +1,67 @@
+#!/usr/bin/perl -w
+###############################################################################
+=pod
+
+=head1 NAME
+
+mig-remove - This will remove the specified files from the mig tracking table
+for the schema pointed to by the MIGSCHEMA environment variable in the
+PostgreSQL database specified by various PG environment variables. <inhales,
+exhales, phew>
+
+You'll need to invoke B<mig-init> prior to using commands like B<mig-remove>
+
+=head1 SYNOPSIS
+
+B<mig-remove> <file> [file] [...]
+
+=cut
+
+###############################################################################
+
+use strict;
+use Switch;
+use Env qw(
+    HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
+    MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
+);
+use Pod::Usage;
+use DBI;
+use Cwd 'abs_path';
+use FindBin;
+my $mig_bin = "$FindBin::Bin/";
+use lib "$FindBin::Bin/";
+use Mig;
+
+pod2usage(-verbose => 2) if ! $ARGV[0] || $ARGV[0] eq '--help';
+
+Mig::die_if_no_env_migschema();
+Mig::die_if_mig_tracking_table_does_not_exist();
+
+foreach my $arg (@ARGV) {
+    my $file = abs_path($arg);
+    if ($file =~ /^$MIGBASEWORKDIR/) {
+        remove_this_file($file);
+    } else {
+        print "File falls outside of MIGWORKDIR ($MIGWORKDIR): $file\n";
+    }
+}
+
+exit 0;
+
+###############################################################################
+
+sub remove_this_file {
+    my $file = shift;
+    my $tracked_file_id = Mig::check_for_tracked_file($file,{'allow_missing'=>1});
+    if ($tracked_file_id) {
+        print "removing tracked file: $file\n";
+        my $dbh = Mig::db_connect();
+        my $rv = $dbh->do("
+            DELETE FROM $MIGSCHEMA.tracked_file WHERE id = $tracked_file_id;
+        ") || die "Error deleting from table $MIGSCHEMA.tracked_file (id = $tracked_file_id): $!\n";
+        Mig::db_disconnect($dbh);
+    } else {
+        print "File not currently tracked: $file\n";
+    }
+}