class Parity::Backup
Constants
- BLANK_ARGUMENTS
- DATABASE_KEY_NAME
- DATABASE_YML_RELATIVE_PATH
- DEVELOPMENT_ENVIRONMENT_KEY_NAME
Attributes
additional_args[R]
from[R]
parallelize[R]
parallelize?[R]
to[R]
Public Class Methods
new(args)
click to toggle source
# File lib/parity/backup.rb, line 10 def initialize(args) @from, @to = args.values_at(:from, :to) @additional_args = args[:additional_args] || BLANK_ARGUMENTS @parallelize = args[:parallelize] || false end
Public Instance Methods
restore()
click to toggle source
# File lib/parity/backup.rb, line 16 def restore if to == DEVELOPMENT_ENVIRONMENT_KEY_NAME restore_to_development elsif from == DEVELOPMENT_ENVIRONMENT_KEY_NAME restore_from_development else restore_to_remote_environment end end
Private Instance Methods
backup_from()
click to toggle source
# File lib/parity/backup.rb, line 102 def backup_from "`#{remote_db_backup_url}` DATABASE" end
database_yaml_file()
click to toggle source
# File lib/parity/backup.rb, line 116 def database_yaml_file ERB.new(IO.read(DATABASE_YML_RELATIVE_PATH)).result end
delete_local_temp_backup()
click to toggle source
# File lib/parity/backup.rb, line 84 def delete_local_temp_backup Kernel.system("rm tmp/latest.backup") end
delete_rails_production_environment_settings()
click to toggle source
# File lib/parity/backup.rb, line 88 def delete_rails_production_environment_settings Kernel.system(<<-SHELL) psql #{development_db} -c "CREATE TABLE IF NOT EXISTS public.ar_internal_metadata (key character varying NOT NULL, value character varying, created_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL, CONSTRAINT ar_internal_metadata_pkey PRIMARY KEY (key)); UPDATE ar_internal_metadata SET value = 'development' WHERE key = 'environment'" SHELL end
development_db()
click to toggle source
# File lib/parity/backup.rb, line 110 def development_db YAML.safe_load(database_yaml_file, aliases: true). fetch(DEVELOPMENT_ENVIRONMENT_KEY_NAME). fetch(DATABASE_KEY_NAME) end
download_remote_backup()
click to toggle source
# File lib/parity/backup.rb, line 70 def download_remote_backup Kernel.system( "curl -o tmp/latest.backup \"$(heroku pg:backups:url --remote #{from})\"", ) end
ensure_temp_directory_exists()
click to toggle source
# File lib/parity/backup.rb, line 66 def ensure_temp_directory_exists Kernel.system("mkdir -p tmp") end
heroku_app_name()
click to toggle source
# File lib/parity/backup.rb, line 62 def heroku_app_name HerokuAppName.new(to).to_s end
processor_cores()
click to toggle source
# File lib/parity/backup.rb, line 120 def processor_cores if parallelize? && ruby_version_over_2_2? Etc.nprocessors else 1 end end
remote_db_backup_url()
click to toggle source
# File lib/parity/backup.rb, line 106 def remote_db_backup_url "heroku pg:backups:url --remote #{from}" end
reset_remote_database()
click to toggle source
# File lib/parity/backup.rb, line 55 def reset_remote_database Kernel.system( "heroku pg:reset --remote #{to} #{additional_args} "\ "--confirm #{heroku_app_name}", ) end
restore_from_development()
click to toggle source
# File lib/parity/backup.rb, line 32 def restore_from_development reset_remote_database Kernel.system( "heroku pg:push #{development_db} DATABASE_URL --remote #{to} "\ "#{additional_args}", ) end
restore_from_local_temp_backup()
click to toggle source
# File lib/parity/backup.rb, line 76 def restore_from_local_temp_backup Kernel.system( "pg_restore tmp/latest.backup --verbose --no-acl --no-owner "\ "--dbname #{development_db} --jobs=#{processor_cores} "\ "#{additional_args}", ) end
restore_to_development()
click to toggle source
# File lib/parity/backup.rb, line 40 def restore_to_development ensure_temp_directory_exists download_remote_backup wipe_development_database restore_from_local_temp_backup delete_local_temp_backup delete_rails_production_environment_settings end
restore_to_remote_environment()
click to toggle source
# File lib/parity/backup.rb, line 94 def restore_to_remote_environment reset_remote_database Kernel.system( "heroku pg:backups:restore #{backup_from} --remote #{to} "\ "#{additional_args}", ) end
ruby_version_over_2_2?()
click to toggle source
# File lib/parity/backup.rb, line 128 def ruby_version_over_2_2? Etc.respond_to?(:nprocessors) end
wipe_development_database()
click to toggle source
# File lib/parity/backup.rb, line 49 def wipe_development_database Kernel.system( "dropdb --if-exists #{development_db} && createdb #{development_db}", ) end