From 91ede977785e01102a634abd686b19de5e09a4cb Mon Sep 17 00:00:00 2001 From: Marc Cousin Date: Mon, 5 Dec 2016 14:37:02 +0100 Subject: [PATCH] Support multi-schema in slonik_build_env --- tools/altperl/slonik_build_env.pl | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tools/altperl/slonik_build_env.pl b/tools/altperl/slonik_build_env.pl index 91c86d01..4c01a094 100644 --- a/tools/altperl/slonik_build_env.pl +++ b/tools/altperl/slonik_build_env.pl @@ -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; -- 2.39.5