FIX: Create readonly functions during backup

Temporarily recreate already dropped functions in the discourse_functions schema in order to allow restoring of backups which still reference dropped functions.
This commit is contained in:
Gerhard Schlager
2019-08-08 16:06:27 +02:00
parent 8aa5df69f0
commit 7cb51d0e40
11 changed files with 164 additions and 43 deletions

View File

@ -22,7 +22,11 @@ module Migration
SQL
end
def self.readonly_function_name(table_name, column_name = nil)
def self.drop_readonly_function(table_name, column_name = nil)
DB.exec("DROP FUNCTION IF EXISTS #{readonly_function_name(table_name, column_name)} CASCADE")
end
def self.readonly_function_name(table_name, column_name = nil, with_schema: true)
function_name = [
"raise",
table_name,
@ -30,12 +34,7 @@ module Migration
"readonly()"
].compact.join("_")
if DB.exec(<<~SQL).to_s == '1'
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name = '#{FUNCTION_SCHEMA_NAME}'
SQL
if with_schema && function_schema_exists?
"#{FUNCTION_SCHEMA_NAME}.#{function_name}"
else
function_name
@ -51,5 +50,21 @@ module Migration
def self.readonly_trigger_name(table_name, column_name = nil)
[table_name, column_name, "readonly"].compact.join("_")
end
def self.function_schema_exists?
DB.exec(<<~SQL).to_s == '1'
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name = '#{FUNCTION_SCHEMA_NAME}'
SQL
end
def self.existing_discourse_function_names
DB.query_single(<<~SQL)
SELECT routine_name
FROM information_schema.routines
WHERE routine_type = 'FUNCTION' AND specific_schema = '#{FUNCTION_SCHEMA_NAME}'
SQL
end
end
end