mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 00:41:16 +08:00
BUGFIX: restore wasn't working now that we have some views
This commit is contained in:
@ -76,23 +76,25 @@ module BackupRestore
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.move_tables_between_schemas_sql(source, destination)
|
def self.move_tables_between_schemas_sql(source, destination)
|
||||||
# TODO: Postgres 9.3 has "CREATE SCHEMA schema IF NOT EXISTS;"
|
|
||||||
<<-SQL
|
<<-SQL
|
||||||
DO $$DECLARE row record;
|
DO $$DECLARE row record;
|
||||||
BEGIN
|
BEGIN
|
||||||
-- create <destination> schema if it does not exists already
|
-- create <destination> schema if it does not exists already
|
||||||
-- NOTE: DROP & CREATE SCHEMA is easier, but we don't want to drop the public schema
|
-- NOTE: DROP & CREATE SCHEMA is easier, but we don't want to drop the public schema
|
||||||
-- ortherwise extensions (like hstore & pg_trgm) won't work anymore...
|
-- ortherwise extensions (like hstore & pg_trgm) won't work anymore...
|
||||||
IF NOT EXISTS(SELECT 1 FROM pg_namespace WHERE nspname = '#{destination}')
|
CREATE SCHEMA IF NOT EXISTS #{destination};
|
||||||
THEN
|
|
||||||
CREATE SCHEMA #{destination};
|
|
||||||
END IF;
|
|
||||||
-- 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}'
|
||||||
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
|
||||||
|
FOR row IN SELECT viewname FROM pg_views WHERE schemaname = '#{source}'
|
||||||
|
LOOP
|
||||||
|
EXECUTE 'DROP VIEW IF EXISTS #{destination}.' || quote_ident(row.viewname) || ' CASCADE;';
|
||||||
|
EXECUTE 'ALTER VIEW #{source}.' || quote_ident(row.viewname) || ' SET SCHEMA #{destination};';
|
||||||
|
END LOOP;
|
||||||
END$$;
|
END$$;
|
||||||
SQL
|
SQL
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user