From b01b1570aba95bb61aa52e49181cae75b7342eca Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 3 May 2022 23:40:34 +0100 Subject: [PATCH] FIX: Handle enum types during database restore (#16624) c1db9687 introduced an postgres enum type. Our database restore logic did not handle custom types correctly, and would therefore raise a 'type already exists' error when restoring any backup. This commit adds restore handling for enums, mirroring the similar logic for tables and views. --- lib/backup_restore.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/backup_restore.rb b/lib/backup_restore.rb index 60b5d25b27f..8e45e1dc8a7 100644 --- a/lib/backup_restore.rb +++ b/lib/backup_restore.rb @@ -103,6 +103,15 @@ module BackupRestore EXECUTE 'DROP VIEW IF EXISTS #{destination}.' || quote_ident(row.viewname) || ' CASCADE;'; EXECUTE 'ALTER VIEW #{source}.' || quote_ident(row.viewname) || ' SET SCHEMA #{destination};'; END LOOP; + -- move all enums to enums + FOR row IN ( + SELECT typname FROM pg_type t + LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace + WHERE typcategory = 'E' AND n.nspname = '#{source}' AND pg_catalog.pg_get_userbyid(typowner) = '#{owner}' + ) LOOP + EXECUTE 'DROP TYPE IF EXISTS #{destination}.' || quote_ident(row.typname) || ' CASCADE;'; + EXECUTE 'ALTER TYPE #{source}.' || quote_ident(row.typname) || ' SET SCHEMA #{destination};'; + END LOOP; END$$; SQL end