-
-
Save frankyston/2d42cf3c9403ceed1711c3101c5dc774 to your computer and use it in GitHub Desktop.
Rake Task for truncating all tables in a rails app. Preserves the schema_migrations.
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
desc "Truncate all tables in the database" | |
task :truncate_all_tables! => :environment do | |
unless Rails.env.production? | |
puts "TRUNCATING ALL TABLES in 3 seconds!!!!" | |
sleep 3 | |
ActiveRecord::Base.connection.tables.each do |table| | |
unless table == "schema_migrations" | |
puts "Truncating #{table}." | |
ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table} RESTART IDENTITY CASCADE;") | |
else | |
puts "skipping schema_migrations table" | |
end | |
end | |
else | |
puts "This task is not available in production!!!! It will destroy all data in the database." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment