Last active
December 18, 2018 10:30
-
-
Save shime/6ad6a6a676a78f81f4c166a6c916f6aa to your computer and use it in GitHub Desktop.
Reseed database in Rails without dropping it. Originally posted at https://shime.sh/til/faster-database-resets-in-rails
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 'Clears the database and then seeds it' | |
task reseed: :environment do | |
Rake::Task["db:truncate"].invoke | |
Rake::Task["db:seed"].invoke | |
end | |
desc 'Clears the database' | |
task truncate: :environment do | |
puts "Truncating database" | |
ActiveRecord::Base.connection.tables.reject {|t| t =~ /internal|schema_migrations/}.map do |table| | |
putc "." | |
ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table} RESTART IDENTITY CASCADE") | |
end | |
puts | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment