update database rake tasks
This commit is contained in:
parent
c39cc5d240
commit
c99180b299
1 changed files with 21 additions and 6 deletions
|
|
@ -1,11 +1,14 @@
|
||||||
namespace :db do
|
namespace :db do
|
||||||
desc 'Backup remote PostgreSQL database'
|
desc 'Backup remote PostgreSQL database'
|
||||||
task :backup do
|
task :backup do
|
||||||
remote_host = ENV.fetch('REMOTE_DB_HOST', 'roundhouse.proxy.rlwy.net')
|
db_url = ENV.fetch('REMOTE_DB_URL') { raise 'Please set REMOTE_DB_URL' }
|
||||||
remote_port = ENV.fetch('REMOTE_DB_PORT', '54629')
|
uri = URI.parse(db_url)
|
||||||
remote_user = ENV.fetch('REMOTE_DB_USER', 'postgres')
|
|
||||||
remote_db = ENV.fetch('REMOTE_DB_NAME', 'railway')
|
remote_host = uri.host
|
||||||
password = ENV.fetch('REMOTE_DB_PASSWORD') { raise 'Please set REMOTE_DB_PASSWORD' }
|
remote_port = uri.port
|
||||||
|
remote_user = uri.user
|
||||||
|
remote_db = uri.path.delete_prefix('/')
|
||||||
|
password = uri.password
|
||||||
|
|
||||||
backup_dir = File.expand_path('backups')
|
backup_dir = File.expand_path('backups')
|
||||||
FileUtils.mkdir_p(backup_dir)
|
FileUtils.mkdir_p(backup_dir)
|
||||||
|
|
@ -21,7 +24,7 @@ namespace :db do
|
||||||
puts 'Backup completed!'
|
puts 'Backup completed!'
|
||||||
end
|
end
|
||||||
|
|
||||||
desc 'Restore PostgreSQL database from backup'
|
desc 'Restore PostgreSQL database from backup (use CLEAN=1 to drop and recreate first)'
|
||||||
task :restore, [:backup_file] => [:environment] do |_, args|
|
task :restore, [:backup_file] => [:environment] do |_, args|
|
||||||
local_user = ENV.fetch('LOCAL_DB_USER', 'justin')
|
local_user = ENV.fetch('LOCAL_DB_USER', 'justin')
|
||||||
local_db = ENV.fetch('LOCAL_DB_NAME', 'hensei_dev')
|
local_db = ENV.fetch('LOCAL_DB_NAME', 'hensei_dev')
|
||||||
|
|
@ -32,6 +35,18 @@ namespace :db do
|
||||||
|
|
||||||
raise 'Backup file not found. Please specify a valid backup file.' unless backup_file && File.exist?(backup_file)
|
raise 'Backup file not found. Please specify a valid backup file.' unless backup_file && File.exist?(backup_file)
|
||||||
|
|
||||||
|
if ENV['CLEAN']
|
||||||
|
puts 'Dropping and recreating database...'
|
||||||
|
ActiveRecord::Base.connection.execute(<<~SQL)
|
||||||
|
SELECT pg_terminate_backend(pid)
|
||||||
|
FROM pg_stat_activity
|
||||||
|
WHERE datname = '#{local_db}' AND pid <> pg_backend_pid()
|
||||||
|
SQL
|
||||||
|
ActiveRecord::Base.connection.disconnect!
|
||||||
|
Rake::Task['db:drop'].invoke
|
||||||
|
Rake::Task['db:create'].invoke
|
||||||
|
end
|
||||||
|
|
||||||
puts "Restoring database from #{backup_file}..."
|
puts "Restoring database from #{backup_file}..."
|
||||||
system("pg_restore --no-owner --role=#{local_user} --disable-triggers -U #{local_user} -d #{local_db} #{backup_file}")
|
system("pg_restore --no-owner --role=#{local_user} --disable-triggers -U #{local_user} -d #{local_db} #{backup_file}")
|
||||||
puts 'Restore completed!'
|
puts 'Restore completed!'
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue