From 762e6c82b36588c7ffd1367ab67bfc5dce1e6f7d Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Fri, 24 May 2024 18:12:35 +0200 Subject: [PATCH] Simplify upgrade of postgis databases Adds new commands: list-all and list-enabled. Allow passing no argument to commands "status" and "upgrade" to perform the operation on all databases. --- NEWS | 6 +++++ loader/postgis.pl | 58 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index afe3a39f541..55cab590e89 100644 --- a/NEWS +++ b/NEWS @@ -44,6 +44,12 @@ Andreas Schild (German Team) * New Features * + - Improvements in the 'postgis' script: + - new command list-enabled + - new command list-all + - command upgrade upgrades all databases that need to be + - command status reports status of all databases + (Sandro Santilli) - #5721, Allow sharing sequences between different topologies (Lars Opsahl) - #5667, TopoGeo_LoadGeometry (Sandro Santilli) - #5055, add explicit <> geometry operator to prevent non-unique diff --git a/loader/postgis.pl b/loader/postgis.pl index 553e2216209..c8973d7e7be 100644 --- a/loader/postgis.pl +++ b/loader/postgis.pl @@ -18,9 +18,11 @@ sub usage print qq{Usage: $0 [] Commands: help print this message and exits - enable enable PostGIS in given database - upgrade upgrade PostGIS in given database - status print PostGIS status in given database + list-all list all databases + list-enabled list PostGIS-enabled databases + enable ... enable PostGIS in given databases + upgrade [...] upgrade PostGIS in given databases (or all) + status [...] print PostGIS status in given databases (or all) install-extension-upgrades [--pg_sharedir ] [--extension ] [...] Ensure files required to upgrade PostGIS from the given version are installed on the system. @@ -174,6 +176,35 @@ sub install_extension_upgrades } +sub get_all_databases +{ + my $ALL = `psql -qXtAc 'select datname from pg_database where datallowconn' template1`; + return split("\n", $ALL); +} + +sub list_all_databases +{ + my @ALL = get_all_databases(); + foreach (@ALL) + { + print $_ . "\n"; + } +} + +sub list_enabled_databases +{ + my @ALL = get_all_databases(); + my $sql = " + SELECT proname + FROM pg_proc + WHERE proname = 'postgis_full_version' + "; + foreach my $db (@ALL) + { + my $enabled = `psql -qXtAc "${sql}" ${db}`; + print $db . "\n" if $enabled ne ''; + } +} sub enable { @@ -185,11 +216,14 @@ sub enable sub upgrade { - die "Please specify at least a database name" unless @_; + my @DB = @_; - foreach my $db (@_) + #die "Please specify at least a database name" unless @DB; + @DB = get_all_databases() unless @DB; + + foreach my $db (@DB) { - print "upgrading db $db if needed\n"; + print "upgrading PostGIS in db $db if needed\n"; open(my $SESSION, "| psql -qXtA ${db} |") || die "Could not connect to database ${db}"; print $SESSION <<'EOF'; DO $BODY$ @@ -217,9 +251,11 @@ BEGIN sub status { - die "Please specify at least a database name" unless @_; + my @DB = @_; + + @DB = get_all_databases() unless @DB; - foreach my $db (@_) + foreach my $db (@DB) { my $sql = " SELECT n.nspname @@ -284,6 +320,12 @@ sub status elsif ( $cmd eq "enable" ) { exit enable(@ARGV); } +elsif ( $cmd eq "list-all" ) { + exit list_all_databases(@ARGV); +} +elsif ( $cmd eq "list-enabled" ) { + exit list_enabled_databases(@ARGV); +} elsif ( $cmd eq "upgrade" ) { exit upgrade(@ARGV); }