[postgis-tickets] r16719 - Do not DROP CASCADE types which are still used in columns

Sandro Santilli strk at kbt.io
Thu Sep 6 03:21:03 PDT 2018


Author: strk
Date: 2018-09-06 15:21:03 -0700 (Thu, 06 Sep 2018)
New Revision: 16719

Modified:
   trunk/utils/create_undef.pl
Log:
Do not DROP CASCADE types which are still used in columns

Modified: trunk/utils/create_undef.pl
===================================================================
--- trunk/utils/create_undef.pl	2018-09-06 22:20:56 UTC (rev 16718)
+++ trunk/utils/create_undef.pl	2018-09-06 22:21:03 UTC (rev 16719)
@@ -246,10 +246,44 @@
 }
 
 
-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";



More information about the postgis-tickets mailing list