Skip to content

Instantly share code, notes, and snippets.

@shime
Last active December 18, 2018 10:30
Show Gist options
  • Save shime/6ad6a6a676a78f81f4c166a6c916f6aa to your computer and use it in GitHub Desktop.
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
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