Skip to content

Commit 2119c50

Browse files
committed
Allow pre-2.2 Rubies to execute development restore
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.
1 parent af5807e commit 2119c50

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

lib/parity/backup.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def download_remote_backup
7272
def restore_from_local_temp_backup
7373
Kernel.system(
7474
"pg_restore tmp/latest.backup --verbose --clean --no-acl --no-owner "\
75-
"--dbname #{development_db} --jobs #{Etc.nprocessors} "\
75+
"--dbname #{development_db} --jobs #{processor_cores} "\
7676
"#{additional_args}",
7777
)
7878
end
@@ -106,5 +106,17 @@ def development_db
106106
def database_yaml_file
107107
IO.read(DATABASE_YML_RELATIVE_PATH)
108108
end
109+
110+
def processor_cores
111+
if ruby_version_over_2_2?
112+
Etc.nprocessors
113+
else
114+
2
115+
end
116+
end
117+
118+
def ruby_version_over_2_2?
119+
Etc.respond_to?(:nprocessors)
120+
end
109121
end
110122
end

spec/parity/backup_spec.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@
2525
with(delete_local_temp_backup_command)
2626
end
2727

28+
it "restores backups to development with Rubies that do not support Etc.nprocessors" do
29+
allow(IO).to receive(:read).and_return(database_fixture)
30+
allow(Kernel).to receive(:system)
31+
allow(Etc).to receive(:respond_to?).with(:nprocessors).and_return(false)
32+
33+
Parity::Backup.new(from: "production", to: "development").restore
34+
35+
expect(Kernel).
36+
to have_received(:system).
37+
with(make_temp_directory_command)
38+
expect(Kernel).
39+
to have_received(:system).
40+
with(download_remote_database_command)
41+
expect(Kernel).
42+
to have_received(:system).
43+
with(drop_development_database_drop_command)
44+
expect(Kernel).
45+
to have_received(:system).
46+
with(restore_from_local_temp_backup_command(cores: 2))
47+
expect(Kernel).
48+
to have_received(:system).
49+
with(delete_local_temp_backup_command)
50+
end
51+
2852
it "restores backups to staging from production" do
2953
stub_heroku_app_name
3054
allow(Kernel).to receive(:system)
@@ -101,9 +125,9 @@ def download_remote_database_command
101125
'curl -o tmp/latest.backup "$(heroku pg:backups:url --remote production)"'
102126
end
103127

104-
def restore_from_local_temp_backup_command
128+
def restore_from_local_temp_backup_command(cores: number_of_processes)
105129
"pg_restore tmp/latest.backup --verbose --clean --no-acl --no-owner "\
106-
"--dbname #{default_db_name} --jobs #{number_of_processes} "
130+
"--dbname #{default_db_name} --jobs #{cores} "
107131
end
108132

109133
def number_of_processes

0 commit comments

Comments
 (0)