Created
February 27, 2017 17:33
-
-
Save marat-chardymov/7ec01d6c355343a58f83a6face01b794 to your computer and use it in GitHub Desktop.
Clear all postgres database with Rails
This file contains 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 'delete all data from db except migrations' | |
task :truncate_db => :environment do | |
conn = ActiveRecord::Base.connection | |
postgres = "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname='public'" | |
tables = conn.execute(postgres).map { |r| r['tablename'] } | |
tables.delete "schema_migrations" | |
tables.each { |t| conn.execute("TRUNCATE \"#{t}\" CASCADE") } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment