Created
April 7, 2016 12:27
-
-
Save wok/49f30249eba0c4ba2db66edfb322169e to your computer and use it in GitHub Desktop.
codeship.rake
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 | |
desc 'Drops all tables in the database' | |
task drop_all_tables: :environment do | |
fail 'Only allowed in test environment' unless Rails.env.test? | |
connection = ActiveRecord::Base.connection | |
case connection.adapter_name | |
# SQL Server via TinyTDS | |
when 'SQLServer' | |
last_error_message = nil | |
begin | |
command = "EXEC sp_msforeachtable 'DROP TABLE ?'" | |
connection.raw_connection.execute(command).do | |
rescue TinyTds::Error => e | |
if last_error_message != e.message | |
last_error_message = e.message | |
retry | |
end | |
end | |
else | |
fail "Don't know how to drop tables for #{connection.adapter_name}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment