Database cleaner wipes out all tables even when using :except option
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
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?
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