Skip to content

Instantly share code, notes, and snippets.

@shime shime/reseed.rake
Last active Dec 18, 2018

Embed
What would you like to do?
Reseed database in Rails without dropping it. Originally posted at https://shime.sh/til/faster-database-resets-in-rails
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
You can’t perform that action at this time.