Skip to content

Instantly share code, notes, and snippets.

@penguinwokrs
Created September 14, 2018 07:18
Show Gist options
  • Save penguinwokrs/b41cbd50f64eda6e18c4880fdf0ffb59 to your computer and use it in GitHub Desktop.
Save penguinwokrs/b41cbd50f64eda6e18c4880fdf0ffb59 to your computer and use it in GitHub Desktop.
easy db truncate task
namespace :db do
namespace :reset do
desc 'db record reset'
task timeleap: :environment do
Rake::Task['db:reset:truncates'].invoke
Rake::Task['db:seed'].invoke
end
desc 'db record truncate'
task truncates: :environment do
tables = Settings.database.truncate.tables
tables.each do |table|
ApplicationRecord.connection.execute('SET FOREIGN_KEY_CHECKS=0;')
ApplicationRecord.connection.execute("TRUNCATE TABLE #{table};")
ApplicationRecord.connection.execute('SET FOREIGN_KEY_CHECKS=1;')
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment