From ace720499fecd761af63aa6da3cffc1f321e983e Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Fri, 13 Nov 2009 22:13:17 +0000 Subject: [PATCH] Build out version specific upgrade scripts. git-svn-id: http://svn.osgeo.org/postgis/trunk@4804 b70326c6-7e19-0410-871a-916f4a2858ee --- postgis/Makefile.in | 12 ++- utils/postgis_proc_upgrade.pl | 159 ++++++++++++++++++++++++++++------ 2 files changed, 141 insertions(+), 30 deletions(-) diff --git a/postgis/Makefile.in b/postgis/Makefile.in index 5264acdcc..4923eeb8a 100644 --- a/postgis/Makefile.in +++ b/postgis/Makefile.in @@ -13,7 +13,7 @@ MODULE_big=postgis-@POSTGIS_MAJOR_VERSION@.@POSTGIS_MINOR_VERSION@ # Files to be copied to the contrib/ directory -DATA_built=postgis.sql uninstall_postgis.sql postgis_upgrade.sql +DATA_built=postgis.sql uninstall_postgis.sql postgis_upgrade_15_minor.sql postgis_upgrade_14_to_15.sql postgis_upgrade_13_to_15.sql DATA=../spatial_ref_sys.sql # SQL objects (files requiring C pre-processing) @@ -83,8 +83,14 @@ include $(PGXS) %.sql: %.sql.in sed 's,MODULE_PATHNAME,$$libdir/postgis-@POSTGIS_MAJOR_VERSION@.@POSTGIS_MINOR_VERSION@,g' $< >$@ -postgis_upgrade.sql: postgis.sql - $(PERL) ../utils/postgis_proc_upgrade.pl $< > $@ +postgis_upgrade_15_minor.sql: postgis.sql + $(PERL) ../utils/postgis_proc_upgrade.pl $< 1.5 > $@ + +postgis_upgrade_14_to_15.sql: postgis.sql + $(PERL) ../utils/postgis_proc_upgrade.pl $< 1.4 > $@ + +postgis_upgrade_13_to_15.sql: postgis.sql + $(PERL) ../utils/postgis_proc_upgrade.pl $< 1.3 > $@ # Generate any .sql.in files from .sql.in.c files by running them through the C pre-processor $(SQL_OBJS): %.in: %.in.c diff --git a/utils/postgis_proc_upgrade.pl b/utils/postgis_proc_upgrade.pl index eb6961179..dbd9e274b 100755 --- a/utils/postgis_proc_upgrade.pl +++ b/utils/postgis_proc_upgrade.pl @@ -20,18 +20,70 @@ eval "exec perl -w $0 $@" use strict; +# +# Conditionally upgraded types and operators. Only include these +# if the major numbers in version_from are less than the version_to +# number. +# +my $objs = { + "104" => { + "types" => { + "box3d_extent" => 1, + "pgis_abs" => 1 + } + }, + "105" => { + "operators" => { + "geography >" => 1, + "geography >=" => 1, + "geography =" => 1, + "geography <=" => 1, + "geography <" => 1, + "geography &&" => 1 + }, + "opclasses" => { + "gist_geography_ops" => 1, + "btree_geography_ops" => 1 + }, + "types" => { + "geography" => 1, + "gidx" => 1 + } + } +}; + +# +# Commandline argument handling +# ($#ARGV == 0) || -die "Usage: perl postgis_proc_upgrade.pl []\nCreates a new SQL script to upgrade all of the PostGIS functions.\n" - if ( @ARGV < 1 || @ARGV > 2 ); +die "Usage: perl postgis_proc_upgrade.pl []\nCreates a new SQL script to upgrade all of the PostGIS functions.\n" + if ( @ARGV < 1 || @ARGV > 3 ); -my $NEWVERSION = "UNDEF"; -my %newtypes = ( "box3d_extent", 1, "pgis_abs", 1 ); +my $sql_file = $ARGV[0]; +my $version_to = ""; +my $version_to_num = 0; +my $version_from = $ARGV[1]; +my $version_from_num = 0; +my $schema = ""; +$schema = $ARGV[2] if @ARGV > 2; -print "BEGIN;\n"; +if ( $version_from =~ /^(\d+)\.(\d+)/ ) +{ + $version_from_num = 100 * $1 + $2; +} +else +{ + die "Version from number invalid, must be of form X.X\n"; +} -print "SET search_path TO $ARGV[1];\n" if @ARGV>1; +die "Unable to open input SQL file $sql_file\n" + if ( ! -f $sql_file ); -open( INPUT, $ARGV[0] ) || die "Couldn't open file: $ARGV[0]\n"; +# +# Search the SQL file for the target version number (the +# version we are upgrading *to*. +# +open( INPUT, $sql_file ) || die "Couldn't open file: $sql_file\n"; while() { # @@ -39,41 +91,66 @@ while() # if (/INSTALL VERSION: (.*)/) { - $NEWVERSION = $1; + $version_to = $1; last; } } close(INPUT); -print "-- $NEWVERSION\n"; +die "Unable to locate target new version number in $sql_file\n" + if( ! $version_to ); +if ( $version_to =~ /(\d+)\.(\d+)\..*/ ) +{ + $version_to = $1 . "." . $2; + $version_to_num = 100 * $1 + $2; +} +else +{ + die "Version to number invalid, must be of form X.X.X\n"; +} + +print qq{ +-- +-- UPGRADE SCRIPT FROM PostGIS $version_from TO PostGIS $version_to +-- + +}; + +print "BEGIN;\n"; +print "SET search_path TO $schema;\n" if $schema; + +# +# Add in the conditional check function to ensure this script is +# not being applied to a major version update. +# while() { - s/NEWVERSION/$NEWVERSION/g; + s/NEWVERSION/$version_to/g; print; } - -open( INPUT, $ARGV[0] ) || die "Couldn't open file: $ARGV[0]\n"; +# +# Go through the SQL file and strip out objects that cannot be +# applied to an existing, loaded database: types and operators +# and operator classes that have already been defined. +# +open( INPUT, $sql_file ) || die "Couldn't open file: $sql_file\n"; while() { - my $checkit = 0; + if (m/^create or replace function/i) { - $checkit = 1 if m/postgis_scripts_installed()/i; print $_; while() { - if ( $checkit && m/SELECT .'(\d\.\d\.\d).'::text/i ) - { - $NEWVERSION = $1; - } print $_; last if m/^\s*LANGUAGE '/; } } - if ( m/^create type (\S+)/i ) + + if ( m/^create type (\w+)/i ) { my $newtype = $1; my $def .= $_; @@ -82,7 +159,16 @@ while() $def .= $_; last if m/\)/; } - print $def if $newtypes{$newtype}; + my $ver = $version_from_num + 1; + while( $version_from_num < $version_to_num && $ver <= $version_to_num ) + { + if( $objs->{$ver}->{"types"}->{$newtype} ) + { + print $def; + last; + } + $ver++; + } } # This code handles casts by dropping and recreating them. @@ -99,15 +185,15 @@ while() if ( /^create aggregate\s+(\S+)\s*\(/i ) { my $aggname = $1; - my $basetype = 'unknown'; + my $aggtype = 'unknown'; my $def = $_; while() { $def .= $_; - $basetype = $1 if (m/basetype\s*=\s*([^,]*)\s*,/i); + $aggtype = $1 if (m/basetype\s*=\s*([^,]*)\s*,/i); last if m/\);/; } - print "DROP AGGREGATE IF EXISTS $aggname($basetype);\n"; + print "DROP AGGREGATE IF EXISTS $aggname($aggtype);\n"; print $def; } @@ -115,15 +201,25 @@ while() if ( /^create operator\s+(\S+)\s*\(/i ) { my $opname = $1; - my $basetype = 'unknown'; + my $optype = 'unknown'; my $def = $_; while() { $def .= $_; - $basetype = $1 if ( m/leftarg\s*=\s*(\w+)\s*,/i ); + $optype = $1 if ( m/leftarg\s*=\s*(\w+)\s*,/i ); last if m/\);/; } - print $def; + my $opsig = $optype . " " . $opname; + my $ver = $version_from_num + 1; + while( $version_from_num < $version_to_num && $ver <= $version_to_num ) + { + if( $objs->{$ver}->{"operators"}->{$opsig} ) + { + print $def; + last; + } + $ver++; + } } # This code handles operator classes by creating them if we are doing a major upgrade @@ -142,7 +238,16 @@ while() } $opctype =~ tr/A-Z/a-z/; $opcidx =~ tr/A-Z/a-z/; - print $def; + my $ver = $version_from_num + 1; + while( $version_from_num < $version_to_num && $ver <= $version_to_num ) + { + if( $objs->{$ver}->{"opclasses"}->{$opclassname} ) + { + print $def; + last; + } + $ver++; + } } }