From be940e6c80286577df9145140aa95ff793c24caf Mon Sep 17 00:00:00 2001 From: NielsKSchjoedt Date: Tue, 25 Nov 2014 18:31:16 +0100 Subject: [PATCH 1/4] Remove CREATE SCHEMA clause from structure dump in postgresql --- lib/apartment/adapters/postgresql_adapter.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index e10bb1f6..9c8104de 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -119,7 +119,8 @@ class PostgresqlSchemaFromSqlAdapter < PostgresqlSchemaAdapter PSQL_DUMP_BLACKLISTED_STATEMENTS= [ /SET search_path/i, # overridden later - /SET lock_timeout/i # new in postgresql 9.3 + /SET lock_timeout/i, # new in postgresql 9.3 + /CREATE SCHEMA/i # remove create schema clause ] def import_database_schema From db0321dbae30af4f80bb9c968cc3076af4a2828f Mon Sep 17 00:00:00 2001 From: NielsKSchjoedt Date: Tue, 25 Nov 2014 18:32:15 +0100 Subject: [PATCH 2/4] Fix foreign key constrains not pointing to default_schema for excluded models --- lib/apartment/adapters/postgresql_adapter.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index 9c8104de..62b11401 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -175,14 +175,31 @@ def pg_dump_schema_migrations_data # def patch_search_path(sql) search_path = "SET search_path = #{self.current_tenant}, #{Apartment.default_schema};" - + excluded_tables = Apartment.excluded_models.map{ |model| model.constantize.table_name }.uniq sql .split("\n") + .map { |line| format_foreign_key_constraints(line, excluded_tables) } .select {|line| check_input_against_regexps(line, PSQL_DUMP_BLACKLISTED_STATEMENTS).empty?} .prepend(search_path) .join("\n") end + # Append default_schema to FK definitions, for tables belonging to excluded_models + # + def format_foreign_key_constraints line, excluded_tables + if line.match(/ADD CONSTRAINT .* FOREIGN KEY/) + table_name = line[/REFERENCES (.*)\(/, 1] + new_table_name = "#{Apartment.default_schema}.#{table_name}" + if excluded_tables.include?(new_table_name) + line.gsub("REFERENCES #{table_name}", "REFERENCES #{new_table_name}") + else + line + end + else + line + end + end + # Checks if any of regexps matches against input # def check_input_against_regexps(input, regexps) From def55af5d51b54559642c7459530544e0913903b Mon Sep 17 00:00:00 2001 From: NielsKSchjoedt Date: Tue, 25 Nov 2014 18:31:16 +0100 Subject: [PATCH 3/4] Remove CREATE SCHEMA clause from structure dump in postgresql --- lib/apartment/adapters/postgresql_adapter.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index e10bb1f6..9c8104de 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -119,7 +119,8 @@ class PostgresqlSchemaFromSqlAdapter < PostgresqlSchemaAdapter PSQL_DUMP_BLACKLISTED_STATEMENTS= [ /SET search_path/i, # overridden later - /SET lock_timeout/i # new in postgresql 9.3 + /SET lock_timeout/i, # new in postgresql 9.3 + /CREATE SCHEMA/i # remove create schema clause ] def import_database_schema From b549a2a099770015eb2cb700ed7e8c06c0bdae3e Mon Sep 17 00:00:00 2001 From: NielsKSchjoedt Date: Tue, 25 Nov 2014 18:32:15 +0100 Subject: [PATCH 4/4] Fix foreign key constrains not pointing to default_schema for excluded models --- lib/apartment/adapters/postgresql_adapter.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index 9c8104de..62b11401 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -175,14 +175,31 @@ def pg_dump_schema_migrations_data # def patch_search_path(sql) search_path = "SET search_path = #{self.current_tenant}, #{Apartment.default_schema};" - + excluded_tables = Apartment.excluded_models.map{ |model| model.constantize.table_name }.uniq sql .split("\n") + .map { |line| format_foreign_key_constraints(line, excluded_tables) } .select {|line| check_input_against_regexps(line, PSQL_DUMP_BLACKLISTED_STATEMENTS).empty?} .prepend(search_path) .join("\n") end + # Append default_schema to FK definitions, for tables belonging to excluded_models + # + def format_foreign_key_constraints line, excluded_tables + if line.match(/ADD CONSTRAINT .* FOREIGN KEY/) + table_name = line[/REFERENCES (.*)\(/, 1] + new_table_name = "#{Apartment.default_schema}.#{table_name}" + if excluded_tables.include?(new_table_name) + line.gsub("REFERENCES #{table_name}", "REFERENCES #{new_table_name}") + else + line + end + else + line + end + end + # Checks if any of regexps matches against input # def check_input_against_regexps(input, regexps)