caseflow
caseflow copied to clipboard
AlecK/APPEALS-35707 Replace `database_cleaner` with `database_cleaner-active_record`
Resolves APPEALS-35707
Description
With Rails 6.1 the database_cleaner
gem is no longer compatible, and needs to be replaced with database_cleaner-active_record
instead. As part of the update, the API has changed and we no longer use DatabaseCleaner[:active_record, { connection: whatever }]
. Instead we should now being using DatabaseCleaner[:active_record, { db: whatever }]
Note: There is currently an issue with database_cleaner-active_record
v2.0.1+ that results in a foreign key constrain error when attempting to truncate. The gem is locked at 2.0.0 until this is resolved. Relevant issue
Attempted Solutions Beyond Locking to 2.0.0
As locking the gem to 2.0.0 is not preferred, I looked for an alternative solution this issue. Below are a list of the proposed solutions I found from various resources:
-
Turning off foreign key checks temporarily by:
-
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
-
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 1')
-
ActiveRecord::Base.connection.disable_referential_integrity
-
ActiveRecord::Base.connection.enable_referential_integrity
-
-
Resetting PK sequence by looping through the tables
-
ActiveRecord::Base.connection.reset_pk_sequence!(tables)
-
-
Various DBCleaner options
-
DatabaseCleaner.clean_with(:truncation, {cascade: true})
-
DatabaseCleaner.clean_with(:deletion)
- database_cleaner-active_record appears to only support the following:
:only
,:except
,:pre_count
, and:cache_tables
. I've tried a few other options that were recommended for this issue, but don't seem to be with this version of db cleaner. - I attempted to use the
:only
option as a way to truncate the tables in order. The first table would be deleted, which resulted in the relation between the two tables vanishing, causing an unidentified table error upon reaching the next table.
-
Testing Plan
- Run
make reset
locally and ensure the databases are re-seeded correctly. Test Story
Code Climate has analyzed commit f52a7b3b and detected 0 issues on this pull request.
View more on Code Climate.