FIX: Restoring backup from PG12 could fail on PG10

The `EXECUTE FUNCTION` syntax for `CREATE TRIGGER` statements was introduced in PostgreSQL 11. We need to replace `EXECUTE FUNCTION` with `EXECUTE PROCEDURE` in order to be able to restore backups created with PG12 on PG10.
This commit is contained in:
Gerhard Schlager
2020-06-16 15:59:35 +02:00
parent 4cff4892e8
commit 859d9b75a7
4 changed files with 112 additions and 3 deletions

View File

@ -95,7 +95,8 @@ module BackupRestore
raise DatabaseRestoreError.new("psql failed: #{last_line}") if Process.last_status&.exitstatus != 0
end
# Removes unwanted SQL added by certain versions of pg_dump.
# Removes unwanted SQL added by certain versions of pg_dump and modifies
# the dump so that it works on the current version of PostgreSQL.
def sed_command
unwanted_sql = [
"DROP SCHEMA", # Discourse <= v1.5
@ -104,11 +105,15 @@ module BackupRestore
"SET default_table_access_method" # PostgreSQL 12
].join("|")
"sed -E '/^(#{unwanted_sql})/d'"
command = "sed -E '/^(#{unwanted_sql})/d' #{@db_dump_path}"
if BackupRestore.postgresql_major_version < 11
command = "#{command} | sed -E 's/^(CREATE TRIGGER.+EXECUTE) FUNCTION/\\1 PROCEDURE/'"
end
command
end
def restore_dump_command
"#{sed_command} #{@db_dump_path} | #{self.class.psql_command} 2>&1"
"#{sed_command} | #{self.class.psql_command} 2>&1"
end
def self.psql_command