diff --git a/lib/parity/backup.rb b/lib/parity/backup.rb index 853f594..423800a 100644 --- a/lib/parity/backup.rb +++ b/lib/parity/backup.rb @@ -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 @@ -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 diff --git a/spec/parity/backup_spec.rb b/spec/parity/backup_spec.rb index 0939d48..cb1724e 100644 --- a/spec/parity/backup_spec.rb +++ b/spec/parity/backup_spec.rb @@ -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) @@ -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