-
Notifications
You must be signed in to change notification settings - Fork 460
Updated database connection handling via pg_dump. #128
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
Open
vdragsic
wants to merge
6
commits into
influitive:development
Choose a base branch
from
vdragsic:development
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d7558ce
Updated database conn handling via pg_dump.
ed04352
Fixed pg_dump connection via socket.
214d169
Merge branch 'development' of github.com:influitive/apartment into de…
dbaa807
Excluded tables aren't copied to newly created schemas.
2757c99
Update apartment.gemspec
b55eafa
Merge branch 'development' of https://github.com/influitive/apartment…
vdragsic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -149,23 +149,34 @@ def copy_schema_migrations | |
| # | ||
| def pg_dump_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_tenant} #{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.) | ||
|
|
||
| `pg_dump -s -x -O -n #{default_tenant} #{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 #{default_tenant} #{dbname}` | ||
| 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 | ||
|
|
@@ -196,11 +207,30 @@ def collect_table_names(models) | |
| end | ||
| end | ||
|
|
||
| # Convenience method for current database name | ||
| # Build pg_dump command with host, dbname, user and password | ||
| # | ||
| def dbname | ||
| ActiveRecord::Base.connection_config[:database] | ||
| # @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' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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