Fix cross-database modification detection with HasAndBelongsToMany
The JOIN-table for ActiveRecord
associations using has_and_belongs_to_many
has no model file so there is no association with the gitlab_schema
. This makes the cross-database modification detection raise errors with an undefined table.
Example:
Database::PreventCrossDatabaseModification::CrossDatabaseModificationAcrossUnsupportedTablesError:
Cross-database data modification of 'gitlab_main, undefined' were detected within a transaction modifying the 'customer_relations_contacts, issue_customer_relations_contacts' tables
MR where the error was raised: !71889 (merged)
Option 1: Turn has_and_belongs_to_many
associations to has_many through
associations
We use has_and_belongs_to_many
in the following files (ag -l has_and_belongs_to_many
):
app/models/issue.rb
app/models/customer_relations/contact.rb
app/models/prometheus_alert_event.rb
app/models/self_managed_prometheus_alert_event.rb
ee/app/models/approval_project_rule.rb
ee/app/models/merge_requests/external_status_check.rb
ee/app/models/approval_merge_request_rule.rb
ee/app/models/concerns/approval_rule_like.rb
ee/app/models/concerns/ee/protected_branch.rb
ee/lib/ee/gitlab/background_migration/migrate_approver_to_approval_rules.rb
lib/gitlab/background_migration/add_modified_to_approval_merge_request_rule.rb
How:
- Define
ActiveRecord
models for the definedjoin_tables
. - Update
has_and_belongs_to_many
tohas_many through:
If we fix this issue then we can definitely remove a few entries from the spec/support/database/cross-database-modification-allowlist.yml
allowlist file.
Edited by Adam Hegyi