Skip to content

Instantly share code, notes, and snippets.

@keithpitt
Last active February 14, 2017 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithpitt/ce8e081f40385e1339485b8cc6e4d7f4 to your computer and use it in GitHub Desktop.
Save keithpitt/ce8e081f40385e1339485b8cc6e4d7f4 to your computer and use it in GitHub Desktop.
# 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