Last active
March 6, 2018 05:02
-
-
Save rosskevin/7549349 to your computer and use it in GitHub Desktop.
My db.rake with multi-environment db:rebuild and db:truncate. Some strange behavior in ActiveRecord `database_tasks.rb` adds to the confusion. See this StackOverflow post in the comment.
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 :db do | |
# Rake::Task['db:load_config'].enhance [:environment] | |
desc 'Truncate all tables' | |
task :truncate => :environment do | |
puts 'Truncating: ' | |
ActiveRecord::Base.connection.tables.each do |table| | |
unless %w(schema_migrations rails_admin_histories).include? table | |
t = table.classify | |
puts "\t#{t}" | |
ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table};") | |
#t = t.constantize # this had problems on gem-based models i.e. tagging | |
#t = t.destroy_all | |
end | |
end | |
end | |
desc 'Rebuild dev and test' | |
task :rebuild, [:parallel] do |t, args| | |
rebuild_env(:development, args) | |
#print_tables() | |
rebuild_env(:test) | |
#print_tables() | |
end | |
desc 'Rebuild dev' | |
task :rebuild_dev, [:parallel] do |t, args| | |
rebuild_env(:development, args) | |
end | |
desc 'Rebuild test' | |
task :rebuild_test, [:parallel] do |t, args| | |
rebuild_env(:test, args) | |
end | |
desc 'Rebuild staging' | |
task :rebuild_staging do | |
rebuild_env(:staging) | |
end | |
private | |
def current_db | |
begin | |
ActiveRecord::Base.connection.current_database | |
rescue | |
end | |
end | |
def assert_correct_db | |
env = Rails.env | |
db = current_db | |
puts "current database[#{db}] for env[#{env}]" | |
correct = false | |
if Rails.env.test? | |
correct = db.include? '_test' | |
elsif Rails.env.development? | |
correct = db.include? '_dev' | |
elsif Rails.env.staging? | |
correct = db.include? '_stag' | |
else | |
raise "Unknown db[#{db}] to match env[#{env}]" | |
end | |
raise "Unexpected database[#{db}] for env[#{env}]" unless correct | |
#puts "Correct database[#{db}] for env[#{env}]" | |
end | |
def print_tables | |
puts "\n\tTables in [#{current_db()}]:" | |
puts "\t-----------------------------" | |
connection = ActiveRecord::Base.connection | |
#ap connection | |
#puts "current database[#{connection.current_database}]" | |
connection.tables.each do |table| | |
t = table.classify | |
puts "\t\t#{t}" | |
end | |
#puts '----------------------------------------------------------' | |
end | |
def rebuild_env(env, args=nil) | |
parallel = false | |
if !args.nil? | |
parallel = 'true'.eql? args[:parallel] | |
end | |
set_env(env) | |
puts "\n------------------------------------------------------------------------------------------------------------" | |
# Parallel | |
if parallel && (env.eql? :test) | |
puts "Rebuilding the [#{env}] parallel environments..." | |
begin | |
run('parallel:drop') | |
rescue | |
end | |
run('parallel:create') | |
run('parallel:migrate') | |
puts "\nDone with the [#{env}] parallel environments." | |
else | |
puts "Rebuilding the [#{env}] environment..." | |
run('db:drop') | |
# assert_correct_db() | |
run('db:create') | |
assert_correct_db() | |
run('db:migrate') | |
print_tables() | |
assert_correct_db() | |
run('db:seed') | |
assert_correct_db() | |
# these do not work in parallel | |
puts "\nDone with [#{current_db()}] for the [#{env}] environment." | |
end | |
puts "------------------------------------------------------------------------------------------------------------\n" | |
end | |
def run(task_name) | |
print "\nRunning #{task_name}..." | |
Rake::Task[task_name].reenable | |
Rake::Task[task_name].invoke | |
print "done.\n" | |
end | |
def set_env(env) | |
Rails.env = env.to_s | |
ENV['RAILS_ENV'] = env.to_s | |
ActiveRecord::Tasks::DatabaseTasks.env = env.to_s | |
# This will cause the engine and configurations to be loaded. Need this for things like the proper migrations appended by the engine | |
run(:environment) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment