CVS User Account cvsuser
Sun Feb 20 02:12:15 PST 2005
Log Message:
-----------
Added support for multiple sets in slon_tools.conf.

Modified create_set.pl to accept a set ID or name (as given in
slon_tools.conf).  I put the (unfortunately-named) get_set function in
slon-tools.pm so that it can be used by other set-manipulating
scripts.

Also added --config and --help flags to create_set.pl, as I've been
doing with the other scripts.

Modified Files:
--------------
    slony1-engine/tools/altperl:
        slon-tools.pm (r1.16 -> r1.17)
        create_set.pl (r1.12 -> r1.13)
        slon_tools.conf-sample (r1.1 -> r1.2)

-------------- next part --------------
Index: slon-tools.pm
===================================================================
RCS file: /usr/local/cvsroot/slony1/slony1-engine/tools/altperl/slon-tools.pm,v
retrieving revision 1.16
retrieving revision 1.17
diff -Ltools/altperl/slon-tools.pm -Ltools/altperl/slon-tools.pm -u -w -r1.16 -r1.17
--- tools/altperl/slon-tools.pm
+++ tools/altperl/slon-tools.pm
@@ -204,6 +204,50 @@
   return $result;
 }
 
+# This is a horrible function name, but it really *is* what it should
+# be called.
+sub get_set {
+    my $set = shift();
+    my $match;
+
+    # If the variables are already set through $ENV{SLONYSET}, just
+    # make sure we have an integer for $SET_ID
+    if ($TABLE_ID) {
+	return 0 unless $set =~ /^(?:set)?(\d+)$/;
+	return $1;
+    }
+
+    # Die if we don't have any sets defined in the configuration file.
+    unless (defined $SLONY_SETS
+	    and ref($SLONY_SETS) eq "HASH"
+	    and keys %{$SLONY_SETS}) {
+	die "There are no sets defined in your configuration file.";
+    }
+
+    # Is this a set name or number?
+    if ($SLONY_SETS->{$set}) {
+	$match = $SLONY_SETS->{$set};
+    }
+    elsif ($set =~ /^\d+$/) {
+	my ($name) = grep { $SLONY_SETS->{$_}->{"set_id"} == $set } keys %{$SLONY_SETS};
+	$match = $SLONY_SETS->{$name};
+    }
+    else {
+	return 0;
+    }
+
+    # Set the variables for this set.
+    $TABLE_ID     = $match->{"table_id"};
+    $SEQUENCE_ID  = $match->{"sequence_id"};
+    @PKEYEDTABLES = @{$match->{"pkeyedtables"}};
+    %KEYEDTABLES  = %{$match->{"keyedtables"}};
+    @SERIALTABLES = @{$match->{"serialtables"}};
+    @SEQUENCES    = @{$match->{"sequences"}};
+
+    return $match->{"set_id"};
+}
+
+
 # This function checks to see if there is a still-in-progress subscription
 # It does so by looking to see if there is a SUBSCRIBE_SET event corresponding
 # to a sl_subscribe entry that is not yet active.
Index: create_set.pl
===================================================================
RCS file: /usr/local/cvsroot/slony1/slony1-engine/tools/altperl/create_set.pl,v
retrieving revision 1.12
retrieving revision 1.13
diff -Ltools/altperl/create_set.pl -Ltools/altperl/create_set.pl -u -w -r1.12 -r1.13
--- tools/altperl/create_set.pl
+++ tools/altperl/create_set.pl
@@ -3,96 +3,116 @@
 # Author: Christopher Browne
 # Copyright 2004 Afilias Canada
 
-require '@@PGLIBDIR@@/slon-tools.pm';
-require '@@SYSCONFDIR@@/slon_tools.conf';
-my ($set) = @ARGV;
-if ($set =~ /^set(\d+)$/) {
-  $set = $1;
-} else {
-  print "Need set identifier\n";
-  die "create_set.pl setN\n";
-}
+use Getopt::Long;
 
-$OUTPUTFILE="/tmp/add_tables.$$";
+$CONFIG_FILE = '@@SYSCONFDIR@@/slon_tools.conf';
+$SHOW_USAGE  = 0;
 
-open (OUTFILE, ">$OUTPUTFILE");
-print OUTFILE genheader();
+# Read command-line options
+GetOptions("config=s" => \$CONFIG_FILE,
+	   "help"     => \$SHOW_USAGE);
+
+my $USAGE =
+"Usage: create_set.pl [--config file] set
+
+    set  The name or ID of the set to be created
 
