Skip to content

Instantly share code, notes, and snippets.

@berpj
Last active February 20, 2019 16:16
Show Gist options
  • Save berpj/12249ddca673d3e110f4ec1a6f063e7e to your computer and use it in GitHub Desktop.
Save berpj/12249ddca673d3e110f4ec1a6f063e7e to your computer and use it in GitHub Desktop.
# lib/tasks/db_clean.rake
# Homemade rake task to clean DB for Rails 5
namespace :db do
desc "Reset and seed DB"
task :clean => :environment do
# Empty tables and reset primary id autoincrement
ApplicationRecord.connection.tables.each do |table|
next if table.match(/\Aschema_migrations\Z/) || table.match(/\Aar_internal_metadata\Z/)
ApplicationRecord.connection.execute("TRUNCATE TABLE #{table} RESTART IDENTITY CASCADE")
end
# Seed
sh "rails db:seed"
end
end
@berpj
Copy link
Author

berpj commented Feb 20, 2019

You can just do rails db:clean to run it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment