Commit 33b0821b authored by Adam Hegyi's avatar Adam Hegyi Committed by Steve Abrams

Delete merge_request_metrics -> ci_pipelines FK

parent aad2d3f5
# frozen_string_literal: true
class DropCiPipelinesMrMetricsFk < Gitlab::Database::Migration[1.0]
disable_ddl_transaction!
def up
with_lock_retries do
remove_foreign_key_if_exists(:merge_request_metrics, :ci_pipelines, name: "fk_rails_33ae169d48")
end
end
def down
add_concurrent_foreign_key(:merge_request_metrics, :ci_pipelines, name: "fk_rails_33ae169d48", column: :pipeline_id, target_column: :id, on_delete: "cascade")
end
end
3d011cc67fc6ac661788f2d0e3766e51d624a4248ac9dbd861a4db810d396091
\ No newline at end of file
...@@ -30328,9 +30328,6 @@ ALTER TABLE ONLY container_repositories ...@@ -30328,9 +30328,6 @@ ALTER TABLE ONLY container_repositories
ALTER TABLE ONLY clusters_applications_jupyter ALTER TABLE ONLY clusters_applications_jupyter
ADD CONSTRAINT fk_rails_331f0aff78 FOREIGN KEY (oauth_application_id) REFERENCES oauth_applications(id) ON DELETE SET NULL; ADD CONSTRAINT fk_rails_331f0aff78 FOREIGN KEY (oauth_application_id) REFERENCES oauth_applications(id) ON DELETE SET NULL;
ALTER TABLE ONLY merge_request_metrics
ADD CONSTRAINT fk_rails_33ae169d48 FOREIGN KEY (pipeline_id) REFERENCES ci_pipelines(id) ON DELETE CASCADE;
ALTER TABLE ONLY suggestions ALTER TABLE ONLY suggestions
ADD CONSTRAINT fk_rails_33b03a535c FOREIGN KEY (note_id) REFERENCES notes(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_33b03a535c FOREIGN KEY (note_id) REFERENCES notes(id) ON DELETE CASCADE;
...@@ -45,3 +45,7 @@ terraform_state_versions: ...@@ -45,3 +45,7 @@ terraform_state_versions:
- table: ci_builds - table: ci_builds
column: ci_build_id column: ci_build_id
on_delete: async_nullify on_delete: async_nullify
merge_request_metrics:
- table: ci_pipelines
column: pipeline_id
on_delete: async_delete
...@@ -48,4 +48,10 @@ RSpec.describe MergeRequest::Metrics do ...@@ -48,4 +48,10 @@ RSpec.describe MergeRequest::Metrics do
end end
end end
end end
it_behaves_like 'cleanup by a loose foreign key' do
let!(:merge_request) { create(:merge_request) }
let!(:parent) { create(:ci_pipeline, project: merge_request.target_project) }
let!(:model) { merge_request.metrics.tap { |metrics| metrics.update!(pipeline: parent) } }
end
end end
...@@ -92,4 +92,9 @@ RSpec.describe Terraform::StateVersion do ...@@ -92,4 +92,9 @@ RSpec.describe Terraform::StateVersion do
end end
end end
end end
it_behaves_like 'cleanup by a loose foreign key' do
let!(:model) { create(:terraform_state_version) }
let!(:parent) { model.build }
end
end end
...@@ -47,3 +47,28 @@ RSpec.shared_examples 'it has loose foreign keys' do ...@@ -47,3 +47,28 @@ RSpec.shared_examples 'it has loose foreign keys' do
expect(deleted_records.status_processed.count).to be(1) expect(deleted_records.status_processed.count).to be(1)
end end
end end
RSpec.shared_examples 'cleanup by a loose foreign key' do
let(:foreign_key_definition) do
foreign_keys_for_parent = Gitlab::Database::LooseForeignKeys.definitions_by_table[parent.class.table_name]
foreign_keys_for_parent.find { |definition| definition.from_table == model.class.table_name }
end
def find_model
model.class.find_by(id: model.id)
end
it 'deletes the model' do
parent.delete
expect(find_model).to be_present
LooseForeignKeys::ProcessDeletedRecordsService.new(connection: model.connection).execute
if foreign_key_definition.on_delete.eql?(:async_delete)
expect(find_model).not_to be_present
else
expect(find_model[foreign_key_definition.column]).to eq(nil)
end
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