Last active
August 29, 2015 14:09
-
-
Save tlux/47e1bb20c7957e59854c to your computer and use it in GitHub Desktop.
DB Drop and Truncation Tasks
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
| namespace :db do | |
| task drop_tables: :environment do | |
| connection = ActiveRecord::Base.connection | |
| connection.tables.sort.each do |table_name| | |
| connection.execute("DROP TABLE #{table_name} CASCADE") | |
| puts "Dropped: #{table_name}" | |
| end | |
| end | |
| task truncate: :environment do | |
| connection = ActiveRecord::Base.connection | |
| tables = connection.tables.sort - %w(schema_migrations) | |
| tables.each do |table_name| | |
| connection.execute("TRUNCATE TABLE #{table_name}") | |
| puts "Truncated: #{table_name}" | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment