From f900cbec32010934bffc863006bc266a95dbc3cd Mon Sep 17 00:00:00 2001 From: Alexandria | Aster <92374839+alexandria-gfsc@users.noreply.github.com> Date: Tue, 15 Mar 2022 16:57:45 +0000 Subject: [PATCH] Fix rake db:* tasks reference to production database; Add direct synchronisation task (#1059) --- lib/tasks/db.rake | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index b6bbe678a..c1667f26c 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -141,6 +141,32 @@ namespace :db do DB_DUMP_SSH_URL = 'DB_DUMP_SSH_URL' DB_DUMP_STAGING_SSH_URL = 'DB_DUMP_STAGING_SSH_URL' + desc "Synchronize staging with the production database in one fell swoop" + task sync_prod_staging: :environment do + + prod_ssh_url = if ENV[DB_DUMP_SSH_URL] + ENV[DB_DUMP_SSH_URL] + else + 'root@placecal.org -p 666' + end + + ssh_url = if ENV[DB_DUMP_STAGING_SSH_URL] + ENV[DB_DUMP_STAGING_SSH_URL] + else + 'root@placecal-staging.org -p 666' + end + + $stdout.puts "Backing up staging db (May take a while.) ..." + puts `ssh #{ssh_url} dokku postgres:export placecal-db > $(date -Im)_placecal-staging.sql` + $stdout.puts "Replicating production db to staging db (May take a while.) ..." + puts `ssh #{prod_ssh_url} dokku postgres:export placecal-db2 | ssh #{ssh_url} dokku postgres:import placecal-db` + if $?.success? + $stdout.puts "Replicated production to staging (you might have to run rails db:migrate in dokku?)" + else + $stderr.puts "Failed to replicate production to staging!" + end + end + desc "Download production DB dump" task dump_production: :environment do @@ -157,7 +183,7 @@ namespace :db do end $stdout.puts "Downloading production db to #{filename} (May take a while.) ..." - puts `ssh #{ssh_url} dokku postgres:export placecal-db > #{filename}` + puts `ssh #{ssh_url} dokku postgres:export placecal-db2 > #{filename}` if $?.success? $stdout.puts "Downloaded production db to #{filename}" ENV[DB_DUMP_ENV_KEY] = filename