0.02
No commit activity in last 3 years
No release in over 3 years
super-fast database cleaner
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.13
>= 0
>= 0
>= 0
~> 10.0
~> 3.0
 Project Readme

DatabaseFlusher

Build Status Code Climate

database_flusher is a tiny and fast database cleaner inspired by database_cleaner and database_rewinder.

Features

  • No monkey patching - uses ActiveSupport::Notifications and Mongo::Monitoring to catch INSERT statements
  • Fast :deletion strategy that cleans only tables/collections where INSERT statements were performed
  • Faster disable_referential_integrity for PostgreSQL
  • Executes multiple DELETE statements as one query with ActiveRecord
  • Supports only one database for each ORM

Supported ORMs and strategies

ORM Deletion Transaction
ActiveRecord >= 4.2 Yes Yes
Mongoid >= 5.0 Yes No

mysql2 needs MULTI_STATEMENTS flag to be set and requires active_record >= 5.0.0

adapter: mysql2
flags:
  - MULTI_STATEMENTS

Installation

Add this line to your application's Gemfile:

gem 'database_flusher'

And then execute:

$ bundle

Usage

RSpec

RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.before :suite do
    if File.exists?('.~lock')
      puts "Unclean shutdown, cleaning the whole database..."
      DatabaseFlusher[:active_record].clean_with(:deletion)
      DatabaseFlusher[:mongoid].clean_with(:deletion)
    else
      File.open('.~lock', 'a') {}
    end

    DatabaseFlusher[:active_record].strategy = :transaction
    DatabaseFlusher[:mongoid].strategy = :deletion
  end

  config.after :suite do
    File.unlink('.~lock')
  end

  config.before :each do
    DatabaseFlusher[:active_record].strategy = :transaction
  end

  config.before :each, type: :feature do
    if Capybara.current_driver != :rack_test
      DatabaseFlusher[:active_record].strategy = :deletion
    end
  end

  config.before :each do
    DatabaseFlusher.start
  end

  config.append_after :each do
    DatabaseFlusher.clean
  end
end

Cucumber

if File.exists?('.~lock')
  puts "Unclean shutdown, cleaning the whole database..."
  DatabaseFlusher[:active_record].clean_with(:deletion)
  DatabaseFlusher[:mongoid].clean_with(:deletion)
else
  File.open('.~lock', 'a') {}
end

at_exit do
  File.unlink('.~lock')
end

DatabaseFlusher[:active_record].strategy = :transaction
DatabaseFlusher[:mongoid].strategy = :deletion

# Use Before hook to make sure it runs after capybara driver is set.
Before do
  if Capybara.current_driver == :rack_test
    DatabaseFlusher[:active_record].strategy = :transaction
  else
    DatabaseFlusher[:active_record].strategy = :deletion
  end
  DatabaseFlusher.start
end

# Use Around hook to make sure it runs after capybara session reset.
Around do |scenario, block|
  begin
    block.call
  ensure
    DatabaseFlusher.clean
  end
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ebeigarts/database_flusher. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.