Created
November 2, 2011 01:21
-
-
Save wulftone/1332577 to your computer and use it in GitHub Desktop.
Rake task for Rails: Completely reload test and dev databases
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 | |
desc 'Drop, create, migrate, and seed a database' | |
task :reload => :environment do | |
puts "Environment Check: Rails Environment = #{Rails.env}" | |
Rake::Task['db:drop'].reenable | |
Rake::Task['db:drop'].invoke | |
Rake::Task['db:create'].reenable | |
Rake::Task['db:create'].invoke | |
Rake::Task['db:migrate'].reenable if Rails.env.development? | |
Rake::Task['db:migrate'].invoke if Rails.env.development? | |
Rake::Task['db:test:prepare'].reenable if Rails.env.test? | |
Rake::Task['db:test:prepare'].invoke if Rails.env.test? | |
Rake::Task['db:seed'].reenable | |
Rake::Task['db:seed'].invoke | |
end | |
desc 'Drop, create, migrate, and seed development and test databases' | |
namespace :reload do | |
task :all do | |
['development','test'].each do |env| | |
Rails.env = env | |
puts "=== Starting #{Rails.env} reload ===\n\n" | |
Rake::Task['db:reload'].reenable | |
Rake::Task['db:reload'].invoke | |
puts "=== Finishing #{Rails.env} reload ===\n\n" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's another way to do this with a little data safety in mind:
task :build => :environment do
input = ''
STDOUT.puts "Drop and recreate database? y[es] or n[o]"
input = STDIN.gets.chomp
if input == "y"
puts "Executing tasks..."
Rake::Task["db:drop"].execute
Rake::Task["db:create"].execute
Rake::Task["db:migrate"].execute
Rake::Task["db:test:prepare"].execute
Rake::Task['db:seed'].execute
else
puts "Aborting tasks..."
end
end