Skip to content

Commit

Permalink
Simplify upgrade of postgis databases
Browse files Browse the repository at this point in the history
Adds new commands: list-all and list-enabled.

Allow passing no argument to commands "status" and "upgrade" to
perform the operation on all databases.
  • Loading branch information
strk committed May 24, 2024
1 parent ca40cf9 commit 762e6c8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 50 additions & 8 deletions loader/postgis.pl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ sub usage
print qq{Usage: $0 <command> [<args>]
Commands:
help print this message and exits
enable <database> enable PostGIS in given database
upgrade <database> upgrade PostGIS in given database
status <database> print PostGIS status in given database
list-all list all databases
list-enabled list PostGIS-enabled databases
enable <db>... enable PostGIS in given databases
upgrade [<db>...] upgrade PostGIS in given databases (or all)
status [<db>...] print PostGIS status in given databases (or all)
install-extension-upgrades [--pg_sharedir <dir>] [--extension <name>] [<from>...]
Ensure files required to upgrade PostGIS from
the given version are installed on the system.
Expand Down Expand Up @@ -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
{
Expand All @@ -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$
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 762e6c8

Please sign in to comment.