Created
February 2, 2012 03:26
-
-
Save garrow/1721219 to your computer and use it in GitHub Desktop.
Drop every table in the database without requiring the DROP DATABASE permission.
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
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