User model does not use Foreign Keys to nullify dependents
Discovered by !74177 (merged).
When executing bin/rspec spec/features/profile_spec.rb:29
it fails with:
Puma caught this error: Cross-database data modification of 'gitlab_main, gitlab_ci' were detected within a transaction modifying the 'uploads, routes, namespaces, keys, project_authorizations, events, releases, ci_builds' tables
This happens due:
- The
profile_spec.rb:29
callsRegistrationsController#destroy
- The
RegistrationsController#destroy
callscurrent_user.delete_async
- The
current_user.delete_async
callsDeleteUserWorker.perform_async
- Since it is feature spec configured to run sidekiq inline
- Since it is feature spec it runs controller requests in a separate thread
- The
DeleteUserWorker.perform
callsUsers::DestroyService.execute
- The
Users::DestroyService.execute
callsuser.destroy
- Since
user.destroy
hashas_many builds, pipelines
set tonullify
within a single transaction this tries toUPDATE ci_builds SET user_id=NULL
failing cross-database modification
class User
has_many :builds, dependent: :nullify, class_name: 'Ci::Build' # rubocop:disable Cop/ActiveRecordDependent
has_many :pipelines, dependent: :nullify, class_name: 'Ci::Pipeline' # rubocop:disable Cop/ActiveRecordDependent
end
Proposal
Migrate has_many dependent: :nullify
to the usage of Loose Foreign Key.
Edited by Kamil Trzciński