Created
November 1, 2016 18:53
-
-
Save frank184/ccd0a86afcdb9e13f8a00fb5ab5b0eee to your computer and use it in GitHub Desktop.
Truncate Test DB for when working with `before(:all)`
This file contains hidden or 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
| # /lib/tasks/db.rake | |
| namespace :db do | |
| desc "Truncate all table unless running production" | |
| task :truncate => :environment do | |
| abort("The Rails environment is running in production mode!") if Rails.env.production? | |
| ActiveRecord::Base.logger = Logger.new(STDOUT) | |
| connection = ActiveRecord::Base.connection | |
| connection.execute("SET FOREIGN_KEY_CHECKS=0;") | |
| tables = connection.execute("SHOW TABLES").map{|r| r[0]} | |
| tables.delete "schema_migrations" | |
| tables.each {|table| connection.execute("TRUNCATE #{table}")} | |
| connection.execute("SET FOREIGN_KEY_CHECKS=1;") | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment