Allow 'bucardo_ctl update herd foo remove <table(s)>'
authorGreg Sabino Mullane <greg@endpoint.com>
Mon, 16 Aug 2010 19:46:27 +0000 (15:46 -0400)
committerGreg Sabino Mullane <greg@endpoint.com>
Mon, 16 Aug 2010 19:46:27 +0000 (15:46 -0400)
 and 'bucardo_ctl update herd foo add <table(s)>'

Changes
bucardo_ctl

diff --git a/Changes b/Changes
index b2ba2174a5119b1b6a6b5169893389eec848f6aa..4dc0f605e03b9a3caa42b6279520a80a08c6fc21 100644 (file)
--- a/Changes
+++ b/Changes
@@ -70,6 +70,9 @@ Bucardo version 4.5.0, released ??, 2010
 
   - Add create_child_q_table() function. [GSM]
 
+  - Allow 'bucardo_ctl update herd foo remove <table(s)>'
+    and 'bucardo_ctl update herd foo add <table(s)>' [GSM]
+
   - Better formatting of bucardo.reason.log, and log startup failures. [GSM]
 
   - Log the current configuration settings on startup. [GSM]
index f0052f983de4f2fbed6478df8f6244e124be5502..fd333d203e74415d2fe161c7455e210fac138e87 100755 (executable)
@@ -1063,6 +1063,103 @@ sub update {
         exit 0;
     }
 
+    if ('herd' eq $item and ($nouns[0] eq 'add' or $nouns[0] eq 'remove')) {
+        my $v = shift @nouns;
+        die "Can only $v to one herd at a time\n" if @$info > 1;
+        $info = $info->[0];
+        my $miniusage = "Usage: update herd $name $v <table(s)>\n";
+        @nouns or die $miniusage;
+
+        ## Grab all tables in this herd
+        $SQL = 'SELECT g.id, g.schemaname, g.tablename FROM bucardo.goat g '
+            . 'JOIN bucardo.herdmap h ON (h.goat = g.id) WHERE h.herd = ?';
+        $sth = $dbh->prepare($SQL);
+        $sth->execute($name);
+        my $tablist = $sth->fetchall_hashref('id');
+
+        my %found;
+
+        if ($v eq 'remove') {
+
+            for my $tab (@nouns) {
+                my $wild = ($tab =~ s/\*//) ? 1 : 0;
+                for my $id (keys %$tablist) {
+                    my ($sname,$tname) = ($tablist->{$id}{schemaname}, $tablist->{$id}{tablename});
+                    if ($wild) {
+                        if ($tname =~ /$tab/) {
+                            $found{$id} = [$sname,$tname];
+                        }
+                    }
+                    elsif ($tab =~ /(.+)\.(.+)/) {
+                        my ($s,$t) = ($1,$2);
+                        if ($s eq $sname and $t eq $tname) {
+                            $found{$id} = [$sname,$tname];
+                        }
+                    }
+                    elsif ($tname eq $tab) {
+                        $found{$id} = [$sname,$tname];
+                    }
+                }
+            }
+
+            if (keys %found) {
+                $SQL = 'DELETE FROM bucardo.herdmap WHERE herd = ? AND goat = ?';
+                $sth = $dbh->prepare($SQL);
+                for my $id (sort keys %found) {
+                    my ($sname,$tname) = @{$found{$id}};
+                    $count = $sth->execute($name,$id);
+                    if ($count >= 1) {
+                        print "  Removed from herd $name: $sname.$tname\n";
+                    }
+                    else {
+                        print "Could not remove from herd $name: $sname.$tname\n";
+                    }
+                }
+                $dbh->commit();
+            }
+
+            exit 0;
+
+        } ## end 'remove'
+
+        ## Must be 'add'
+        my $flatsql = 'SELECT id,schemaname,tablename FROM bucardo.goat WHERE tablename = ?';
+        my $wildsql = 'SELECT id,schemaname,tablename FROM bucardo.goat WHERE tablename ~ ?';
+
+        for my $tab (@nouns) {
+            my $wild = ($tab =~ s/\*//) ? 1 : 0;
+
+            $sth = $dbh->prepare($wild ? $wildsql : $flatsql);
+            $count = $sth->execute($tab);
+            next if $count < 1;
+
+            for my $row (@{$sth->fetchall_arrayref()}) {
+                $found{$row->[0]} = [$row->[1],$row->[2]];
+            }
+        }
+
+        if (keys %found) {
+
+            $SQL = 'INSERT INTO bucardo.herdmap(herd,goat) VALUES (?,?)';
+            $sth = $dbh->prepare($SQL);
+            for my $id (sort keys %found) {
+                next if exists $tablist->{$id};
+                $tablist->{$id}++;
+                my ($sname,$tname) = @{$found{$id}};
+                $count = $sth->execute($name,$id);
+                if ($count >= 1) {
+                    print "  Added to herd $name: $sname.$tname\n";
+                }
+                else {
+                    print "Could not add to herd $name: $sname.$tname\n";
+                }
+            }
+            $dbh->commit();
+        }
+
+        exit 0;
+    }
+
     ## Process each change in turn
     ## Can be code, sync, dbgroup, table, schema, herd, or db
     my %change;