Commit 9b779fe3 authored by Tiger Watson's avatar Tiger Watson

Merge branch '202423-validate-foreign-key-on-group-hooks' into 'master'

Validate foreign key on GroupHooks

See merge request gitlab-org/gitlab!60527
parents c7d26021 ce36cb6e
---
title: Validate foreign key on GroupHooks
merge_request: 60527
author:
type: other
# frozen_string_literal: true
class UpdateInvalidWebHooks < ActiveRecord::Migration[6.0]
disable_ddl_transaction!
class WebHook < ActiveRecord::Base
include EachBatch
self.table_name = 'web_hooks'
end
def up
WebHook.each_batch(of: 10_000, column: :id) do |relation|
relation.where(type: 'ProjectHook')
.where.not(project_id: nil)
.where.not(group_id: nil)
.update_all(group_id: nil)
end
end
def down
# no-op
end
end
# frozen_string_literal: true
class ValidateForeignKeyOnGroupHooks < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
CONSTRAINT_NAME = 'fk_rails_d35697648e'
def up
validate_foreign_key :web_hooks, :group_id, name: CONSTRAINT_NAME
end
def down
# no-op
end
end
468373a97f7bd66197c81f01bebd27256cf96ec8fc226c5d73e579a7ecc3930d
\ No newline at end of file
3244023441c2afa450ad76345a494975b4a7154892298daf1ec4223d27fb7ca3
\ No newline at end of file
......@@ -26934,7 +26934,7 @@ ALTER TABLE ONLY pool_repositories
ADD CONSTRAINT fk_rails_d2711daad4 FOREIGN KEY (source_project_id) REFERENCES projects(id) ON DELETE SET NULL;
ALTER TABLE ONLY web_hooks
ADD CONSTRAINT fk_rails_d35697648e FOREIGN KEY (group_id) REFERENCES namespaces(id) ON DELETE CASCADE NOT VALID;
ADD CONSTRAINT fk_rails_d35697648e FOREIGN KEY (group_id) REFERENCES namespaces(id) ON DELETE CASCADE;
ALTER TABLE ONLY group_group_links
ADD CONSTRAINT fk_rails_d3a0488427 FOREIGN KEY (shared_group_id) REFERENCES namespaces(id) ON DELETE CASCADE;
......@@ -30,7 +30,7 @@ module EE
has_many :ldap_group_links, foreign_key: 'group_id', dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :saml_group_links, foreign_key: 'group_id'
has_many :hooks, dependent: :destroy, class_name: 'GroupHook' # rubocop:disable Cop/ActiveRecordDependent
has_many :hooks, class_name: 'GroupHook'
has_many :allowed_email_domains, -> { order(id: :asc) }, autosave: true
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe UpdateInvalidWebHooks do
let(:web_hooks) { table(:web_hooks) }
let(:groups) { table(:namespaces) }
let(:projects) { table(:projects) }
before do
group = groups.create!(name: 'gitlab', path: 'gitlab-org')
project = projects.create!(namespace_id: group.id)
web_hooks.create!(group_id: group.id, type: 'GroupHook')
web_hooks.create!(project_id: project.id, type: 'ProjectHook')
web_hooks.create!(group_id: group.id, project_id: project.id, type: 'ProjectHook')
end
it 'clears group_id when ProjectHook type and project_id are present', :aggregate_failures do
expect(web_hooks.where.not(group_id: nil).where.not(project_id: nil).count).to eq(1)
migrate!
expect(web_hooks.where.not(group_id: nil).where.not(project_id: nil).count).to eq(0)
expect(web_hooks.where(type: 'GroupHook').count).to eq(1)
expect(web_hooks.where(type: 'ProjectHook').count).to eq(2)
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