-foreach my $table (@SERIALTABLES) {
-  $table = ensure_namespace($table);
-  print OUTFILE "
-		echo '  Adding unique key to table $table...';
-		table add key (
-		    node id=$MASTERNODE,
-		    full qualified name='$table'
-		);
 ";
+
+if ($SHOW_USAGE) {
+    print $USAGE;
+    exit 0;
 }
-print OUTFILE "
-try {
-      create set (id = $set, origin = $MASTERNODE, comment = 'Set $set for $CLUSTER_NAME');
-} on error {
-      echo 'Could not create subscription set $set for $CLUSTER_NAME!';
-      exit -1;
-}
-";
-print OUTFILE "
-	echo 'Subscription set $set created';
-	echo 'Adding tables to the subscription set';
 
-";
+require '@@PGLIBDIR@@/slon-tools.pm';
+require $CONFIG_FILE;
 
-if ($TABLE_ID < 1) {
-  $TABLE_ID = 1;
+my ($set) = @ARGV;
+$SET_ID = get_set($set);
+unless ($SET_ID) {
+    die $USAGE;
 }
+
+$FILE="/tmp/add_tables.$$";
+open (SLONIK, ">", $FILE);
+print SLONIK genheader();
+
+# Tables without primary keys
+print SLONIK "\n";
+print SLONIK "# TABLE ADD KEY\n";
 foreach my $table (@SERIALTABLES) {
   $table = ensure_namespace($table);
-  print OUTFILE "
-		set add table (set id = $set, origin = $MASTERNODE, id = $TABLE_ID, full qualified name = '$table', comment = 'Table $table without primary key', key=serial);
-                echo 'Add unkeyed table $table';
-"; 
+    print SLONIK "  echo '  Adding unique key to table $table...';\n";
+    print SLONIK "  table add key (\n";
+    print SLONIK "    node id=$MASTERNODE,\n";
+    print SLONIK "    full qualified name='$table'\n";
+    print SLONIK "  );\n";
+}
+
+# CREATE SET
+print SLONIK "\n";
+print SLONIK "# CREATE SET\n";
+print SLONIK "  try {\n";
+print SLONIK "    create set (id = $SET_ID, origin = $MASTERNODE, comment = 'Set $SET_ID for $CLUSTER_NAME');\n";
+print SLONIK "  } on error {\n";
+print SLONIK "    echo 'Could not create subscription set $SET_ID for $CLUSTER_NAME!';\n";
+print SLONIK "    exit -1;\n";
+print SLONIK "  }\n";
+
+# SET ADD TABLE
+print SLONIK "\n";
+print SLONIK "# SET ADD TABLE\n";
+print SLONIK "  echo 'Subscription set $SET_ID created';\n";
+print SLONIK "  echo 'Adding tables to the subscription set';\n";
+
+$TABLE_ID = 1 if $TABLE_ID < 1;
+
+foreach my $table (@SERIALTABLES) {
+    $table = ensure_namespace($table);
+    print SLONIK "  set add table (set id = $SET_ID, origin = $MASTERNODE, id = $TABLE_ID,\n";
+    print SLONIK "                 full qualified name = '$table', key=serial,\n";
+    print SLONIK "                 comment = 'Table $table without primary key');\n";
+    print SLONIK "  echo 'Add unkeyed table $table';\n";
   $TABLE_ID++;
 }
 
 foreach my $table (@PKEYEDTABLES) {
   $table = ensure_namespace($table);
-  print OUTFILE "
-		set add table (set id = $set, origin = $MASTERNODE, id = $TABLE_ID, full qualified name = '$table', comment = 'Table $table with primary key');
-                echo 'Add primary keyed table $table';
-";
+    print SLONIK "  set add table (set id = $SET_ID, origin = $MASTERNODE, id = $TABLE_ID,\n";
+    print SLONIK "                 full qualified name = '$table',\n";
+    print SLONIK "                 comment = 'Table $table with primary key');\n";
+    print SLONIK "  echo 'Add primary keyed table $table';\n";
   $TABLE_ID++;
 }
 
 foreach my $table (keys %KEYEDTABLES) {
   my $key = $KEYEDTABLES{$table};
   $table = ensure_namespace($table);
-  print OUTFILE "
-		set add table (set id = $set, origin = $MASTERNODE, id = $TABLE_ID, full qualified name = '$table', key='$key', comment = 'Table $table with candidate primary key $key');
-                echo 'Add candidate primary keyed table $table';
-";
+    print SLONIK "  set add table (set id = $SET_ID, origin = $MASTERNODE, id = $TABLE_ID,\n";
+    print SLONIK "                 full qualified name = '$table', key='$key'\n";
+    print SLONIK "                 comment = 'Table $table with candidate primary key $key');\n";
+    print SLONIK "  echo 'Add candidate primary keyed table $table';\n";
   $TABLE_ID++;
 }
 
-print OUTFILE "
-                echo 'Adding sequences to the subscription set';
-";
+# SET ADD SEQUENCE
+print SLONIK "\n";
+print SLONIK "# SET ADD SEQUENCE\n";
+print SLONIK "  echo 'Adding sequences to the subscription set';\n";
 
-if ($SEQUENCE_ID < 1) {
-  $SEQUENCE_ID = 1;
-}
+$SEQUENCE_ID = 1 if $SEQUENCE_ID < 1;
 foreach my $seq (@SEQUENCES) {
   $seq = ensure_namespace($seq);
-  print OUTFILE "
-                set add sequence (set id = $set, origin = $MASTERNODE, id = $SEQUENCE_ID, full qualified name = '$seq', comment = 'Sequence $seq');
-                echo 'Add sequence $seq';
-";
+    print SLONIK "  set add sequence (set id = $SET_ID, origin = $MASTERNODE, id = $SEQUENCE_ID,\n";
+    print SLONIK "                    full qualified name = '$seq',\n";
+    print SLONIK "                    comment = 'Sequence $seq');\n";
+    print SLONIK "  echo 'Add sequence $seq';\n";
   $SEQUENCE_ID++;
 }
-print OUTFILE "
-        echo 'All tables added';
-";
+print SLONIK "  echo 'All tables added';\n";
 
-run_slonik_script($OUTPUTFILE);
+close SLONIK;
+run_slonik_script($FILE);
 
 ### If object hasn't a namespace specified, assume it's in "public", and make it so...
 sub ensure_namespace {
@@ -104,4 +124,3 @@
   }
   return $object;
 }
-
Index: slon_tools.conf-sample
===================================================================
RCS file: /usr/local/cvsroot/slony1/slony1-engine/tools/altperl/slon_tools.conf-sample,v
retrieving revision 1.1
retrieving revision 1.2
diff -Ltools/altperl/slon_tools.conf-sample -Ltools/altperl/slon_tools.conf-sample -u -w -r1.1 -r1.2
--- tools/altperl/slon_tools.conf-sample
+++ tools/altperl/slon_tools.conf-sample
@@ -66,58 +66,77 @@
              password => '');
 }
 
