]> granicus.if.org Git - postgis/commitdiff
Do not DROP CASCADE types which are still used in columns
authorSandro Santilli <strk@kbt.io>
Thu, 6 Sep 2018 22:21:03 +0000 (22:21 +0000)
committerSandro Santilli <strk@kbt.io>
Thu, 6 Sep 2018 22:21:03 +0000 (22:21 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@16719 b70326c6-7e19-0410-871a-916f4a2858ee

utils/create_undef.pl

index 3795bb53d48bf6c182dec9941ca7f78b9f4353e3..13d3889896a6424f390cf5feab6d28c1dba43ae9 100755 (executable)
@@ -246,10 +246,44 @@ foreach my $fn (@funcs)
 }
 
 
-print "-- Drop all types.\n";
+print "-- Drop all types if unused in column types.\n";
+my $quotedtypelist = join ',', map { "'$_'" } @types;
 foreach my $type (@types)
 {
-       print "DROP TYPE IF EXISTS $type CASCADE;\n";
+       print <<EOF;
+DO \$\$
+DECLARE
+       rec RECORD;
+BEGIN
+       FOR rec IN
+               SELECT n.nspname, c.relname, a.attname, t.typname
+               FROM pg_attribute a
+               JOIN pg_class c ON a.attrelid = c.oid
+               JOIN pg_namespace n ON c.relnamespace = n.oid
+               JOIN pg_type t ON a.atttypid = t.oid
+               WHERE t.typname = '$type'
+                 AND NOT (
+                               -- we exclude coplexes defined as types
+                               -- by our own extension
+                               c.relkind = 'c'
+                               AND
+                               c.relname in ( $quotedtypelist )
+                       )
+       LOOP
+               RAISE EXCEPTION
+                 'Column "%" of table "%"."%" '
+                 'depends on type "%", drop it first',
+                 rec.attname, rec.nspname, rec.relname, rec.typname;
+       END LOOP;
+END;
+\$\$;
+-- NOTE: CASCADE is still needed for chicken-egg problem
+--       of input function depending on type and type
+--       depending on function
+DROP TYPE IF EXISTS $type CASCADE;
+
+EOF
+
 }
 
 print "-- Drop all functions needed for types definition.\n";