mirror of
https://github.com/discourse/discourse.git
synced 2025-03-26 06:57:24 +08:00
FIX: Restore failed if schema contained objects not owned by the current DB user
This commit is contained in:
parent
0d646d1a26
commit
13b4eb9cce
lib
@ -83,12 +83,14 @@ module BackupRestore
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.move_tables_between_schemas(source, destination)
|
def self.move_tables_between_schemas(source, destination)
|
||||||
|
owner = database_configuration.username
|
||||||
|
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
DB.exec(move_tables_between_schemas_sql(source, destination))
|
DB.exec(move_tables_between_schemas_sql(source, destination, owner))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.move_tables_between_schemas_sql(source, destination)
|
def self.move_tables_between_schemas_sql(source, destination, owner)
|
||||||
<<~SQL
|
<<~SQL
|
||||||
DO $$DECLARE row record;
|
DO $$DECLARE row record;
|
||||||
BEGIN
|
BEGIN
|
||||||
@ -97,13 +99,13 @@ module BackupRestore
|
|||||||
-- otherwise extensions (like hstore & pg_trgm) won't work anymore...
|
-- otherwise extensions (like hstore & pg_trgm) won't work anymore...
|
||||||
CREATE SCHEMA IF NOT EXISTS #{destination};
|
CREATE SCHEMA IF NOT EXISTS #{destination};
|
||||||
-- move all <source> tables to <destination> schema
|
-- move all <source> tables to <destination> schema
|
||||||
FOR row IN SELECT tablename FROM pg_tables WHERE schemaname = '#{source}'
|
FOR row IN SELECT tablename FROM pg_tables WHERE schemaname = '#{source}' AND tableowner = '#{owner}'
|
||||||
LOOP
|
LOOP
|
||||||
EXECUTE 'DROP TABLE IF EXISTS #{destination}.' || quote_ident(row.tablename) || ' CASCADE;';
|
EXECUTE 'DROP TABLE IF EXISTS #{destination}.' || quote_ident(row.tablename) || ' CASCADE;';
|
||||||
EXECUTE 'ALTER TABLE #{source}.' || quote_ident(row.tablename) || ' SET SCHEMA #{destination};';
|
EXECUTE 'ALTER TABLE #{source}.' || quote_ident(row.tablename) || ' SET SCHEMA #{destination};';
|
||||||
END LOOP;
|
END LOOP;
|
||||||
-- move all <source> views to <destination> schema
|
-- move all <source> views to <destination> schema
|
||||||
FOR row IN SELECT viewname FROM pg_views WHERE schemaname = '#{source}'
|
FOR row IN SELECT viewname FROM pg_views WHERE schemaname = '#{source}' AND viewowner = '#{owner}'
|
||||||
LOOP
|
LOOP
|
||||||
EXECUTE 'DROP VIEW IF EXISTS #{destination}.' || quote_ident(row.viewname) || ' CASCADE;';
|
EXECUTE 'DROP VIEW IF EXISTS #{destination}.' || quote_ident(row.viewname) || ' CASCADE;';
|
||||||
EXECUTE 'ALTER VIEW #{source}.' || quote_ident(row.viewname) || ' SET SCHEMA #{destination};';
|
EXECUTE 'ALTER VIEW #{source}.' || quote_ident(row.viewname) || ' SET SCHEMA #{destination};';
|
||||||
|
@ -194,6 +194,7 @@ module BackupRestore
|
|||||||
[ password_argument, # pass the password to pg_dump (if any)
|
[ password_argument, # pass the password to pg_dump (if any)
|
||||||
"pg_dump", # the pg_dump command
|
"pg_dump", # the pg_dump command
|
||||||
"--schema=public", # only public schema
|
"--schema=public", # only public schema
|
||||||
|
"-T public.pg_*", # exclude tables and views whose name starts with "pg_"
|
||||||
"--file='#{@dump_filename}'", # output to the dump.sql file
|
"--file='#{@dump_filename}'", # output to the dump.sql file
|
||||||
"--no-owner", # do not output commands to set ownership of objects
|
"--no-owner", # do not output commands to set ownership of objects
|
||||||
"--no-privileges", # prevent dumping of access privileges
|
"--no-privileges", # prevent dumping of access privileges
|
||||||
|
Loading…
x
Reference in New Issue
Block a user