Skip to content

Instantly share code, notes, and snippets.

@wok
Created April 7, 2016 12:27
Show Gist options
  • Save wok/49f30249eba0c4ba2db66edfb322169e to your computer and use it in GitHub Desktop.
Save wok/49f30249eba0c4ba2db66edfb322169e to your computer and use it in GitHub Desktop.
codeship.rake
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