Support multi-schema in slonik_build_env
authorMarc Cousin <marc.cousin@dalibo.com>
Mon, 5 Dec 2016 13:37:02 +0000 (14:37 +0100)
committerSteve Singer <ssinger@ca.afilias.info>
Sat, 10 Dec 2016 15:51:40 +0000 (10:51 -0500)
tools/altperl/slonik_build_env.pl

index 91c86d016b8de23e8f54c4bea7d1a6c79d9d3f0a..4c01a0947d47908f33c8aa8abe6718577c7dcda9 100644 (file)
@@ -19,16 +19,19 @@ my $dataBaseUser;
 my $dataBasePassword;
 my $dataBasePort;
 my @nodes;
-my $schema = 'public';
+my @schema=();
 my $usage =
-  "$0 -node host:database:user[:password:port] [-node ...] [-schema myschema]
+  "$0 -node host:database:user[:password:port] [-node ...] [-schema myschema] [-schema myschema2...]
 First node is assumed to be the master.
 Default schema is \"public\"\n";
 
-&usage if ( !GetOptions( 'node=s@' => \@nodes, 'schema=s' => \$schema ) );
+&usage if ( !GetOptions( 'node=s@' => \@nodes, 'schema=s' => \@schema ) );
 
 die "At least one node is required" if ( scalar(@nodes) < 1 );
 
+# If we get no schema, use public
+@schema = ('public') unless (@schema);
+
 my $nodeNumber = 1;
 my $parentString;
 foreach my $node (@nodes) {
@@ -58,20 +61,22 @@ my $dbh = DBI->connect( $connectString, $dataBaseUser, $dataBasePassword,
     { RaiseError => 0, PrintError => 0, AutoCommit => 1 } );
 die "connect: $DBI::errstr" if ( !defined($dbh) || $DBI::err );
 
-# Read in all the user 'normal' tables in $schema (public by default).
+# Read in all the user 'normal' tables in @schema (public by default).
+# put all schemas between single quotes for the query
+my @protected_schema=map("'".$_."'",@schema);
 my $tableQuery = $dbh->prepare( "
 SELECT pg_namespace.nspname || '.' || pg_class.relname,pg_class.relkind,pg_class.relhaspkey 
 FROM pg_namespace,pg_class
 WHERE pg_class.reltype > 0
 AND pg_class.relnamespace = pg_catalog.pg_namespace.oid
 AND (pg_class.relkind = 'r' OR pg_class.relkind = 'S')
-AND pg_namespace.nspname = '$schema' AND pg_namespace.oid = pg_class.relnamespace"
+AND pg_namespace.nspname IN (" . join(',',@protected_schema) . ") AND pg_namespace.oid = pg_class.relnamespace"
 );
 
 die "prepare(tableQuery): $DBI::errstr"
   if ( !defined($tableQuery) || $DBI::err );
 die "execute(tableQuery): $DBI::errstr" if ( !$tableQuery->execute() );
-die "No objects to replicate found in schema \"$schema\"\n"
+die "No objects to replicate found in schema(s) \"" . join(',',@schema) . "\"\n"
   if ( $tableQuery->rows <= 0 );
 
 my @tablesWithIndexes;