Commit 57ecf75d authored by Fabio Pitino's avatar Fabio Pitino

Merge branch 'enable-bulk-insert-for-needs' into 'master'

Enable BulkInsertSafe on Ci::BuildNeed

See merge request gitlab-org/gitlab!36815
parents f9f7cbaa a08a82bd
...@@ -4,6 +4,8 @@ module Ci ...@@ -4,6 +4,8 @@ module Ci
class BuildNeed < ApplicationRecord class BuildNeed < ApplicationRecord
extend Gitlab::Ci::Model extend Gitlab::Ci::Model
include BulkInsertSafe
belongs_to :build, class_name: "Ci::Build", foreign_key: :build_id, inverse_of: :needs belongs_to :build, class_name: "Ci::Build", foreign_key: :build_id, inverse_of: :needs
validates :build, presence: true validates :build, presence: true
......
...@@ -6,6 +6,7 @@ class CommitStatus < ApplicationRecord ...@@ -6,6 +6,7 @@ class CommitStatus < ApplicationRecord
include AfterCommitQueue include AfterCommitQueue
include Presentable include Presentable
include EnumWithNil include EnumWithNil
include BulkInsertableAssociations
self.table_name = 'ci_builds' self.table_name = 'ci_builds'
......
...@@ -55,7 +55,9 @@ module Ci ...@@ -55,7 +55,9 @@ module Ci
build = project.builds.new(attributes) build = project.builds.new(attributes)
build.assign_attributes(::Gitlab::Ci::Pipeline::Seed::Build.environment_attributes_for(build)) build.assign_attributes(::Gitlab::Ci::Pipeline::Seed::Build.environment_attributes_for(build))
build.retried = false build.retried = false
BulkInsertableAssociations.with_bulk_insert(enabled: ::Gitlab::Ci::Features.bulk_insert_on_create?(project)) do
build.save! build.save!
end
build build
end end
end end
......
---
title: Enable BulkInsertSafe on Ci::BuildNeed
merge_request: 36815
author:
type: performance
...@@ -61,6 +61,10 @@ module Gitlab ...@@ -61,6 +61,10 @@ module Gitlab
def self.destroy_only_unlocked_expired_artifacts_enabled? def self.destroy_only_unlocked_expired_artifacts_enabled?
::Feature.enabled?(:destroy_only_unlocked_expired_artifacts, default_enabled: false) ::Feature.enabled?(:destroy_only_unlocked_expired_artifacts, default_enabled: false)
end end
def self.bulk_insert_on_create?(project)
::Feature.enabled?(:ci_bulk_insert_on_create, project, default_enabled: true)
end
end end
end end
end end
......
...@@ -8,7 +8,9 @@ module Gitlab ...@@ -8,7 +8,9 @@ module Gitlab
include Chain::Helpers include Chain::Helpers
def perform! def perform!
BulkInsertableAssociations.with_bulk_insert(enabled: ::Gitlab::Ci::Features.bulk_insert_on_create?(project)) do
pipeline.save! pipeline.save!
end
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
error("Failed to persist the pipeline: #{e}") error("Failed to persist the pipeline: #{e}")
end end
......
...@@ -17,4 +17,22 @@ RSpec.describe Ci::BuildNeed, model: true do ...@@ -17,4 +17,22 @@ RSpec.describe Ci::BuildNeed, model: true do
it { expect(described_class.artifacts).to contain_exactly(with_artifacts) } it { expect(described_class.artifacts).to contain_exactly(with_artifacts) }
end end
describe 'BulkInsertSafe' do
let(:ci_build) { build(:ci_build) }
it "bulk inserts from Ci::Build model" do
ci_build.needs_attributes = [
{ name: "build", artifacts: true },
{ name: "build2", artifacts: true },
{ name: "build3", artifacts: true }
]
expect(described_class).to receive(:bulk_insert!).and_call_original
BulkInsertableAssociations.with_bulk_insert do
ci_build.save!
end
end
end
end end
...@@ -1684,6 +1684,12 @@ RSpec.describe Ci::CreatePipelineService do ...@@ -1684,6 +1684,12 @@ RSpec.describe Ci::CreatePipelineService do
expect(pipeline).to be_persisted expect(pipeline).to be_persisted
expect(pipeline.builds.pluck(:name)).to contain_exactly("build_a", "test_a") expect(pipeline.builds.pluck(:name)).to contain_exactly("build_a", "test_a")
end end
it 'bulk inserts all needs' do
expect(Ci::BuildNeed).to receive(:bulk_insert!).and_call_original
expect(pipeline).to be_persisted
end
end end
context 'when pipeline on feature is created' do context 'when pipeline on feature is created' do
......
...@@ -278,6 +278,19 @@ RSpec.describe Ci::RetryBuildService do ...@@ -278,6 +278,19 @@ RSpec.describe Ci::RetryBuildService do
expect(new_build.metadata.expanded_environment_name).to eq('production') expect(new_build.metadata.expanded_environment_name).to eq('production')
end end
end end
context 'when build has needs' do
before do
create(:ci_build_need, build: build, name: 'build1')
create(:ci_build_need, build: build, name: 'build2')
end
it 'bulk inserts all needs' do
expect(Ci::BuildNeed).to receive(:bulk_insert!).and_call_original
new_build
end
end
end end
context 'when user does not have ability to execute build' do context 'when user does not have ability to execute build' 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