Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database cleaner wipes out all tables even when using :except option

I'm having an issue with database cleaner on a rails project. I use a sqlite3 database in my test environment, and it has a number of tables containing reference data, populated by the db:test:prepare task, that does not need to be wiped between tests.

I have a number of cucumber scenarios tagged with @javascript using the webdriver driver, and some without that tag.

In my env.rb file, I have configured database cleaner to use the truncation strategy, with the except option:

DatabaseCleaner.strategy = :truncation, {:except => %w[ignore me]}

DatabaseCleaner.clean is called after each scenario, and works as expected on the javascript-tagged scenarios.

However, for the non javascript scenarios it is truncating the entire database, including the tables listed in the :except array. I've also tried calling DatabaseCleaner.clean_with, which didn't work either.

like image 685
AlistairH Avatar asked Jun 08 '11 08:06

AlistairH


3 Answers

I've had similar problem with PostgreSQL and database_cleaner (1.4.0). This code wasn't working and truncated all despite the :except option:

DatabaseCleaner.clean_with(:truncation, :except => %w[countries] )

I found out you have to provide full table name to make it work with PostgreSQL. Following code worked as expected:

DatabaseCleaner.clean_with(:truncation, :except => %w[public.countries] )

UPDATE: it seems like this is fixed in database_cleaner version 1.4.1 and it works there as expected

like image 139
remo Avatar answered Sep 22 '22 10:09

remo


I was running into a similar error, except I was seeing the problem when running my @javascript scenarios.

After much googling, reading and hair-pulling I came across this post

I looked in the database_cleaner hook file mentioned there (cucumber-rails-0.4.0/lib/cucumber/rails/hooks/database_cleaner.rb) and lo-and-behold it sets the database_cleaner strategy to :truncation without exceptions before each scenario with the following tags: @no-txn,@selenium,@culerity,@celerity,@javascript.

Before('@no-txn,@selenium,@culerity,@celerity,@javascript') do
  DatabaseCleaner.strategy = :truncation
end

I copied and pasted the before statement into my cucumber env.rb file and added my :except statement to it. That seems to have fixed the issue.

Are you certain it's not your @javascript scenarios that are causing the problem?

like image 39
patrickmcgraw Avatar answered Sep 21 '22 10:09

patrickmcgraw


For latest rails version >=6 and database_cleaner (v1.8.5),we do not need to use public prefix for tables. You can add the following in your rails_helper.rb:

  tables_to_be_excluded = %w[user_types]
  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation, {except: tables_to_be_excluded})
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
like image 21
riddhi Avatar answered Sep 23 '22 10:09

riddhi