diff --git a/changelogs/unreleased/283938_cascade_delete_inheriting_services.yml b/changelogs/unreleased/283938_cascade_delete_inheriting_services.yml new file mode 100644 index 00000000000..25989863327 --- /dev/null +++ b/changelogs/unreleased/283938_cascade_delete_inheriting_services.yml @@ -0,0 +1,5 @@ +--- +title: Change services.inherit_from_id foreign key to ON DELETE CASCADE +merge_request: 48163 +author: +type: fixed diff --git a/db/migrate/20201119162801_change_services_inherit_from_id_foreign_key.rb b/db/migrate/20201119162801_change_services_inherit_from_id_foreign_key.rb new file mode 100644 index 00000000000..a7f12fcf726 --- /dev/null +++ b/db/migrate/20201119162801_change_services_inherit_from_id_foreign_key.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class ChangeServicesInheritFromIdForeignKey < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + def up + add_concurrent_foreign_key :services, :services, column: :inherit_from_id, on_delete: :cascade, name: 'fk_services_inherit_from_id' + remove_foreign_key_if_exists :services, name: 'fk_868a8e7ad6' + end + + def down + remove_foreign_key_if_exists :services, name: 'fk_services_inherit_from_id' + add_concurrent_foreign_key :services, :services, column: :inherit_from_id, on_delete: :nullify, name: 'fk_868a8e7ad6' + end +end diff --git a/db/schema_migrations/20201119162801 b/db/schema_migrations/20201119162801 new file mode 100644 index 00000000000..b6aa67ad5f6 --- /dev/null +++ b/db/schema_migrations/20201119162801 @@ -0,0 +1 @@ +c41f4649540c23d25f0ae26c3754476409b5e77f53b96db65f2c1fd4a6caf087 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index 3be04c5f68d..2559cecd6db 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -23112,9 +23112,6 @@ ALTER TABLE ONLY merge_request_diffs ALTER TABLE ONLY ci_pipelines ADD CONSTRAINT fk_86635dbd80 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE; -ALTER TABLE ONLY services - ADD CONSTRAINT fk_868a8e7ad6 FOREIGN KEY (inherit_from_id) REFERENCES services(id) ON DELETE SET NULL; - ALTER TABLE ONLY geo_event_log ADD CONSTRAINT fk_86c84214ec FOREIGN KEY (repository_renamed_event_id) REFERENCES geo_repository_renamed_events(id) ON DELETE CASCADE; @@ -24780,6 +24777,9 @@ ALTER TABLE ONLY resource_label_events ALTER TABLE ONLY ci_builds_metadata ADD CONSTRAINT fk_rails_ffcf702a02 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE; +ALTER TABLE ONLY services + ADD CONSTRAINT fk_services_inherit_from_id FOREIGN KEY (inherit_from_id) REFERENCES services(id) ON DELETE CASCADE; + ALTER TABLE ONLY timelogs ADD CONSTRAINT fk_timelogs_issues_issue_id FOREIGN KEY (issue_id) REFERENCES issues(id) ON DELETE CASCADE; diff --git a/spec/lib/gitlab/data_builder/pipeline_spec.rb b/spec/lib/gitlab/data_builder/pipeline_spec.rb index 4e0cc8a1fa9..e5dfff33a2a 100644 --- a/spec/lib/gitlab/data_builder/pipeline_spec.rb +++ b/spec/lib/gitlab/data_builder/pipeline_spec.rb @@ -21,9 +21,10 @@ RSpec.describe Gitlab::DataBuilder::Pipeline do let(:data) { described_class.build(pipeline) } let(:attributes) { data[:object_attributes] } let(:build_data) { data[:builds].first } + let(:runner_data) { build_data[:runner] } let(:project_data) { data[:project] } - it 'has correct attributes' do + it 'has correct attributes', :aggregate_failures do expect(attributes).to be_a(Hash) expect(attributes[:ref]).to eq(pipeline.ref) expect(attributes[:sha]).to eq(pipeline.sha) @@ -36,6 +37,7 @@ RSpec.describe Gitlab::DataBuilder::Pipeline do expect(build_data[:id]).to eq(build.id) expect(build_data[:status]).to eq(build.status) expect(build_data[:allow_failure]).to eq(build.allow_failure) + expect(runner_data).to eq(nil) expect(project_data).to eq(project.hook_attrs(backward: false)) expect(data[:merge_request]).to be_nil expect(data[:user]).to eq({ @@ -46,6 +48,18 @@ RSpec.describe Gitlab::DataBuilder::Pipeline do }) end + context 'build with runner' do + let!(:build) { create(:ci_build, pipeline: pipeline, runner: ci_runner) } + let(:ci_runner) { create(:ci_runner) } + + it 'has runner attributes', :aggregate_failures do + expect(runner_data[:id]).to eq(ci_runner.id) + expect(runner_data[:description]).to eq(ci_runner.description) + expect(runner_data[:active]).to eq(ci_runner.active) + expect(runner_data[:is_shared]).to eq(ci_runner.instance_type?) + end + end + context 'pipeline without variables' do it 'has empty variables hash' do expect(attributes[:variables]).to be_a(Array)