Clean up 'bucardo add dbgroup' functionality. Allow --force argument to work.
authorGreg Sabino Mullane <greg@endpoint.com>
Fri, 10 Jun 2011 15:52:11 +0000 (11:52 -0400)
committerGreg Sabino Mullane <greg@endpoint.com>
Fri, 10 Jun 2011 15:52:11 +0000 (11:52 -0400)
bucardo
t/02-bctl-dbg.t

diff --git a/bucardo b/bucardo
index 1dfdcab344592cfed54995bc093dba1a41c845de..85e7dc0fee8983a04e7bebfd54b9e28f47e71921 100755 (executable)
--- a/bucardo
+++ b/bucardo
@@ -1370,7 +1370,6 @@ sub add_dbgroup {
     ## 2+ Databases to add to this group, with optional role information attached
     ## Returns: undef
     ## Example: bucardo add dbgroup nycservers nyc1:source nyc2:source lax1
-    ## TODO: Wildcards?
 
     ## Grab our generic usage message
     my $usage = usage('add_dbgroup');
@@ -1389,8 +1388,9 @@ sub add_dbgroup {
         $QUIET or print qq{Created database group "$name"\n};
     }
 
-    ## Everything else is databases to add to this group
+    ## Add all these databases to the group
     for my $fulldb (@nouns) {
+
         ## Figure out the optional role
         my ($db,$role) = extract_name_and_role($fulldb);
 
@@ -1419,7 +1419,6 @@ sub remove_dbgroup {
     ## 1+ Name of a database group
     ## Returns: undef
     ## Example: bucardo remove dbgroup sales
-    ## TODO: --force argument needed
 
     ## Grab our generic usage message
     my $usage = usage('remove_dbgroup');
@@ -1447,12 +1446,23 @@ sub remove_dbgroup {
             $sth->execute($name);
         };
         if ($@) {
-            if ($@ =~ /"sync_targetgroup_fk"/) {
-                die qq{Cannot remove database group "$name": it is being used by one or more syncs\n};
+            if ($@ =~ /"sync_dbs_fk"/) {
+                if ($bcargs->{force}) {
+                    $QUIET or warn qq{Dropping all syncs that reference the dbgroup "$name"\n};
+                    $dbh->rollback();
+                    $dbh->do('DELETE FROM bucardo.sync WHERE dbs = ' . $dbh->quote($name));
+                    eval {
+                        $sth->execute($name);
+                    };
+                    goto NEND if ! $@;
+                }
+                else {
+                    die qq{Cannot remove database group "$name": it is being used by one or more syncs\n};
+                }
             }
             die qq{Could not delete database group "$name"\n$@\n};
         }
-
+          NEND:
         $QUIET or print qq{Removed database group "$name"\n};
     }
 
index 2de8346c2ae9b4687bf02c4d441beeb95fdd37e8..1f6b18c574b8a448b21b9fdb0f9ac7bd04a6c259 100644 (file)
@@ -10,7 +10,7 @@ use warnings;
 use Data::Dumper;
 use lib 't','.';
 use DBD::Pg;
-use Test::More tests => 22;
+use Test::More tests => 24;
 
 use vars qw/$t $res $command $dbhX $dbhA $dbhB/;
 
@@ -97,6 +97,18 @@ $bct->ctl('bucardo add dbgroup foobar2');
 $res = $bct->ctl('bucardo remove dbgroup foobar1 foobar2');
 like ($res, qr/Removed database group "foobar1".*Removed database group "foobar2"/s, $t);
 
+$t = 'Removal of dbgroup fails if used in a sync';
+$bct->ctl('bucardo add herd therd bucardo_test1');
+$bct->ctl('bucardo add dbgroup foobar3 A:source B');
+$bct->ctl('bucardo add sync mysync herd=therd dbs=foobar3');
+$res = $bct->ctl('bucardo remove dbgroup foobar3');
+chomp $res;
+is ($res, q/Cannot remove database group "foobar3": it is being used by one or more syncs/, $t);
+
+$t = 'Removal of dbgroup works if used in a sync and the --force argument used';
+$res = $bct->ctl('bucardo remove dbgroup foobar3 --force');
+like ($res, qr/Dropping all syncs that reference the dbgroup "foobar3".*Removed database group "foobar3"/s, $t);
+
 ## Update
 
 $bct->ctl('bucardo add dbgroup foobar');