From d7558ce631d38e73ed18acd5ab9e4050084c6acb Mon Sep 17 00:00:00 2001 From: Veljko Dragsic Date: Fri, 4 Apr 2014 16:57:28 +0200 Subject: [PATCH 1/4] Updated database conn handling via pg_dump. --- lib/apartment/adapters/postgresql_adapter.rb | 35 ++++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index cff418d5..b6102860 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -148,18 +148,15 @@ def copy_schema_migrations # @return {String} raw SQL contaning only postgres schema dump # def pg_dump_schema - dbname = ActiveRecord::Base.connection_config[:database] - default_schema = Apartment.default_schema - # Skip excluded tables? :/ # excluded_tables = # collect_table_names(Apartment.excluded_models) # .map! {|t| "-T #{t}"} # .join(' ') - # `pg_dump -s -x -O -n #{default_schema} #{excluded_tables} #{dbname}` + cmd = build_pg_dump "-s -x -O -n #{Apartment.default_schema}" - `pg_dump -s -x -O -n #{default_schema} #{dbname}` + `#{cmd}` end # Dump data from schema_migrations table @@ -167,7 +164,9 @@ def pg_dump_schema # @return {String} raw SQL contaning inserts with data from schema_migrations # def pg_dump_schema_migrations_data - `pg_dump -a --inserts -t schema_migrations -n #{Apartment.default_schema} bithub_development` + cmd = build_pg_dump "-a --inserts -t schema_migrations -n #{Apartment.default_schema}" + + `#{cmd}` end # Remove "SET search_path ..." line from SQL dump and prepend search_path set to current tenant @@ -198,8 +197,30 @@ def collect_table_names(models) end end - end + # Build pg_dump command with host, dbname, user and password + # + # @return {String} raw pg_dump command containg connection params and db name + # + def build_pg_dump(switches="") + # read database config + db_config = Rails.configuration.database_configuration.fetch Rails.env + db_name = db_config['database'] + db_host = db_config['socket'] || db_config['host'] || 'localhost' + db_port = db_config['port'] || "5432" + db_user = db_config['user'] + db_pwd = db_config['password'] + + # build command + cmd_env = db_pwd ? "PGPASSWORD=#{db_pwd}" : "" + cmd_host = "-h #{db_host}" + cmd_port = "-p #{db_port}" + cmd_user = db_user ? "-U #{db_user}" : "" + + "#{cmd_env} pg_dump #{cmd_host} #{cmd_user} #{switches} #{db_name}" + end + + end end end From ed0435277dd3b566d6d003a59d92adcaeaf4d843 Mon Sep 17 00:00:00 2001 From: Veljko Dragsic Date: Wed, 16 Apr 2014 01:46:37 +0200 Subject: [PATCH 2/4] Fixed pg_dump connection via socket. --- lib/apartment/adapters/postgresql_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index b6102860..ced7294a 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -206,7 +206,7 @@ def build_pg_dump(switches="") # read database config db_config = Rails.configuration.database_configuration.fetch Rails.env db_name = db_config['database'] - db_host = db_config['socket'] || db_config['host'] || 'localhost' + db_host = db_config['socket'] ? File.dirname(db_config['socket']) : db_config['host'] || 'localhost' db_port = db_config['port'] || "5432" db_user = db_config['user'] db_pwd = db_config['password'] From dbaa8073e878b31ac1d0dc7bd9ec8733ebdbb9b0 Mon Sep 17 00:00:00 2001 From: Veljko Dragsic Date: Thu, 8 May 2014 11:07:19 +0200 Subject: [PATCH 3/4] Excluded tables aren't copied to newly created schemas. --- lib/apartment/adapters/postgresql_adapter.rb | 22 ++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index ced7294a..4fd9da83 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -148,13 +148,23 @@ def copy_schema_migrations # @return {String} raw SQL contaning only postgres schema dump # def pg_dump_schema - # Skip excluded tables? :/ - # excluded_tables = - # collect_table_names(Apartment.excluded_models) - # .map! {|t| "-T #{t}"} - # .join(' ') - cmd = build_pg_dump "-s -x -O -n #{Apartment.default_schema}" + # Tables from excluded models aren't copied to newly created + # tenant/schema due to possible issue with foreign keys. + + # (issue occurs when FK on 'tableA' refrences 'tableB', 'tableA' is + # within tenant schema, 'tableB' is within excluded model in public + # schema. In that case FK on 'tenant.tableA' references to + # non-existing row in 'tenant.tableB', but should ref. to + # 'public.tableB', that's why tables from excluded models shouldn't + # be copied to tenant schemes.) + + excluded_tables = + collect_table_names(Apartment.excluded_models) + .map! {|t| "-T #{t}"} + .join(' ') + + cmd = build_pg_dump "-s -x -O #{excluded_tables} -n #{Apartment.default_schema}" `#{cmd}` end From 2757c99c4beea10b2341226ad4f94f88c84c8c02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Veljko=20Drag=C5=A1i=C4=87?= Date: Fri, 27 Jun 2014 16:06:14 +0200 Subject: [PATCH 4/4] Update apartment.gemspec --- apartment.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apartment.gemspec b/apartment.gemspec index e09b07d0..ddc3e6d9 100644 --- a/apartment.gemspec +++ b/apartment.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |s| s.homepage = %q{https://github.com/influitive/apartment} s.licenses = ["MIT"] - s.add_dependency 'activerecord', '>= 3.1.2', '< 4.1' # must be >= 3.1.2 due to bug in prepared_statements + s.add_dependency 'activerecord', '>= 3.1.2' # must be >= 3.1.2 due to bug in prepared_statements s.add_dependency 'rack', '>= 1.3.6' s.add_development_dependency 'appraisal'