I am currently using DatabaseCleaner
in my Rails project with PostgreSQL running, and set it up as below.
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation, { pre_count: true, reset_ids: true })
end
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
in one of my rails test suite, I printed out id of an instance. I assume it should be relatively small number since clean_with(:truncate) suppose to clear db and run vacuum on it. but it gets increased every time I run the test.
test passes and it doesn't matter what sequence it uses. but why clean_with(:truncation)
doesn't work in a way it should?
====== EDIT ======
this is in the scope of RSpec test. I understand sequence numbering has no impact on performance, but expensive cleaning (:truncation) on each :suite and use cheap and quick cleaning (:transaction) does. so I want to understand why clean_with(:truncation)
does not reset id for me to obtain clean db state before running test suite.
truncate
on itself does not reset any sequence in Postgres. But Postgres supportstruncate .. restart identity
. Maybe in rails this can be configured somehow – ArightDatabaseCleaner.strategy = :truncate
, it resets properly. but I want to use transaction for each to optimize performance – Knighthood