Skip to content

Instantly share code, notes, and snippets.

@frank184
Created November 1, 2016 18:53
Show Gist options
  • Select an option

  • Save frank184/ccd0a86afcdb9e13f8a00fb5ab5b0eee to your computer and use it in GitHub Desktop.

Select an option

Save frank184/ccd0a86afcdb9e13f8a00fb5ab5b0eee to your computer and use it in GitHub Desktop.
Truncate Test DB for when working with `before(:all)`
# /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