postgis/utils/postgis_proc_upgrade.pl

195 lines
4 KiB
Perl
Raw Normal View History

#/bin/sh
#
# This script produces an .sql file containing
# CREATE OR REPLACE calls for each function
# in postgis.sql
#
# In addition, the transaction contains
# a check for Major postgis_lib_version()
# to match the one contained in lwpostgis.sql
#
# This never happens by just running make install
# as MODULE_FILENAME contains SO_MAJOR under
# all architectures.
#
#
eval "exec perl -w $0 $@"
if (0);
use strict;
($#ARGV == 0) ||
die "Usage: perl postgis_proc_upgrade.pl <postgis.sql> [<schema>]\nCreates a new SQL script to upgrade all of the PostGIS functions.\n"
if ( @ARGV < 1 || @ARGV > 2 );
my $NEWVERSION = "UNDEF";
my %newtypes = ( "box3d_extent", 1, "pgis_abs", 1 );
print "BEGIN;\n";
print "SET search_path TO $ARGV[1];\n" if @ARGV>1;
open( INPUT, $ARGV[0] ) || die "Couldn't open file: $ARGV[0]\n";
while(<INPUT>)
{
#
# Since 1.1.0 scripts/lib/release versions are the same
#
if (/INSTALL VERSION: (.*)/)
{
$NEWVERSION = $1;
last;
}
}
close(INPUT);
print "-- $NEWVERSION\n";
while(<DATA>)
{
s/NEWVERSION/$NEWVERSION/g;
print;
}
open( INPUT, $ARGV[0] ) || die "Couldn't open file: $ARGV[0]\n";
while(<INPUT>)
{
my $checkit = 0;
if (m/^create or replace function/i)
{
$checkit = 1 if m/postgis_scripts_installed()/i;
print $_;
while(<INPUT>)
{
if ( $checkit && m/SELECT .'(\d\.\d\.\d).'::text/i )
{
$NEWVERSION = $1;
}
print $_;
last if m/^\s*LANGUAGE '/;
}
}
if ( m/^create type (\S+)/i )
{
my $newtype = $1;
my $def .= $_;
while(<INPUT>)
{
$def .= $_;
last if m/\)/;
}
print $def if $newtypes{$newtype};
}
# This code handles casts by dropping and recreating them.
if ( /^create cast\s+\(\s*(\w+)\s+as\s+(\w+)\)/i )
{
my $type1 = $1;
my $type2 = $2;
my $def = $_;
print "DROP CAST IF EXISTS ($type1 AS $type2);\n";
print $def;
}
# This code handles aggregates by dropping and recreating them.
if ( /^create aggregate\s+(\S+)\s*\(/i )
{
my $aggname = $1;
my $basetype = 'unknown';
my $def = $_;
while(<INPUT>)
{
$def .= $_;
$basetype = $1 if (m/basetype\s*=\s*([^,]*)\s*,/i);
last if m/\);/;
}
print "DROP AGGREGATE IF EXISTS $aggname($basetype);\n";
print $def;
}
# This code handles operators by creating them if we are doing a major upgrade
if ( /^create operator\s+(\S+)\s*\(/i )
{
my $opname = $1;
my $basetype = 'unknown';
my $def = $_;
while(<INPUT>)
{
$def .= $_;
$basetype = $1 if ( m/leftarg\s*=\s*(\w+)\s*,/i );
last if m/\);/;
}
print $def;
}
# This code handles operator classes by creating them if we are doing a major upgrade
if ( /^create operator class\s+(\w+)\s*/i )
{
my $opclassname = $1;
my $opctype = 'unknown';
my $opcidx = 'unknown';
my $def = $_;
while(<INPUT>)
{
$def .= $_;
$opctype = $1 if ( m/for type (\w+) /i );
$opcidx = $1 if ( m/using (\w+) /i );
last if m/\);/;
}
$opctype =~ tr/A-Z/a-z/;
$opcidx =~ tr/A-Z/a-z/;
print $def;
}
}
close( INPUT );
print "COMMIT;\n";
1;
__END__
CREATE OR REPLACE FUNCTION postgis_major_version_check()
RETURNS text
AS '
DECLARE
old_scripts text;
new_scripts text;
old_maj text;
new_maj text;
BEGIN
--
-- This uses postgis_lib_version() rather then
-- postgis_scripts_installed() as in 1.0 because
-- in the 1.0 => 1.1 transition that would result
-- in an impossible upgrade:
--
-- from 0.3.0 to 1.1.0
--
-- Next releases will still be ok as
-- postgis_lib_version() and postgis_scripts_installed()
-- would both return actual PostGIS release number.
--
SELECT into old_scripts postgis_lib_version();
SELECT into new_scripts ''NEWVERSION'';
SELECT into old_maj substring(old_scripts from 1 for 2);
SELECT into new_maj substring(new_scripts from 1 for 2);
IF old_maj != new_maj THEN
RAISE EXCEPTION ''Upgrade from version % to version % requires a dump/reload. See PostGIS manual for instructions'', old_scripts, new_scripts;
ELSE
RETURN ''Scripts versions checked for upgrade: ok'';
END IF;
END
'
LANGUAGE 'plpgsql';
SELECT postgis_major_version_check();
DROP FUNCTION postgis_major_version_check();