Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated database connection handling via pg_dump. #128

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
Changes from 2 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
35 changes: 28 additions & 7 deletions lib/apartment/adapters/postgresql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,26 +148,25 @@ 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
#
# @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 +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'] ? File.dirname(db_config['socket']) : db_config['host'] || 'localhost'

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