From b71af05d628025b8df2b5b6a737c2a9611211c8d Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 17 Sep 2018 14:44:29 +0800 Subject: [PATCH] Backward compatibility for dropping functions in `ColumnDropper`. https://meta.discourse.org/t/launcher-rebuild-error-pg-error-schema-discourse-functions-does-not-exist/96209 --- lib/migration/base_dropper.rb | 17 ++++++++++++++++- lib/migration/column_dropper.rb | 3 +++ lib/migration/table_dropper.rb | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/migration/base_dropper.rb b/lib/migration/base_dropper.rb index eff5fcdb1b5..9215c559df2 100644 --- a/lib/migration/base_dropper.rb +++ b/lib/migration/base_dropper.rb @@ -82,7 +82,22 @@ module Migration "readonly()" ].compact.join("_") - "#{FUNCTION_SCHEMA_NAME}.#{function_name}" + if DB.exec(<<~SQL).to_s == '1' + SELECT schema_name + FROM information_schema.schemata + WHERE schema_name = '#{FUNCTION_SCHEMA_NAME}' + SQL + + "#{FUNCTION_SCHEMA_NAME}.#{function_name}" + else + function_name + end + end + + def self.old_readonly_function_name(table_name, column_name = nil) + readonly_function_name(table_name, column_name).sub( + "#{FUNCTION_SCHEMA_NAME}.", '' + ) end def self.readonly_trigger_name(table_name, column_name = nil) diff --git a/lib/migration/column_dropper.rb b/lib/migration/column_dropper.rb index 10e7b37b1c4..77f3d118813 100644 --- a/lib/migration/column_dropper.rb +++ b/lib/migration/column_dropper.rb @@ -57,6 +57,9 @@ module Migration DB.exec <<~SQL DROP TRIGGER IF EXISTS #{BaseDropper.readonly_trigger_name(@table, column)} ON #{@table}; DROP FUNCTION IF EXISTS #{BaseDropper.readonly_function_name(@table, column)} CASCADE; + -- Backward compatibility for old functions created in the public + -- schema + DROP FUNCTION IF EXISTS #{BaseDropper.old_readonly_function_name(@table, column)} CASCADE; SQL # safe cause it is protected on method entry, can not be passed in params diff --git a/lib/migration/table_dropper.rb b/lib/migration/table_dropper.rb index 0e1a938983a..5718ac0ecb7 100644 --- a/lib/migration/table_dropper.rb +++ b/lib/migration/table_dropper.rb @@ -75,6 +75,9 @@ module Migration DB.exec <<~SQL DROP FUNCTION IF EXISTS #{BaseDropper.readonly_function_name(@old_name)} CASCADE; + -- Backward compatibility for old functions created in the public + -- schema + DROP FUNCTION IF EXISTS #{BaseDropper.old_readonly_function_name(@old_name)} CASCADE; SQL end end