Commit 0255e321 authored by Toon Claes's avatar Toon Claes

Merge branch '283938-cleanup-web_hooks-service-foreign-key' into 'master'

Cleanup foreign key web_hooks, services

See merge request gitlab-org/gitlab!48263
parents 9da665a3 8dc508c1
---
title: Remove orphan service hooks
merge_request: 48263
author:
type: fixed
# frozen_string_literal: true
class RemoveOrphanServiceHooks < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
class WebHook < ActiveRecord::Base
include EachBatch
self.table_name = 'web_hooks'
def self.service_hooks
where(type: 'ServiceHook')
end
end
class Service < ActiveRecord::Base
self.table_name = 'services'
end
def up
WebHook.service_hooks.where.not(service_id: Service.select(:id)).where.not(service_id: nil).each_batch do |relation|
relation.delete_all
end
end
def down
# no-op
end
end
85a642d60e92a880e0a0699f8dcca42aebe2b5363bfcc3010e647734c7cb7dec
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require_migration!
require Rails.root.join('db', 'migrate', '20201119125730_add_web_hooks_service_foreign_key.rb')
RSpec.describe RemoveOrphanServiceHooks, schema: 20201203123201 do
let(:web_hooks) { table(:web_hooks) }
let(:services) { table(:services) }
before do
services.create!
web_hooks.create!(service_id: services.first.id, type: 'ServiceHook')
web_hooks.create!(service_id: nil)
AddWebHooksServiceForeignKey.new.down
web_hooks.create!(service_id: non_existing_record_id, type: 'ServiceHook')
AddWebHooksServiceForeignKey.new.up
end
it 'removes service hooks where the referenced service does not exist', :aggregate_failures do
expect { RemoveOrphanServiceHooks.new.up }.to change { web_hooks.count }.by(-1)
expect(web_hooks.where.not(service_id: services.select(:id)).count).to eq(0)
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