Created
October 5, 2023 13:17
-
-
Save cjavdev/8c1126c0546749bb70cab52fc1fdc2af to your computer and use it in GitHub Desktop.
Handy little rake task for dumping prod db locally then redacting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :dump do | |
task db: :environment do | |
if !Rails.env.development? | |
raise "This task is only available in development environment" | |
end | |
puts "Removing potential existing backups..." | |
if File.directory?("/tmp/backups") | |
FileUtils.rm_rf("/tmp/backups") | |
end | |
puts "Creating a folder to handle backups" | |
FileUtils.mkdir_p("/tmp/backups") | |
Dir.chdir("/tmp/backups") | |
puts "Backuping remote Render db..." | |
system("pg_dump -d #{Rails.application.credentials.prod_db_connection_string} > dump.sql") | |
puts "Dropping local db..." | |
system("dropdb my_app_development") | |
puts "Recreating local db..." | |
system("createdb my_app_development") | |
puts "Restoring local db..." | |
system("psql -d my_app_development -f dump.sql") | |
puts "Done. !! Be sure to run `bin/rails dump:redact` next!!" | |
# Alt, always require redact | |
# Rake::Task["dump:redact"].invoke | |
end | |
task redact: :environment do | |
raise "This task is not for production" if Rails.env.production? | |
puts "Redacting users..." | |
User.all.each do |user| | |
next if user.email == "[email protected]" | |
user.update( | |
email: Faker::Internet.email, | |
first_name: Faker::Name.first_name, | |
last_name: Faker::Name.last_name, | |
phone: Faker::PhoneNumber.phone_number | |
) | |
end | |
puts "Redacting accounts..." | |
Account.all.each do |account| | |
account.update( | |
name: Faker::TvShows::SiliconValley.company | |
) | |
end | |
puts "Redacting addresses..." | |
Address.all.each do |address| | |
address.update( | |
line1: Faker::Address.street_address, | |
city: Faker::Address.city, | |
state: Faker::Address.state, | |
postal_code: Faker::Address.zip | |
) | |
end | |
puts "Redacting projects..." | |
Project.all.each do |project| | |
project.update( | |
title: project.project_type.titleize | |
) | |
end | |
puts "Redacting invoices..." | |
Invoice.all.each do |invoice| | |
invoice.update( | |
total: rand(750_00..30_000_00) | |
) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment