Commit 0446e516 authored by Adam Hegyi's avatar Adam Hegyi

Add locking and test case

parent 89d4d45e
......@@ -7,6 +7,7 @@ class RemoveDastSiteProfilesBuildsCiBuildIdFk < Gitlab::Database::Migration[1.0]
def up
with_lock_retries do
execute('LOCK ci_builds, dast_site_profiles_builds IN ACCESS EXCLUSIVE MODE')
remove_foreign_key_if_exists(:dast_site_profiles_builds, :ci_builds, name: CONSTRAINT_NAME)
end
end
......
......@@ -27,4 +27,9 @@ RSpec.describe Dast::SiteProfilesBuild, type: :model do
end
end
end
it_behaves_like 'cleanup by a loose foreign key' do
let!(:model) { create(:dast_site_profiles_build) }
let(:parent) { model.ci_build }
end
end
......@@ -55,8 +55,13 @@ RSpec.shared_examples 'cleanup by a loose foreign key' do
end
def find_model
primary_key = model.class.primary_key.to_sym
model.class.find_by(primary_key => model.public_send(primary_key))
query = model.class
# handle composite primary keys
connection = model.class.connection
connection.primary_keys(model.class.table_name).each do |primary_key|
query = query.where(primary_key => model.public_send(primary_key))
end
query.first
end
it 'cleans up (delete or nullify) the model' do
......
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