From fb505f7b4057b4f8ef9b3a376d172f3c7b0af43b Mon Sep 17 00:00:00 2001 From: Brock Wilcox Date: Thu, 11 Jul 2024 16:56:11 -0400 Subject: [PATCH] Update prod db fetch to not use the DB redundantly (#4505) Without this it was connecting to the database which prevented it from doing the drop/create. This also removes a now-unneeded work-around for that issue --- lib/tasks/fetch_latest_db.rake | 24 +++++++++++++++++------- lib/tasks/kill_postgres_connections.rake | 16 ---------------- 2 files changed, 17 insertions(+), 23 deletions(-) delete mode 100644 lib/tasks/kill_postgres_connections.rake diff --git a/lib/tasks/fetch_latest_db.rake b/lib/tasks/fetch_latest_db.rake index c8f44aeeb9..fab4e2fd6a 100644 --- a/lib/tasks/fetch_latest_db.rake +++ b/lib/tasks/fetch_latest_db.rake @@ -2,8 +2,8 @@ desc "Update the development db to what is being used in prod" BACKUP_CONTAINER_NAME = 'backups' PASSWORD_REPLACEMENT = 'password' -task :fetch_latest_db => :environment do - if Rails.env.production? +task :fetch_latest_db do + if ENV["RAILS_ENV"] == "production" raise "You may not run this backup script in production!" end @@ -11,13 +11,14 @@ task :fetch_latest_db => :environment do puts "Recreating databases..." system("bin/rails db:environment:set RAILS_ENV=development") - system("rails db:drop db:create") + system("bin/rails db:drop db:create") puts "Restoring the database with #{backup.name}" backup_filepath = fetch_file_path(backup) db_username = ENV["PG_USERNAME"].presence || ENV["USER"].presence || "postgres" db_host = ENV["PG_HOST"].presence || "localhost" - system("pg_restore --clean --no-acl --no-owner -h #{db_host} -d diaper_dev -U #{db_username} #{backup_filepath}") + db_password = ENV["PG_PASSWORD"].presence + system("PGPASSWORD='#{db_password}' pg_restore --clean --no-acl --no-owner -h #{db_host} -d diaper_dev -U #{db_username} #{backup_filepath}") puts "Done!" @@ -30,14 +31,23 @@ task :fetch_latest_db => :environment do # environment. system("bin/rails jobs:clear") - ActiveRecord::Base.connection.reconnect! - puts "Replacing all the passwords with the replacement for ease of use: '#{PASSWORD_REPLACEMENT}'" - replace_user_passwords + system("bin/rails db:replace_user_passwords") puts "DONE!" end +namespace :db do + desc "Replace all user passwords with the replacement password" + task :replace_user_passwords => :environment do + if Rails.env.production? + raise "You may not run this backup script in production!" + end + + replace_user_passwords + end +end + private def fetch_latest_backups diff --git a/lib/tasks/kill_postgres_connections.rake b/lib/tasks/kill_postgres_connections.rake deleted file mode 100644 index 6cba38d3a9..0000000000 --- a/lib/tasks/kill_postgres_connections.rake +++ /dev/null @@ -1,16 +0,0 @@ -task :kill_postgres_connections => :environment do - puts "Killing existing Postgres connections - you may be prompted for your computer admin password" - db_name = Rails.configuration.database_configuration[Rails.env]['database'] - sh = < :kill_postgres_connections