Commit 120fb52a authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add id sequence to pending builds table

This change makes it easier to track queuing sequence and the order in
which builds should be processed.
parent c5e0c841
...@@ -37,7 +37,7 @@ module Ci ...@@ -37,7 +37,7 @@ module Ci
has_one :deployment, as: :deployable, class_name: 'Deployment' has_one :deployment, as: :deployable, class_name: 'Deployment'
has_one :pending_state, class_name: 'Ci::BuildPendingState', inverse_of: :build has_one :pending_state, class_name: 'Ci::BuildPendingState', inverse_of: :build
has_one :queuing_entry, class_name: 'Ci::PendingBuild', foreign_key: :build_id, inverse_of: :build has_one :queuing_entry, class_name: 'Ci::PendingBuild', foreign_key: :build_id
has_many :trace_sections, class_name: 'Ci::BuildTraceSection' has_many :trace_sections, class_name: 'Ci::BuildTraceSection'
has_many :trace_chunks, class_name: 'Ci::BuildTraceChunk', foreign_key: :build_id, inverse_of: :build has_many :trace_chunks, class_name: 'Ci::BuildTraceChunk', foreign_key: :build_id, inverse_of: :build
has_many :report_results, class_name: 'Ci::BuildReportResult', inverse_of: :build has_many :report_results, class_name: 'Ci::BuildReportResult', inverse_of: :build
......
...@@ -5,14 +5,18 @@ module Ci ...@@ -5,14 +5,18 @@ module Ci
extend Gitlab::Ci::Model extend Gitlab::Ci::Model
belongs_to :project belongs_to :project
belongs_to :build, class_name: 'Ci::Build', inverse_of: :queuing_entry belongs_to :build, class_name: 'Ci::Build'
def self.upsert!(build) def self.upsert!(build)
entry = self.new(build: build, project: build.project) entry = self.new(build: build, project: build.project)
raise ArgumentError, 'queuing entry invalid' unless entry.valid? raise ArgumentError, 'queuing entry invalid' unless entry.valid?
upsert({ build_id: entry.build_id, project_id: entry.project_id }) attributes = { build_id: entry.build_id, project_id: entry.project_id }
ActiveRecord::InsertAll
.new(self, [attributes], on_duplicate: :skip, returning: %w[build_id])
.execute
end end
end end
end end
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
class AddPendingBuildsTable < ActiveRecord::Migration[6.0] class AddPendingBuildsTable < ActiveRecord::Migration[6.0]
def up def up
create_table :ci_pending_builds, id: false do |t| create_table :ci_pending_builds do |t|
t.references :build, primary_key: true, default: nil, index: false, foreign_key: { to_table: :ci_builds, on_delete: :cascade } t.references :build, index: { unique: true }, null: false, foreign_key: { to_table: :ci_builds, on_delete: :cascade }
t.references :project, index: true, null: false, foreign_key: { on_delete: :cascade } t.references :project, index: true, null: false, foreign_key: { on_delete: :cascade }
t.datetime_with_timezone :created_at, null: false, default: -> { 'NOW()' } t.datetime_with_timezone :created_at, null: false, default: -> { 'NOW()' }
end end
......
...@@ -10753,11 +10753,21 @@ CREATE SEQUENCE ci_namespace_monthly_usages_id_seq ...@@ -10753,11 +10753,21 @@ CREATE SEQUENCE ci_namespace_monthly_usages_id_seq
ALTER SEQUENCE ci_namespace_monthly_usages_id_seq OWNED BY ci_namespace_monthly_usages.id; ALTER SEQUENCE ci_namespace_monthly_usages_id_seq OWNED BY ci_namespace_monthly_usages.id;
CREATE TABLE ci_pending_builds ( CREATE TABLE ci_pending_builds (
id bigint NOT NULL,
build_id bigint NOT NULL, build_id bigint NOT NULL,
project_id bigint NOT NULL, project_id bigint NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL created_at timestamp with time zone DEFAULT now() NOT NULL
); );
CREATE SEQUENCE ci_pending_builds_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE ci_pending_builds_id_seq OWNED BY ci_pending_builds.id;
CREATE TABLE ci_pipeline_artifacts ( CREATE TABLE ci_pipeline_artifacts (
id bigint NOT NULL, id bigint NOT NULL,
created_at timestamp with time zone NOT NULL, created_at timestamp with time zone NOT NULL,
...@@ -19474,6 +19484,8 @@ ALTER TABLE ONLY ci_job_variables ALTER COLUMN id SET DEFAULT nextval('ci_job_va ...@@ -19474,6 +19484,8 @@ ALTER TABLE ONLY ci_job_variables ALTER COLUMN id SET DEFAULT nextval('ci_job_va
ALTER TABLE ONLY ci_namespace_monthly_usages ALTER COLUMN id SET DEFAULT nextval('ci_namespace_monthly_usages_id_seq'::regclass); ALTER TABLE ONLY ci_namespace_monthly_usages ALTER COLUMN id SET DEFAULT nextval('ci_namespace_monthly_usages_id_seq'::regclass);
ALTER TABLE ONLY ci_pending_builds ALTER COLUMN id SET DEFAULT nextval('ci_pending_builds_id_seq'::regclass);
ALTER TABLE ONLY ci_pipeline_artifacts ALTER COLUMN id SET DEFAULT nextval('ci_pipeline_artifacts_id_seq'::regclass); ALTER TABLE ONLY ci_pipeline_artifacts ALTER COLUMN id SET DEFAULT nextval('ci_pipeline_artifacts_id_seq'::regclass);
ALTER TABLE ONLY ci_pipeline_chat_data ALTER COLUMN id SET DEFAULT nextval('ci_pipeline_chat_data_id_seq'::regclass); ALTER TABLE ONLY ci_pipeline_chat_data ALTER COLUMN id SET DEFAULT nextval('ci_pipeline_chat_data_id_seq'::regclass);
...@@ -20645,7 +20657,7 @@ ALTER TABLE ONLY ci_namespace_monthly_usages ...@@ -20645,7 +20657,7 @@ ALTER TABLE ONLY ci_namespace_monthly_usages
ADD CONSTRAINT ci_namespace_monthly_usages_pkey PRIMARY KEY (id); ADD CONSTRAINT ci_namespace_monthly_usages_pkey PRIMARY KEY (id);
ALTER TABLE ONLY ci_pending_builds ALTER TABLE ONLY ci_pending_builds
ADD CONSTRAINT ci_pending_builds_pkey PRIMARY KEY (build_id); ADD CONSTRAINT ci_pending_builds_pkey PRIMARY KEY (id);
ALTER TABLE ONLY ci_pipeline_artifacts ALTER TABLE ONLY ci_pipeline_artifacts
ADD CONSTRAINT ci_pipeline_artifacts_pkey PRIMARY KEY (id); ADD CONSTRAINT ci_pipeline_artifacts_pkey PRIMARY KEY (id);
...@@ -22534,6 +22546,8 @@ CREATE UNIQUE INDEX index_ci_job_variables_on_key_and_job_id ON ci_job_variables ...@@ -22534,6 +22546,8 @@ CREATE UNIQUE INDEX index_ci_job_variables_on_key_and_job_id ON ci_job_variables
CREATE UNIQUE INDEX index_ci_namespace_monthly_usages_on_namespace_id_and_date ON ci_namespace_monthly_usages USING btree (namespace_id, date); CREATE UNIQUE INDEX index_ci_namespace_monthly_usages_on_namespace_id_and_date ON ci_namespace_monthly_usages USING btree (namespace_id, date);
CREATE UNIQUE INDEX index_ci_pending_builds_on_build_id ON ci_pending_builds USING btree (build_id);
CREATE INDEX index_ci_pending_builds_on_project_id ON ci_pending_builds USING btree (project_id); CREATE INDEX index_ci_pending_builds_on_project_id ON ci_pending_builds USING btree (project_id);
CREATE INDEX index_ci_pipeline_artifacts_failed_verification ON ci_pipeline_artifacts USING btree (verification_retry_at NULLS FIRST) WHERE (verification_state = 3); CREATE INDEX index_ci_pipeline_artifacts_failed_verification ON ci_pipeline_artifacts USING btree (verification_retry_at NULLS FIRST) WHERE (verification_state = 3);
...@@ -47,10 +47,10 @@ RSpec.describe Ci::UpdateBuildQueueService do ...@@ -47,10 +47,10 @@ RSpec.describe Ci::UpdateBuildQueueService do
::Ci::PendingBuild.create!(build: build, project: project) ::Ci::PendingBuild.create!(build: build, project: project)
end end
it 'does nothing and returns updated build id' do it 'does nothing and returns nil' do
queued = subject.push(build, transition) queued = subject.push(build, transition)
expect(queued).to eq build.id expect(queued).to be_nil
end end
end 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