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

fix(test): fk always valid #290

Merged
merged 2 commits into from
Oct 2, 2023
Merged

fix(test): fk always valid #290

merged 2 commits into from
Oct 2, 2023

Conversation

BuonOmo
Copy link
Collaborator

@BuonOmo BuonOmo commented Sep 22, 2023

This fixes the test #test_all_foreign_keys_valid_having_foreign_keys_in_multiple_schemas.

@rafiss this makes it that we do not need the query to validate referential integrity (see slack).

This fixes the test
`#test_all_foreign_keys_valid_having_foreign_keys_in_multiple_schemas`.
- Allow using various schemas for the console,
  and always start with a clean slate.
- If no internet, select latest used Rails version.
- Allow to easily show schema loading logs.
- Closer CRDB configuration between CI and local dev.
- Add editor config file.
# referential integrity (e.g: adding a foreign key with invalid data
# raises).
# So foreign keys should always be valid for that matter.
def all_foreign_keys_valid?
Copy link
Contributor

Choose a reason for hiding this comment

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

I noticed that this method may be deprecated: https://github.com/rails/rails/blob/4b8cdef21054aaf6d23a2d2f90418b58e4d0c8ff/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb#L654-L661

I wonder if it's better to avoid using it. It seems like maybe one other option is to override check_all_foreign_keys_valid instead. What do you think?

Or another thing might be the config.active_record.verify_foreign_keys_for_fixtures option, which looks it will disable these checks in the first place https://github.com/rails/rails/blob/4b8cdef21054aaf6d23a2d2f90418b58e4d0c8ff/guides/source/configuring.md?plain=1#L1286. It's referenced here.

If we do keep this method, I'm not sure I understand this - does this mean that the adapter will always consider all foreign keys to be valid? Could it be changed so that all_foreign_keys_valid? only returns true if disable_referential_integrity was called?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@rafiss nice find! It seems like shipping with next AR minor.

I'll keep this one for now, and when shipping next AR, update check_all_foreign_keys_valid! to be sure we don't check anything.

If we do keep this method, I'm not sure I understand this - does this mean that the adapter will always consider all foreign keys to be valid? Could it be changed so that all_foreign_keys_valid? only returns true if disable_referential_integrity was called?

The idea is to use the same method as the abstract one, I could make it more obvious by using this bit of code:

def self.included(base)
  # Getting rid of PG specific check as in CRDB there are no trigger,
  # and thus no such thing as an invalid foreign key.
  base.remove_method(:check_all_foreign_keys_valid!)

About the option, I think the documentation is correct: this is PG/sqlite specific!

Copy link
Contributor

@rafiss rafiss Oct 2, 2023

Choose a reason for hiding this comment

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

 # Getting rid of PG specific check as in CRDB there are no trigger,
 # and thus no such thing as an invalid foreign key.

That's not quite accurate - CRDB does have the concept of an invalid foreign key, it's just that they are not implemented using triggers. (I know you are on vacation now, but just leaving this note to keep track of this discussion.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@rafiss still available a bit :)

Feel free to change the comment. I extrapolated from what I saw: it is not possible using the same method disable_referential_integrity to generate the same incoherence in the DB.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah yes that does seem correct - CRDB doesn't have a way to "turn off" referential integrity checks other than just removing the foreign key constraint entirely. i think we can leave things as they are.

@BuonOmo BuonOmo mentioned this pull request Sep 27, 2023
@rafiss rafiss merged commit e645a35 into master Oct 2, 2023
@rafiss rafiss deleted the fix-all-fk-valid branch October 2, 2023 22:34
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