From e85f9e6b4f8c36da92f54900b25f412c22f6c4e2 Mon Sep 17 00:00:00 2001 From: Greg Sabino Mullane Date: Fri, 10 Jun 2011 10:09:23 -0400 Subject: [PATCH] Enforce the 'bucardo add db' uses the next word as the internal name, to be consistent with the other nouns. --- bucardo | 73 +++++++++++++++++++++++--------------------------- bucardo.schema | 4 +-- t/02-bctl-db.t | 26 +++++++++--------- 3 files changed, 48 insertions(+), 55 deletions(-) diff --git a/bucardo b/bucardo index fa44d999d..1dfdcab34 100755 --- a/bucardo +++ b/bucardo @@ -172,7 +172,7 @@ if ($bcargs->{version}) { $bcargs->{vv} and $bcargs->{verbose} = 2; my $VERBOSE = delete $bcargs->{verbose}; -my $DEBUG = delete $bcargs->{debug} || 0; +my $DEBUG = delete $bcargs->{debug} || $ENV{BUCARDO_DEBUG} || 0; my $DAYSBACK = delete $bcargs->{daysback} || $DEFAULT_DAYSBACK; my $COMPRESS = delete $bcargs->{compress}; my $SHOWDAYS = delete $bcargs->{showdays} || 1; @@ -719,15 +719,15 @@ sub add_database { ## Add one or more databases. Inserts to the bucardo.db table ## By default, we do a test connection as well (turn off with the --force argument) ## Arguments: two or more - ## 1. The name of the database we connect to + ## 1. The internal name Bucardo uses to refer to this database ## 2+ name=value parameters, dash-dash arguments ## Returns: undef - ## Example: bucardo add db qpf name=nyc1 host=nyc1.example.com group=sales + ## Example: bucardo add db nyc1 dbname=nyc1 host=nyc1.example.com group=sales ## Grab our generic usage message my $usage = usage('add_database'); - ## The first word is the name of the database we connect to (bucardo.db.dbname) + ## The first word is the internal name (bucardo.db.name) my $item_name = shift @nouns || ''; ## No name is a problem @@ -738,9 +738,9 @@ sub add_database { ## Inputs and aliases, database column name, flags, default my $validcols = qq{ - name name 0 $item_name type dbtype 0 postgres - dbname dbname 0 $item_name + name name 0 $item_name + db|dbname dbname 0 $item_name user|dbuser|pguser dbuser 0 bucardo host|dbhost|pghost dbhost 0 ENV:PGHOSTADDR|PGHOST port|dbport|pgport dbport numeric ENV:PGPORT @@ -762,15 +762,14 @@ sub add_database { my ($dbcols,$cols,$phs,$vals,$extra) = process_simple_args({cols => $validcols, list => \@nouns, usage => $usage}); - my $newname = $dbcols->{name} or die qq{Must provide a database name\n}; - ## Cannot add if already there - if (exists $DB->{$newname}) { - print qq{Cannot add database: the name "$newname" already exists\n}; + if (exists $DB->{$item_name}) { + print qq{Cannot add database: the name "$item_name" already exists\n}; exit 1; } ## Clean up and standardize the names + $vals->{dbtype} = lc $vals->{dbtype}; $vals->{dbtype} =~ s/postgres.*/postgres/io; $vals->{dbtype} =~ s/mongo.*/mongo/io; $vals->{dbtype} =~ s/mysql.*/mysql/io; @@ -793,29 +792,21 @@ sub add_database { ## Store certain messages so we can output them in a desired order my $finalmsg = ''; + ## May want to do a test connection to the database TESTCONN: { ## Nothing else to do for flatfiles - if ($vals->{dbtype} eq 'flatfile') { - last TESTCONN; - } - elsif ($vals->{dbtype} eq 'postgres') { - ## All is good, proceed below - } - elsif ($vals->{dbtype} eq 'mongo') { - ## Proceed - } - elsif ($vals->{dbtype} eq 'mysql') { - ## Proceed - } - else { + last TESTCONN if 'flatfile' eq $vals->{dbtype}; + + ## Must have a valid type + if ($vals->{dbtype} !~ /^(?:postgres|mongo|mysql)$/) { die qq{Unknown database type: $vals->{dbtype}\n}; } ## Before going any further, do a test connection $SQL = 'SELECT bucardo.db_getconn(?)'; $sth = $dbh->prepare($SQL); - $count = $sth->execute($newname); + $count = $sth->execute($item_name); my $dbconn = $sth->fetchall_arrayref()->[0][0]; if ('mongo' eq $vals->{dbtype}) { @@ -885,7 +876,7 @@ sub add_database { $testdbh->do(qq{CREATE USER $realuser SUPERUSER ENCRYPTED PASSWORD '$encpass'}); $testdbh->commit(); my $extra = length $pass ? '' : " with password $newpass"; - warn "Created superuser '$realuser'$extra\n"; + warn "Created superuser '$realuser'$extra\n"; }; if ($@) { warn "Unable to create superuser $realuser: $@\n"; @@ -926,23 +917,23 @@ sub add_database { } ## end mysql - ## If we got a group, process that as well - if (exists $extra->{dbgroup}) { - my $gname = $extra->{dbgroup}; - ## We need to store this away as the function below changes the global hash - my $isnew = exists $DBGROUP->{$gname}; + } ## end of TESTCONN - my ($newgroup, $newrole) = add_db_to_group($newname, $gname); - if (! $isnew) { - $finalmsg .= qq{Created database group "$newgroup"\n}; - } - $finalmsg .= qq{Added database "$newname" to group "$newgroup" as $newrole\n}; - } + ## If we got a group, process that as well + if (exists $extra->{dbgroup}) { + my $gname = $extra->{dbgroup}; + ## We need to store this away as the function below changes the global hash + my $isnew = exists $DBGROUP->{$gname}; - } ## end of TESTCONN + my ($newgroup, $newrole) = add_db_to_group($item_name, $gname); + if (! $isnew) { + $finalmsg .= qq{Created database group "$newgroup"\n}; + } + $finalmsg .= qq{Added database "$item_name" to group "$newgroup" as $newrole\n}; + } ## Adjust the db name so add_all_* can use it - $bcargs->{db} = $newname; + $bcargs->{db} = $item_name; ## Add in all tables for this database $finalmsg .= add_all_tables() if grep /addalltab/i, @nouns; @@ -951,7 +942,7 @@ sub add_database { $finalmsg .= add_all_sequences() if grep /addallseq/i, @nouns; if (!$QUIET) { - print qq{Added database "$newname"\n}; + print qq{Added database "$item_name"\n}; $finalmsg and print $finalmsg; } @@ -983,7 +974,8 @@ sub remove_database { ## Make sure all named databases exist for my $name (@nouns) { if (! exists $DB->{$name}) { - die qq{No such database: $name\n}; + my $foo = Dumper $DB; + die qq{No such database: $name ($foo)\n}; } } @@ -991,6 +983,7 @@ sub remove_database { $SQL = 'DELETE FROM bucardo.db WHERE name = ?'; $sth = $dbh->prepare($SQL); + ## Loop through and attempt to delete each given database for my $name (@nouns) { ## Wrap in an eval so we can handle known exceptions eval { diff --git a/bucardo.schema b/bucardo.schema index 256b6a17e..2fe6e49a1 100644 --- a/bucardo.schema +++ b/bucardo.schema @@ -196,8 +196,8 @@ COMMENT ON TABLE bucardo.db IS $$Holds information about each database used in r ALTER TABLE bucardo.db ADD CONSTRAINT db_status CHECK (status IN ('active','inactive')); -CREATE UNIQUE INDEX db_dsn_unique ON bucardo.db(dbhost,dbport,dbname,dbuser) WHERE NOT name ~ '^bctest' AND dbport <> ''; - +CREATE UNIQUE INDEX db_dsn_unique ON bucardo.db(dbtype,dbhost,dbport,dbname,dbuser) + WHERE NOT name ~ '^bctest' AND dbport <> ''; -- -- Databases can belong to zero or more named groups diff --git a/t/02-bctl-db.t b/t/02-bctl-db.t index 7424e2528..ca61f32a1 100644 --- a/t/02-bctl-db.t +++ b/t/02-bctl-db.t @@ -61,19 +61,19 @@ $res = $bct->ctl('bucardo add database foobarz --force'); like ($res, qr/add anyway.*Added database "foobarz"/s, $t); $t = 'Add database works for cluster A'; -$res = $bct->ctl("bucardo add db bucardo_test name=A user=$dbuserA port=$dbportA host=$dbhostA"); +$res = $bct->ctl("bucardo add db A dbname=bucardo_test user=$dbuserA port=$dbportA host=$dbhostA"); is ($res, qq{Added database "A"\n}, $t); $t = q{Add database fails if using the same internal name}; -$res = $bct->ctl("bucardo add db postgres name=A user=$dbuserA port=$dbportA host=$dbhostA"); +$res = $bct->ctl("bucardo add db A dbname=postgres user=$dbuserA port=$dbportA host=$dbhostA"); like ($res, qr/Cannot add database: the name "A" already exists/, $t); $t = q{Add database fails if same paramters given}; -$res = $bct->ctl("bucardo add db bucardo_test name=A2 user=$dbuserA port=$dbportA host=$dbhostA"); +$res = $bct->ctl("bucardo add db A2 dbname=bucardo_test user=$dbuserA port=$dbportA host=$dbhostA"); like ($res, qr/same parameters/, $t); $t = 'Add database works for cluster B works with ssp=false'; -$res = $bct->ctl("bucardo add db bucardo_test name=B user=$dbuserB port=$dbportB host=$dbhostB ssp=0"); +$res = $bct->ctl("bucardo add db B dbname=bucardo_test user=$dbuserB port=$dbportB host=$dbhostB ssp=0"); like ($res, qr/Added database "B"/, $t); $t = 'List databases gives expected results'; @@ -92,21 +92,21 @@ is ($res, qq{Removed database "A"\nRemoved database "B"\n}, $t); ## Tests of add database with group modifier $t = 'Add database works when adding to a new dbgroup'; -$res = $bct->ctl("bucardo add db bucardo_test name=A user=$dbuserA port=$dbportA host=$dbhostA group=group1"); +$res = $bct->ctl("bucardo add db A dbname=bucardo_test user=$dbuserA port=$dbportA host=$dbhostA group=group1"); like ($res, qr/Added database "A".*Created database group "group1".*Added database "A" to group "group1" as target/s, $t); $t = 'Add database works when adding to an existing dbgroup'; -$res = $bct->ctl("bucardo add db bucardo_test name=B user=$dbuserB port=$dbportB host=$dbhostB group=group1"); +$res = $bct->ctl("bucardo add db B dbname=bucardo_test user=$dbuserB port=$dbportB host=$dbhostB group=group1"); like ($res, qr/Added database "B" to group "group1" as target/s, $t); $t = 'Add database works when adding to an existing dbgroup as role source'; $bct->ctl('bucardo remove db B'); -$res = $bct->ctl("bucardo add db bucardo_test name=B user=$dbuserB port=$dbportB host=$dbhostB group=group1:source"); +$res = $bct->ctl("bucardo add db B dbname=bucardo_test user=$dbuserB port=$dbportB host=$dbhostB group=group1:source"); like ($res, qr/Added database "B" to group "group1" as source/s, $t); $t = q{Adding a database into a new group works with 'dbgroup'}; $bct->ctl('bucardo remove db B'); -$res = $bct->ctl("bucardo add db bucardo_test name=B user=$dbuserB port=$dbportB host=$dbhostB dbgroup=group1:replica"); +$res = $bct->ctl("bucardo add db B dbname=bucardo_test user=$dbuserB port=$dbportB host=$dbhostB dbgroup=group1:replica"); like ($res, qr/Added database "B" to group "group1" as target/s, $t); ## Tests for 'remove database' @@ -120,7 +120,7 @@ $res = $bct->ctl('bucardo remove db B'); like ($res, qr/Removed database "B"/, $t); $t = q{Able to remove more than one database at a time}; -$bct->ctl("bucardo add db bucardo_test name=B user=$dbuserB port=$dbportB host=$dbhostB"); +$bct->ctl("bucardo add db B dbname=bucardo_test user=$dbuserB port=$dbportB host=$dbhostB"); $res = $bct->ctl('bucardo remove db A B foobarz'); like ($res, qr/Removed database "A"\nRemoved database "B"/ms, $t); @@ -130,7 +130,7 @@ $t = q{List database returns correct message when no databases}; $res = $bct->ctl('bucardo list db'); like ($res, qr/No databases/, $t); -$bct->ctl("bucardo add db bucardo_test name=B user=$dbuserB port=$dbportB host=$dbhostB ssp=1"); +$bct->ctl("bucardo add db B dbname=bucardo_test user=$dbuserB port=$dbportB host=$dbhostB ssp=1"); $t = q{List databases shows the server_side_prepare setting}; $res = $bct->ctl('bucardo list database B -vv'); like ($res, qr/server_side_prepares = 1/s, $t); @@ -143,7 +143,7 @@ like ($res, qr/Database: B/, $t); $t = q{Add database works with 'addalltables'}; $command = -"bucardo add db bucardo_test name=A user=$dbuserA port=$dbportA host=$dbhostA addalltables"; +"bucardo add db A dbname=bucardo_test user=$dbuserA port=$dbportA host=$dbhostA addalltables"; $res = $bct->ctl($command); like ($res, qr/Added database "A"\nNew tables added: \d/s, $t); @@ -158,7 +158,7 @@ like ($res, qr/that reference database "A".*Removed database "A"/s, $t); $t = q{Add database with 'addallsequences' works}; $res = $bct->ctl("bucardo remove dbgroup abc"); $command = -"bucardo add db bucardo_test name=A user=$dbuserA port=$dbportA host=$dbhostA addallsequences"; +"bucardo add db A dbname=bucardo_test user=$dbuserA port=$dbportA host=$dbhostA addallsequences"; $res = $bct->ctl($command); like ($res, qr/Added database "A"\nNew sequences added: \d/s, $t); @@ -168,7 +168,7 @@ is ($res, '', $t); $t = q{Add database respects the --quiet flag}; $command = -"bucardo add db bucardo_test name=B user=$dbuserB port=$dbportB host=$dbhostB --quiet"; +"bucardo add db B dbname=bucardo_test user=$dbuserB port=$dbportB host=$dbhostB --quiet"; $res = $bct->ctl($command); is ($res, '', $t); -- 2.39.5