-# Keeping the following three lines for backwards compatibility in
-# case this gets incorporated into a 1.0.6 release.
-#
-# TODO: The scripts should check for an environment variable
-# containing the location of a configuration file.  That would
-# simplify this configuration file and allow Slony tools to still work
-# in situations where it doesn't exist.
-#
-if ($ENV{"SLONYSET"}) {
-    require $ENV{"SLONYSET"};
-} else {
+# The $SLONY_SETS variable contains information about all of the sets
+# in your cluster.
+
+my $SLONY_SETS = {
+
+    # A unique name for the set
+    "set1" => {
 
-    # The first ID to use for tables and sequences that are added to
-    # the replication cluster.  This must be unique across the
+	# The set_id, also unique
+	"set_id" => 1,
+
+	# The first ID to use for tables and sequences that are added
+	# to the replication cluster.  This must be unique across the
     # cluster.
     # 
-    # TODO: This should be determined automatically, which can be done
-    # fairly easily in most cases using psql.  create_set should
-    # derive it, and give an option to override it with a specific
-    # value.
-    $TABLE_ID    = 1;
-    $SEQUENCE_ID = 1;
-
-    # This array contains a list of tables that already have primary
-    # keys.
-    @PKEYEDTABLES=(
+	# TODO: This should be determined automatically, which can be
+	# done fairly easily in most cases using psql.  create_set
+	# should derive it, and give an option to override it with a
+	# specific value.
+	"table_id"    => 1,
+	"sequence_id" => 1,
+
+	# This array contains a list of tables that already have
+	# primary keys.
+        "pkeyedtables" => [
 		   'table1',
-		   'table2'
-		   );
+			   'table2',
+			   ],
 
-    # For tables that have unique not null keys, but no primary key,
-    # enter their names and indexes here.
-    %KEYEDTABLES=(
+	# For tables that have unique not null keys, but no primary
+	# key, enter their names and indexes here.
+	"keyedtables" => {
 		  'table3' => 'index_on_table3',
-		  'table4' => 'index_on_table4'
-		  );
+	    'table4' => 'index_on_table4',
+	},
 
-    # If a table does not have a suitable key or set of keys that can
-    # act as a primary key, Slony can add one.
+	# If a table does not have a suitable key or set of keys that
+	# can act as a primary key, Slony can add one.
     #
     # Note: The Slony development team does not recomment this
     # approach -- you should create your own primary keys instead.
-    @SERIALTABLES=(
-		   'table5'
-		   );
+	"serialtables" => ["table5"],
 
     # Sequences that need to be replicated should be entered here.
-    @SEQUENCES=(
-		'sequence1',
-		'sequence2'
-		);
+	"sequences" => ['sequence1',
+			'sequence2',
+			],
+    },
+
+    "set2" => {
+	"set_id"       => 2,
+	"table_id"     => 6,
+	"sequence_id"  => 3,
+	"pkeyedtables" => ["table6"],
+	"keyedtables"  => {},
+	"serialtables" => [],
+	"sequences"    => [],
+    },
+
+};
 
+# Keeping the following three lines for backwards compatibility in
+# case this gets incorporated into a 1.0.6 release.
+#
+# TODO: The scripts should check for an environment variable
+# containing the location of a configuration file.  That would
+# simplify this configuration file and allow Slony tools to still work
+# in situations where it doesn't exist.
+#
+if ($ENV{"SLONYSET"}) {
+    require $ENV{"SLONYSET"};
 }
 
 # Please do not add or change anything below this point.


More information about the Slony1-commit mailing list