Skip to content

Instantly share code, notes, and snippets.

@marat-chardymov
Created February 27, 2017 17:33
Show Gist options
  • Save marat-chardymov/7ec01d6c355343a58f83a6face01b794 to your computer and use it in GitHub Desktop.
Save marat-chardymov/7ec01d6c355343a58f83a6face01b794 to your computer and use it in GitHub Desktop.
Clear all postgres database with Rails
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