Skip to content

Instantly share code, notes, and snippets.

@garrow
Created February 2, 2012 03:26
Show Gist options
  • Save garrow/1721219 to your computer and use it in GitHub Desktop.
Save garrow/1721219 to your computer and use it in GitHub Desktop.
Drop every table in the database without requiring the DROP DATABASE permission.
namespace :db do
namespace :drop do
desc "Drop every table in the database. (Does not require DROP DATABASE permission.)"
task :tables => :load_config do
config = ActiveRecord::Base.configurations[Rails.env || 'development']
begin
ActiveRecord::Base.establish_connection(config)
puts "Dropping #{ActiveRecord::Base.connection.tables.count} tables."
ActiveRecord::Base.connection.tables.each do |table|
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{table}")
puts "Dropped #{table}"
end
rescue
$stderr.puts "A problem occurred while dropping tables."
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment