Commit 58e3d933 authored by Sean McGivern's avatar Sean McGivern

Merge branch '44824-remove-ghost-notification-settings-for-group-and-project' into 'master'

Resolve "Remove ghost notification settings for groups and projects"

Closes #44824

See merge request gitlab-org/gitlab-ce!20567
parents 1db427f9 a0a36a99
...@@ -130,7 +130,7 @@ class User < ActiveRecord::Base ...@@ -130,7 +130,7 @@ class User < ActiveRecord::Base
has_many :builds, dependent: :nullify, class_name: 'Ci::Build' # rubocop:disable Cop/ActiveRecordDependent 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 has_many :pipelines, dependent: :nullify, class_name: 'Ci::Pipeline' # rubocop:disable Cop/ActiveRecordDependent
has_many :todos has_many :todos
has_many :notification_settings, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent has_many :notification_settings
has_many :award_emoji, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent has_many :award_emoji, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :triggers, dependent: :destroy, class_name: 'Ci::Trigger', foreign_key: :owner_id # rubocop:disable Cop/ActiveRecordDependent has_many :triggers, dependent: :destroy, class_name: 'Ci::Trigger', foreign_key: :owner_id # rubocop:disable Cop/ActiveRecordDependent
......
---
title: Adds foreign key to notification_settings.user_id
merge_request: 20567
author: Jacopo Beschi @jacopo-beschi
type: added
class AddForeignKeyFromNotificationSettingsToUsers < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
class NotificationSetting < ActiveRecord::Base
self.table_name = 'notification_settings'
include EachBatch
end
class User < ActiveRecord::Base
self.table_name = 'users'
end
DOWNTIME = false
disable_ddl_transaction!
def up
NotificationSetting.each_batch(of: 1000) do |batch|
batch.where('NOT EXISTS (?)', User.select(1).where('users.id = notification_settings.user_id'))
.delete_all
end
add_concurrent_foreign_key(:notification_settings, :users, column: :user_id, on_delete: :cascade)
end
def down
remove_foreign_key(:notification_settings, column: :user_id)
end
end
...@@ -2350,6 +2350,7 @@ ActiveRecord::Schema.define(version: 20180726172057) do ...@@ -2350,6 +2350,7 @@ ActiveRecord::Schema.define(version: 20180726172057) do
add_foreign_key "milestones", "projects", name: "fk_9bd0a0c791", on_delete: :cascade add_foreign_key "milestones", "projects", name: "fk_9bd0a0c791", on_delete: :cascade
add_foreign_key "note_diff_files", "notes", column: "diff_note_id", on_delete: :cascade add_foreign_key "note_diff_files", "notes", column: "diff_note_id", on_delete: :cascade
add_foreign_key "notes", "projects", name: "fk_99e097b079", on_delete: :cascade add_foreign_key "notes", "projects", name: "fk_99e097b079", on_delete: :cascade
add_foreign_key "notification_settings", "users", name: "fk_0c95e91db7", on_delete: :cascade
add_foreign_key "oauth_openid_requests", "oauth_access_grants", column: "access_grant_id", name: "fk_oauth_openid_requests_oauth_access_grants_access_grant_id" add_foreign_key "oauth_openid_requests", "oauth_access_grants", column: "access_grant_id", name: "fk_oauth_openid_requests_oauth_access_grants_access_grant_id"
add_foreign_key "pages_domains", "projects", name: "fk_ea2f6dfc6f", on_delete: :cascade add_foreign_key "pages_domains", "projects", name: "fk_ea2f6dfc6f", on_delete: :cascade
add_foreign_key "personal_access_tokens", "users" add_foreign_key "personal_access_tokens", "users"
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'migrate', '20180710162338_add_foreign_key_from_notification_settings_to_users.rb')
describe AddForeignKeyFromNotificationSettingsToUsers, :migration do
let(:notification_settings) { table(:notification_settings) }
let(:users) { table(:users) }
let(:projects) { table(:projects) }
before do
users.create!(email: 'email@email.com', name: 'foo', username: 'foo', projects_limit: 0)
projects.create!(name: 'gitlab', path: 'gitlab-org/gitlab-ce', namespace_id: 1)
end
describe 'removal of orphans without user' do
let!(:notification_setting_without_user) { create_notification_settings!(user_id: 123) }
let!(:notification_setting_with_user) { create_notification_settings!(user_id: users.last.id) }
it 'removes orphaned notification_settings without user' do
expect { migrate! }.to change { notification_settings.count }.by(-1)
end
it "doesn't remove notification_settings with valid user" do
expect { migrate! }.not_to change { notification_setting_with_user.reload }
end
end
def create_notification_settings!(**opts)
notification_settings.create!(
source_id: projects.last.id,
source_type: 'Project',
user_id: users.last.id,
**opts)
end
end
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