Commit 88a66287 authored by Arturo Herrero's avatar Arturo Herrero Committed by Adam Hegyi

Remove records without group from webhooks

parent 3916d605
---
title: Remove records without group from webhooks table
merge_request: 57863
author:
type: other
# frozen_string_literal: true
class RemoveRecordsWithoutGroupFromWebhooksTable < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
class WebHook < ActiveRecord::Base
include EachBatch
self.table_name = 'web_hooks'
end
class Group < ActiveRecord::Base
self.inheritance_column = :_type_disabled
self.table_name = 'namespaces'
end
def up
subquery = Group.select(1).where(Group.arel_table[:id].eq(WebHook.arel_table[:group_id]))
WebHook.each_batch(of: 500, column: :id) do |relation|
relation.where(type: 'GroupHook').where.not('EXISTS (?)', subquery).delete_all
end
end
def down
# no-op
end
end
3a195b9671846409cf6665b13caad9713541d9cdd95c9f246c22b7db225ab02c
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require_migration!
require Rails.root.join('db', 'migrate', '20210325092215_add_not_valid_foreign_key_to_group_hooks.rb')
RSpec.describe RemoveRecordsWithoutGroupFromWebhooksTable, schema: 20210330091751 do
let(:web_hooks) { table(:web_hooks) }
let(:groups) { table(:namespaces) }
before do
group = groups.create!(name: 'gitlab', path: 'gitlab-org')
web_hooks.create!(group_id: group.id, type: 'GroupHook')
web_hooks.create!(group_id: nil)
AddNotValidForeignKeyToGroupHooks.new.down
web_hooks.create!(group_id: non_existing_record_id, type: 'GroupHook')
AddNotValidForeignKeyToGroupHooks.new.up
end
it 'removes group hooks where the referenced group does not exist', :aggregate_failures do
expect { RemoveRecordsWithoutGroupFromWebhooksTable.new.up }.to change { web_hooks.count }.by(-1)
expect(web_hooks.where.not(group_id: groups.select(:id)).count).to eq(0)
expect(web_hooks.where.not(group_id: nil).count).to eq(1)
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