Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apartment.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
53 changes: 42 additions & 11 deletions lib/apartment/adapters/postgresql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,26 +148,35 @@ 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(' ')
# Tables from excluded models aren't copied to newly created
# tenant/schema due to possible issue with foreign keys.

# `pg_dump -s -x -O -n #{default_schema} #{excluded_tables} #{dbname}`
# (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.)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made an alternative to this: #182


`pg_dump -s -x -O -n #{default_schema} #{dbname}`
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

# Dump data from schema_migrations table
#
# @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
Expand Down Expand Up @@ -198,8 +207,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'] ? File.dirname(db_config['socket']) : db_config['host'] || 'localhost'
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [108/80]

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