Skip to content

Commit

Permalink
Allow pre-2.2 Rubies to execute development restore
Browse files Browse the repository at this point in the history
In #143, we got a report that because `Etc.nprocessors` was not
implemented until Ruby 2.2 that Ruby 2.1 users were unable to execute
`development restore`, which leverages that method to determine how many
parallel processes to use when restoring the backup from the downloaded
dump.

This change implements a check to see if `Etc.nprocessors` is available.
In the event that it's not, we'll default to two parallel processes. We
will plan to sunset this change in July 2018, giving users on Ruby 2.1,
which has been EOLed, six months to move on.

Close #143.
  • Loading branch information
geoffharcourt committed Feb 2, 2018
1 parent af5807e commit 2119c50
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
14 changes: 13 additions & 1 deletion lib/parity/backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def download_remote_backup
def restore_from_local_temp_backup
Kernel.system(
"pg_restore tmp/latest.backup --verbose --clean --no-acl --no-owner "\
"--dbname #{development_db} --jobs #{Etc.nprocessors} "\
"--dbname #{development_db} --jobs #{processor_cores} "\
"#{additional_args}",
)
end
Expand Down Expand Up @@ -106,5 +106,17 @@ def development_db
def database_yaml_file
IO.read(DATABASE_YML_RELATIVE_PATH)
end

def processor_cores
if ruby_version_over_2_2?
Etc.nprocessors
else
2
end
end

def ruby_version_over_2_2?
Etc.respond_to?(:nprocessors)
end
end
end
28 changes: 26 additions & 2 deletions spec/parity/backup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@
with(delete_local_temp_backup_command)
end

it "restores backups to development with Rubies that do not support Etc.nprocessors" do
allow(IO).to receive(:read).and_return(database_fixture)
allow(Kernel).to receive(:system)
allow(Etc).to receive(:respond_to?).with(:nprocessors).and_return(false)

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: 2))
expect(Kernel).
to have_received(:system).
with(delete_local_temp_backup_command)
end

it "restores backups to staging from production" do
stub_heroku_app_name
allow(Kernel).to receive(:system)
Expand Down Expand Up @@ -101,9 +125,9 @@ def download_remote_database_command
'curl -o tmp/latest.backup "$(heroku pg:backups:url --remote production)"'
end

def restore_from_local_temp_backup_command
def restore_from_local_temp_backup_command(cores: number_of_processes)
"pg_restore tmp/latest.backup --verbose --clean --no-acl --no-owner "\
"--dbname #{default_db_name} --jobs #{number_of_processes} "
"--dbname #{default_db_name} --jobs #{cores} "
end

def number_of_processes
Expand Down

0 comments on commit 2119c50

Please sign in to comment.