Skip to content

Commit

Permalink
Update prod db fetch to not use the DB redundantly (#4505)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
awwaiid committed Jul 11, 2024
1 parent 7c0f000 commit fb505f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
24 changes: 17 additions & 7 deletions lib/tasks/fetch_latest_db.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ 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

backup = fetch_latest_backups

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!"

Expand All @@ -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
Expand Down
16 changes: 0 additions & 16 deletions lib/tasks/kill_postgres_connections.rake

This file was deleted.

0 comments on commit fb505f7

Please sign in to comment.