Skip to content

Instantly share code, notes, and snippets.

@Zeko369
Created May 1, 2024 10:34
Show Gist options
  • Select an option

  • Save Zeko369/dfd0528e5f555d58a60c71791eccc2d4 to your computer and use it in GitHub Desktop.

Select an option

Save Zeko369/dfd0528e5f555d58a60c71791eccc2d4 to your computer and use it in GitHub Desktop.
if ARGV.any? { |arg| %w[db:drop db:reset db:setup].include?(arg) } && ENV['YES_DROP'] != 'true'
# this is wrapped in a method for easier early returns
def check_db_drop
require 'dotenv'
require 'json'
env = Dotenv.parse('.env')
db_url = env['DATABASE_URL']
unless db_url.nil?
db_name = db_url.split('/').last
if !env['SAFE_TO_DROP'].nil?
if JSON.parse(env['SAFE_TO_DROP']).include?(db_name)
puts "Database \"#{db_name}\" is safe to drop, skipping confirmation"
return
else
puts "WARNING: Database \"#{db_name}\" is not in the safe drop list"
end
else
puts "WARNING: SAFE_TO_DROP is not set in .env, set it to an array of DB names that's safe to drop"
end
end
database_name = db_name || '[database from credentials]'
print "WARNING: This command will drop the #{database_name}. Are you sure you want to continue? [Y/n]:"
$stdout.flush
key = $stdin.gets.chomp
abort unless key.downcase == 'y' || key == '' # we also accept enter here since [Y/n] means yes by default
end
check_db_drop
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment