Commit ca143cd3 authored by Stan Hu's avatar Stan Hu

Merge branch 'dblessing-emails-cascade-delete' into 'master'

User emails dependent destroy to foreign key with cascade delete

See merge request gitlab-org/gitlab!39899
parents 43e236b0 9ce79fa6
...@@ -107,7 +107,7 @@ class User < ApplicationRecord ...@@ -107,7 +107,7 @@ class User < ApplicationRecord
has_many :group_deploy_keys has_many :group_deploy_keys
has_many :gpg_keys has_many :gpg_keys
has_many :emails, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent has_many :emails
has_many :personal_access_tokens, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent has_many :personal_access_tokens, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :identities, dependent: :destroy, autosave: true # rubocop:disable Cop/ActiveRecordDependent has_many :identities, dependent: :destroy, autosave: true # rubocop:disable Cop/ActiveRecordDependent
has_many :u2f_registrations, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent has_many :u2f_registrations, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
......
---
title: Add emails user_id foreign key with cascade delete
merge_request: 39899
author:
type: other
# frozen_string_literal: true
class AddEmailsUserIdForeignKey < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
CONSTRAINT_NAME = 'fk_emails_user_id'
def up
with_lock_retries do
add_foreign_key :emails, :users, on_delete: :cascade, validate: false, name: CONSTRAINT_NAME
end
end
def down
with_lock_retries do
remove_foreign_key_if_exists :emails, column: :user_id, name: CONSTRAINT_NAME
end
end
end
# frozen_string_literal: true
class RemoveOrphanedEmails < ActiveRecord::Migration[6.0]
DOWNTIME = false
def up
execute <<~SQL
DELETE FROM emails
WHERE not exists (
SELECT 1 FROM users WHERE users.id = emails.user_id
);
SQL
execute 'DELETE FROM emails WHERE user_id IS NULL;'
end
def down
# no-op
end
end
# frozen_string_literal: true
class ValidateEmailsUserIdForeignKey < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
CONSTRAINT_NAME = 'fk_emails_user_id'
def up
validate_foreign_key :emails, :user_id, name: CONSTRAINT_NAME
end
def down
# no op
end
end
5a5278fdd9539d33a6de226a84ed39b7c5a26929cec68ec5e8d193afb3cfafa2
\ No newline at end of file
476bce9b18177f37b31e15d42f5a1391c0bfbbd312a513c1d5b43085b90afb3e
\ No newline at end of file
5e2dfdf725ad0a3d90b240ced74cf5a872f7126b716847f9f9e99b4ad2a22109
\ No newline at end of file
...@@ -22016,6 +22016,9 @@ ALTER TABLE ONLY public.events ...@@ -22016,6 +22016,9 @@ ALTER TABLE ONLY public.events
ALTER TABLE ONLY public.vulnerabilities ALTER TABLE ONLY public.vulnerabilities
ADD CONSTRAINT fk_efb96ab1e2 FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE; ADD CONSTRAINT fk_efb96ab1e2 FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.emails
ADD CONSTRAINT fk_emails_user_id FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.clusters ALTER TABLE ONLY public.clusters
ADD CONSTRAINT fk_f05c5e5a42 FOREIGN KEY (management_project_id) REFERENCES public.projects(id) ON DELETE SET NULL; ADD CONSTRAINT fk_f05c5e5a42 FOREIGN KEY (management_project_id) REFERENCES public.projects(id) ON DELETE SET NULL;
......
...@@ -35,7 +35,6 @@ RSpec.describe 'Database schema' do ...@@ -35,7 +35,6 @@ RSpec.describe 'Database schema' do
deploy_keys_projects: %w[deploy_key_id], deploy_keys_projects: %w[deploy_key_id],
deployments: %w[deployable_id environment_id user_id], deployments: %w[deployable_id environment_id user_id],
draft_notes: %w[discussion_id commit_id], draft_notes: %w[discussion_id commit_id],
emails: %w[user_id],
epics: %w[updated_by_id last_edited_by_id state_id], epics: %w[updated_by_id last_edited_by_id state_id],
events: %w[target_id], events: %w[target_id],
forked_project_links: %w[forked_from_project_id], forked_project_links: %w[forked_from_project_id],
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment