Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AlecK/APPEALS-35707 Replace database_cleaner with database_cleaner-active_record #20779

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

AKeyframe
Copy link
Contributor

@AKeyframe AKeyframe commented Feb 9, 2024

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

@AKeyframe AKeyframe changed the title Alec k/appeals 35707 AlecK/APPEALS-35707 Replace database_cleaner with database_cleaner-active_record Feb 9, 2024
Copy link

codeclimate bot commented Feb 9, 2024

Code Climate has analyzed commit f52a7b3 and detected 0 issues on this pull request.

View more on Code Climate.

@AKeyframe AKeyframe marked this pull request as ready for review February 12, 2024 15:20
Copy link
Contributor

@jcroteau jcroteau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple minor changes I'd like to see (mostly clean up).

Also, since locking the gem to 2.0.0 is more of a temporary workaround, I would like to see if we can implement a permanent solution similar to the one you alluded to in the PR description (here)...

Gemfile Outdated Show resolved Hide resolved
db/seeds.rb Outdated Show resolved Hide resolved
Copy link
Contributor

@jcroteau jcroteau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⭐ Looks great!

AKeyframe and others added 5 commits April 9, 2024 11:37
Remove old commented gem

Co-authored-by: Jeremy Croteau <[email protected]>
Forgot about byebug, removed

Co-authored-by: Jeremy Croteau <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants