diff --git a/lib/parity/backup.rb b/lib/parity/backup.rb index ba9e244..c18a36e 100644 --- a/lib/parity/backup.rb +++ b/lib/parity/backup.rb @@ -40,6 +40,7 @@ def restore_to_development wipe_development_database restore_from_local_temp_backup delete_local_temp_backup + delete_rails_production_environment_settings end def wipe_development_database @@ -81,6 +82,17 @@ def delete_local_temp_backup Kernel.system("rm tmp/latest.backup") end + def delete_rails_production_environment_settings + Kernel.system( + "psql #{development_db} -c "\ + "\"DO $$ BEGIN IF EXISTS "\ + "(SELECT 1 FROM pg_tables WHERE tablename = 'ar_internal_metadata') "\ + "THEN UPDATE ar_internal_metadata "\ + "SET value = 'development' "\ + "WHERE key = 'environment'; ELSE END IF; END $$;\"", + ) + end + def restore_to_remote_environment reset_remote_database Kernel.system( diff --git a/spec/parity/backup_spec.rb b/spec/parity/backup_spec.rb index fe47d9b..f0fba93 100644 --- a/spec/parity/backup_spec.rb +++ b/spec/parity/backup_spec.rb @@ -1,52 +1,79 @@ require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'parity') describe Parity::Backup do - it "restores backups to development (after dropping the development DB)" do - allow(IO).to receive(:read).and_return(database_fixture) - allow(Kernel).to receive(:system) - allow(Etc).to receive(:nprocessors).and_return(number_of_processes) - - 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) - expect(Kernel). - to have_received(:system). - 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) + context "restoring to the local development environment" do + it "restores backups to development (after dropping the development DB)" do + allow(IO).to receive(:read).and_return(database_fixture) + allow(Kernel).to receive(:system) + allow(Etc).to receive(:nprocessors).and_return(number_of_processes) + + 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) + expect(Kernel). + to have_received(:system). + 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 "drops the 'ar_internal_metadata' table if it exists" do + allow(IO).to receive(:read).and_return(database_fixture) + allow(Kernel).to receive(:system) + allow(Etc).to receive(:nprocessors).and_return(number_of_processes) + + 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) + expect(Kernel). + to have_received(:system). + with(delete_local_temp_backup_command) + expect(Kernel).to have_received(:system).with(set_db_metadata_sql) + end end it "restores backups to staging from production" do @@ -156,4 +183,13 @@ def additional_argument_pass_through def default_db_name "parity_development" end + + def set_db_metadata_sql + "psql parity_development -c "\ + "\"DO $$ BEGIN IF EXISTS "\ + "(SELECT 1 FROM pg_tables WHERE tablename = 'ar_internal_metadata') "\ + "THEN UPDATE ar_internal_metadata "\ + "SET value = 'development' "\ + "WHERE key = 'environment'; ELSE END IF; END $$;\"" + end end