Skip to content

Commit

Permalink
feat(backups): add support for multi-database configurations
Browse files Browse the repository at this point in the history
Rails has multiple database support since 6.0, but as Parity doesn't
parse three-tier database configuration files correctly, it still
doesn't work on Rails applications set up in this way.

This change detects primary configurations. If it finds one, then it
will use it while restoring a backup. Else, it will default to the
first available configuration.
  • Loading branch information
horacio committed Oct 11, 2021
1 parent 81893f7 commit ede6e25
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
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

0 comments on commit ede6e25

Please sign in to comment.