Commit e057e178 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Simplify how we upsert pending builds to the queue

parent 250bb538
......@@ -7,16 +7,12 @@ module Ci
belongs_to :project
belongs_to :build, class_name: 'Ci::Build'
def self.upsert!(build)
def self.upsert_from_build!(build)
entry = self.new(build: build, project: build.project)
raise ArgumentError, 'queuing entry invalid' unless entry.valid?
entry.validate!
attributes = { build_id: entry.build_id, project_id: entry.project_id }
ActiveRecord::InsertAll
.new(self, [attributes], on_duplicate: :skip, returning: %w[build_id])
.execute
self.upsert(entry.attributes.compact, returning: %w[build_id], unique_by: :build_id)
end
end
end
......@@ -19,7 +19,7 @@ module Ci
raise InvalidQueueTransition unless transition.to == 'pending'
transition.within_transaction do
result = ::Ci::PendingBuild.upsert!(build)
result = ::Ci::PendingBuild.upsert_from_build!(build)
unless result.empty?
metrics.increment_queue_operation(:build_queue_push)
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::PendingBuild do
let_it_be(:project) { create(:project) }
let_it_be(:pipeline) { create(:ci_pipeline, project: project) }
let(:build) { create(:ci_build, :created, pipeline: pipeline) }
describe '.upsert_from_build!' do
context 'another pending entry does not exist' do
it 'creates a new pending entry' do
result = described_class.upsert_from_build!(build)
expect(result.rows.dig(0, 0)).to eq build.id
expect(build.reload.queuing_entry).to be_present
end
end
context 'when another queuing entry exists for given build' do
before do
described_class.create!(build: build, project: project)
end
it 'returns a build id as a result' do
result = described_class.upsert_from_build!(build)
expect(result.rows.dig(0, 0)).to eq build.id
end
end
end
end
......@@ -47,10 +47,10 @@ RSpec.describe Ci::UpdateBuildQueueService do
::Ci::PendingBuild.create!(build: build, project: project)
end
it 'does nothing and returns nil' do
it 'does nothing and returns build id' do
queued = subject.push(build, transition)
expect(queued).to be_nil
expect(queued).to eq build.id
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