File: postgis_proc_upgrade.pl

package info (click to toggle)
postgis 1.3.3-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 10,468 kB
  • ctags: 4,310
  • sloc: sql: 73,321; ansic: 35,513; xml: 6,160; java: 6,061; sh: 3,428; perl: 1,447; cpp: 987; makefile: 727; yacc: 276; python: 192
file content (146 lines) | stat: -rwxr-xr-x 3,152 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#/bin/sh

#
# This script produces an .sql file containing
# CREATE OR REPLACE calls for each function
# in lwpostgis.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";

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";

FUNC:
while(<INPUT>)
{
	#
	# Since 1.1.0 scripts/lib/release versions are the same
	#
	if (m/^create or replace function postgis_scripts_installed()/i)
	{
		while(<INPUT>)
		{
			if ( m/SELECT .'(\d\.\d\..*).'::text/i )
			{
				$NEWVERSION = $1;
				last FUNC;
			}
		}
	}
}

while(<DATA>)
{
	s/NEWVERSION/$NEWVERSION/g;
	print;
}

close(INPUT); 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 '/;
		}
	}

#	# This code handles aggregates by dropping and recreating them.
#	# The DROP would fail on aggregates as they would not exist
#	# in old postgis installations, thus we avoid this until we
#	# find a better strategy.
#
#	if (m/^create aggregate (.*) *\(/i)
#	{
#		my $aggname = $1;
#		my $basetype = 'unknown';
#		my $def = $_;
#		while(<INPUT>)
#		{
#			$def .= $_;
#			$basetype = $1 if (m/basetype *= *([^,]*) *,/);
#			last if m/\);/;
#		}
#		print "DROP AGGREGATE $aggname($basetype);\n";
#		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 ''Scripts 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();