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