Skip to content

Instantly share code, notes, and snippets.

@tlux
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save tlux/47e1bb20c7957e59854c to your computer and use it in GitHub Desktop.

Select an option

Save tlux/47e1bb20c7957e59854c to your computer and use it in GitHub Desktop.
DB Drop and Truncation Tasks
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