Last active
February 14, 2017 03:27
-
-
Save keithpitt/ce8e081f40385e1339485b8cc6e4d7f4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Add this file to `spec/support/simple_database_cleaner.rb` | |
class SimpleDatabaseCleaner | |
def clean(connection) | |
@cached_sql ||= {} | |
sql = @cached_sql[connection] ||= | |
begin | |
"".tap do |sql| | |
connection.tables.each do |table_name| | |
next if table_name == "schema_migrations".freeze | |
sql << %{DELETE FROM #{connection.quote_table_name(table_name)};\n} | |
end | |
end | |
end | |
connection.execute sql | |
end | |
end | |
RSpec.configure do |config| | |
database_cleaner = SimpleDatabaseCleaner.new | |
config.before do |example| | |
database_cleaner.clean(ActiveRecord::Base.connection) | |
database_cleaner.clean(RecordWithOtherDatabase.connection) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment