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

Add support for 3-tier database configurations on development #182

Merged
Show file tree
Hide file tree
Changes from all 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ heroku git:remote -r staging -a your-staging-app
heroku git:remote -r production -a your-production-app
```
* There is a `config/database.yml` file that can be parsed as YAML for
`['development']['database']`.
`['development']['database']` or, alternatively, `['development']['primary']['database']` for multi-database configurations.

Pipelines
---------
Expand Down
13 changes: 10 additions & 3 deletions lib/parity/backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Backup
BLANK_ARGUMENTS = "".freeze
DATABASE_YML_RELATIVE_PATH = "config/database.yml".freeze
DEVELOPMENT_ENVIRONMENT_KEY_NAME = "development".freeze
PRIMARY_DATABASE_KEY_NAME = "primary".freeze
DATABASE_KEY_NAME = "database".freeze

def initialize(args)
Expand Down Expand Up @@ -108,9 +109,15 @@ def remote_db_backup_url
end

def development_db
YAML.load(database_yaml_file).
fetch(DEVELOPMENT_ENVIRONMENT_KEY_NAME).
fetch(DATABASE_KEY_NAME)
db_configuration = YAML.safe_load(database_yaml_file, aliases: true).
fetch(DEVELOPMENT_ENVIRONMENT_KEY_NAME)

if db_configuration.key?(PRIMARY_DATABASE_KEY_NAME)
db_configuration[PRIMARY_DATABASE_KEY_NAME].
fetch(DATABASE_KEY_NAME)
else
db_configuration.fetch(DATABASE_KEY_NAME)
end
end

def database_yaml_file
Expand Down
8 changes: 8 additions & 0 deletions spec/fixtures/database_three_tier.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
development:
primary:
database: parity_development
username: user
primary_replica:
database: parity_development
username: user_readonly
replica: true
30 changes: 30 additions & 0 deletions spec/parity/backup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,32 @@
to have_received(:system).
with(delete_local_temp_backup_command)
end

it "is able to load a database.yml file with 3-tier configuration" do
allow(IO).to receive(:read).and_return(database_fixture_three_tier)
allow(Kernel).to receive(:system)

Parity::Backup.new(
from: "production",
to: "development",
).restore

expect(Kernel).
to have_received(:system).
with(make_temp_directory_command)
expect(Kernel).
to have_received(:system).
with(download_remote_database_command)
expect(Kernel).
to have_received(:system).
with(drop_development_database_drop_command)
expect(Kernel).
to have_received(:system).
with(restore_from_local_temp_backup_command(cores: 1))
expect(Kernel).
to have_received(:system).
with(delete_local_temp_backup_command)
end
end

it "restores backups to staging from production" do
Expand Down Expand Up @@ -231,6 +257,10 @@ def database_fixture_with_erb
IO.read(fixture_path("database_with_erb.yml"))
end

def database_fixture_three_tier
IO.read(fixture_path("database_three_tier.yml"))
end

def fixture_path(filename)
File.join(File.dirname(__FILE__), "..", "fixtures", filename)
end
Expand Down