From 5150d56564451aa26a97bdf90b81ffa248565cb2 Mon Sep 17 00:00:00 2001
From: Maxime Orefice <morefice@gitlab.com>
Date: Thu, 8 Feb 2024 10:50:53 +0100
Subject: [PATCH] Prepare partitioning constraint for ci_stages

This commit enqueues a job which will create a new constraint this
weekend on ci_job_artifacts. This is a prerequisite in order to
partition this table.

Changelog: other
---
 ...d_partitioning_constraint_for_ci_stages.rb | 31 +++++++++++++++++++
 db/schema_migrations/20240208094017           |  1 +
 db/structure.sql                              |  3 ++
 spec/models/ci/build_runner_session_spec.rb   |  4 +--
 spec/models/ci/build_spec.rb                  |  4 +--
 spec/models/ci/build_trace_metadata_spec.rb   |  4 +--
 spec/models/ci/job_variable_spec.rb           |  6 ++--
 spec/models/ci/pending_build_spec.rb          |  9 +++---
 spec/models/ci/resource_group_spec.rb         |  6 ++--
 spec/models/ci/running_build_spec.rb          |  7 ++---
 spec/models/ci/sources/pipeline_spec.rb       |  6 ++--
 spec/requests/api/commit_statuses_spec.rb     |  7 +++--
 .../ci/create_commit_status_service_spec.rb   |  7 +++--
 13 files changed, 65 insertions(+), 30 deletions(-)
 create mode 100644 db/post_migrate/20240208094017_add_partitioning_constraint_for_ci_stages.rb
 create mode 100644 db/schema_migrations/20240208094017

diff --git a/db/post_migrate/20240208094017_add_partitioning_constraint_for_ci_stages.rb b/db/post_migrate/20240208094017_add_partitioning_constraint_for_ci_stages.rb
new file mode 100644
index 0000000000000..4b6d9c814a56d
--- /dev/null
+++ b/db/post_migrate/20240208094017_add_partitioning_constraint_for_ci_stages.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+class AddPartitioningConstraintForCiStages < Gitlab::Database::Migration[2.2]
+  include Gitlab::Database::PartitioningMigrationHelpers::TableManagementHelpers
+
+  milestone '16.10'
+  disable_ddl_transaction!
+  TABLE_NAME = :ci_stages
+  PARENT_TABLE_NAME = :p_ci_stages
+  FIRST_PARTITION = [100, 101]
+  PARTITION_COLUMN = :partition_id
+
+  def up
+    prepare_constraint_for_list_partitioning(
+      table_name: TABLE_NAME,
+      partitioning_column: PARTITION_COLUMN,
+      parent_table_name: PARENT_TABLE_NAME,
+      initial_partitioning_value: FIRST_PARTITION,
+      async: true
+    )
+  end
+
+  def down
+    revert_preparing_constraint_for_list_partitioning(
+      table_name: TABLE_NAME,
+      partitioning_column: PARTITION_COLUMN,
+      parent_table_name: PARENT_TABLE_NAME,
+      initial_partitioning_value: FIRST_PARTITION
+    )
+  end
+end
diff --git a/db/schema_migrations/20240208094017 b/db/schema_migrations/20240208094017
new file mode 100644
index 0000000000000..4ac88a47c6560
--- /dev/null
+++ b/db/schema_migrations/20240208094017
@@ -0,0 +1 @@
+74cf13414b035b623dd44401794ac00e0377b1340110a9719f4f9e73cf6a21b4
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index 1efe8fff1098b..1acbecf9e9a28 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -30338,6 +30338,9 @@ ALTER TABLE ONLY pages_domain_acme_orders
 ALTER TABLE ONLY pages_domains
     ADD CONSTRAINT pages_domains_pkey PRIMARY KEY (id);
 
+ALTER TABLE ci_stages
+    ADD CONSTRAINT partitioning_constraint CHECK ((partition_id = ANY (ARRAY[(100)::bigint, (101)::bigint]))) NOT VALID;
+
 ALTER TABLE ONLY path_locks
     ADD CONSTRAINT path_locks_pkey PRIMARY KEY (id);
 
diff --git a/spec/models/ci/build_runner_session_spec.rb b/spec/models/ci/build_runner_session_spec.rb
index dac7edbe6cc54..198eb9364970c 100644
--- a/spec/models/ci/build_runner_session_spec.rb
+++ b/spec/models/ci/build_runner_session_spec.rb
@@ -183,11 +183,11 @@
     let(:build_runner_session) { create(:ci_build_runner_session, build: new_build) }
 
     before do
-      stub_current_partition_id
+      stub_current_partition_id(ci_testing_partition_id_for_check_constraints)
     end
 
     it 'assigns the same partition id as the one that build has' do
-      expect(build_runner_session.partition_id).to eq(ci_testing_partition_id)
+      expect(build_runner_session.partition_id).to eq(ci_testing_partition_id_for_check_constraints)
     end
   end
 end
diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb
index 8741d347c3618..67d163a34044a 100644
--- a/spec/models/ci/build_spec.rb
+++ b/spec/models/ci/build_spec.rb
@@ -5601,14 +5601,14 @@ def run_job_without_exception
     let(:ci_build) { create(:ci_build, pipeline: new_pipeline) }
 
     before do
-      stub_current_partition_id
+      stub_current_partition_id(ci_testing_partition_id_for_check_constraints)
     end
 
     it 'includes partition_id in the token prefix' do
       prefix = ci_build.token.match(/^glcbt-([\h]+)_/)
       partition_prefix = prefix[1].to_i(16)
 
-      expect(partition_prefix).to eq(ci_testing_partition_id)
+      expect(partition_prefix).to eq(ci_testing_partition_id_for_check_constraints)
     end
   end
 
diff --git a/spec/models/ci/build_trace_metadata_spec.rb b/spec/models/ci/build_trace_metadata_spec.rb
index 866d94b4cbed7..667f519ecf50e 100644
--- a/spec/models/ci/build_trace_metadata_spec.rb
+++ b/spec/models/ci/build_trace_metadata_spec.rb
@@ -169,11 +169,11 @@
     let(:metadata) { create(:ci_build_trace_metadata, build: new_build) }
 
     before do
-      stub_current_partition_id
+      stub_current_partition_id(ci_testing_partition_id_for_check_constraints)
     end
 
     it 'assigns the same partition id as the one that build has' do
-      expect(metadata.partition_id).to eq(ci_testing_partition_id)
+      expect(metadata.partition_id).to eq(ci_testing_partition_id_for_check_constraints)
     end
   end
 end
diff --git a/spec/models/ci/job_variable_spec.rb b/spec/models/ci/job_variable_spec.rb
index a56e6b6be43ad..8b1191a393088 100644
--- a/spec/models/ci/job_variable_spec.rb
+++ b/spec/models/ci/job_variable_spec.rb
@@ -49,15 +49,15 @@
       let(:job_variable_2) { build(:ci_job_variable, job: ci_build) }
 
       before do
-        stub_current_partition_id
+        stub_current_partition_id(ci_testing_partition_id_for_check_constraints)
       end
 
       it 'creates job variables successfully', :aggregate_failures do
         described_class.bulk_insert!([job_variable, job_variable_2])
 
         expect(described_class.count).to eq(2)
-        expect(described_class.first.partition_id).to eq(ci_testing_partition_id)
-        expect(described_class.last.partition_id).to eq(ci_testing_partition_id)
+        expect(described_class.first.partition_id).to eq(ci_testing_partition_id_for_check_constraints)
+        expect(described_class.last.partition_id).to eq(ci_testing_partition_id_for_check_constraints)
       end
     end
   end
diff --git a/spec/models/ci/pending_build_spec.rb b/spec/models/ci/pending_build_spec.rb
index 331522070df17..076aad34a9429 100644
--- a/spec/models/ci/pending_build_spec.rb
+++ b/spec/models/ci/pending_build_spec.rb
@@ -2,7 +2,7 @@
 
 require 'spec_helper'
 
-RSpec.describe Ci::PendingBuild do
+RSpec.describe Ci::PendingBuild, feature_category: :continuous_integration do
   let_it_be(:project) { create(:project) }
   let_it_be(:pipeline) { create(:ci_pipeline, project: project) }
 
@@ -200,21 +200,20 @@
     include Ci::PartitioningHelpers
 
     before do
-      stub_current_partition_id
+      stub_current_partition_id(ci_testing_partition_id_for_check_constraints)
     end
 
     let(:new_pipeline ) { create(:ci_pipeline, project: pipeline.project) }
     let(:new_build) { create(:ci_build, pipeline: new_pipeline) }
 
     it 'assigns the same partition id as the one that build has', :aggregate_failures do
-      expect(new_build.partition_id).to eq ci_testing_partition_id
-      expect(new_build.partition_id).not_to eq pipeline.partition_id
+      expect(new_build.partition_id).to eq ci_testing_partition_id_for_check_constraints
 
       described_class.upsert_from_build!(build)
       described_class.upsert_from_build!(new_build)
 
       expect(build.reload.queuing_entry.partition_id).to eq pipeline.partition_id
-      expect(new_build.reload.queuing_entry.partition_id).to eq ci_testing_partition_id
+      expect(new_build.reload.queuing_entry.partition_id).to eq ci_testing_partition_id_for_check_constraints
     end
   end
 
diff --git a/spec/models/ci/resource_group_spec.rb b/spec/models/ci/resource_group_spec.rb
index 9e98cc884de20..e2aaeb2a18e2c 100644
--- a/spec/models/ci/resource_group_spec.rb
+++ b/spec/models/ci/resource_group_spec.rb
@@ -2,7 +2,7 @@
 
 require 'spec_helper'
 
-RSpec.describe Ci::ResourceGroup do
+RSpec.describe Ci::ResourceGroup, feature_category: :continuous_delivery do
   let_it_be(:group) { create(:group) }
 
   it_behaves_like 'cleanup by a loose foreign key' do
@@ -37,7 +37,7 @@
     include Ci::PartitioningHelpers
 
     before do
-      stub_current_partition_id
+      stub_current_partition_id(ci_testing_partition_id_for_check_constraints)
     end
 
     subject { resource_group.assign_resource_to(build) }
@@ -78,7 +78,7 @@
     include Ci::PartitioningHelpers
 
     before do
-      stub_current_partition_id
+      stub_current_partition_id(ci_testing_partition_id_for_check_constraints)
     end
 
     subject { resource_group.release_resource_from(build) }
diff --git a/spec/models/ci/running_build_spec.rb b/spec/models/ci/running_build_spec.rb
index 7f254bd235ce7..0a81eaf9ac062 100644
--- a/spec/models/ci/running_build_spec.rb
+++ b/spec/models/ci/running_build_spec.rb
@@ -54,21 +54,20 @@
     include Ci::PartitioningHelpers
 
     before do
-      stub_current_partition_id
+      stub_current_partition_id(ci_testing_partition_id_for_check_constraints)
     end
 
     let(:new_pipeline ) { create(:ci_pipeline, project: pipeline.project) }
     let(:new_build) { create(:ci_build, :running, pipeline: new_pipeline, runner: runner) }
 
     it 'assigns the same partition id as the one that build has', :aggregate_failures do
-      expect(new_build.partition_id).to eq ci_testing_partition_id
-      expect(new_build.partition_id).not_to eq pipeline.partition_id
+      expect(new_build.partition_id).to eq ci_testing_partition_id_for_check_constraints
 
       described_class.upsert_shared_runner_build!(build)
       described_class.upsert_shared_runner_build!(new_build)
 
       expect(build.reload.runtime_metadata.partition_id).to eq pipeline.partition_id
-      expect(new_build.reload.runtime_metadata.partition_id).to eq ci_testing_partition_id
+      expect(new_build.reload.runtime_metadata.partition_id).to eq ci_testing_partition_id_for_check_constraints
     end
   end
 
diff --git a/spec/models/ci/sources/pipeline_spec.rb b/spec/models/ci/sources/pipeline_spec.rb
index 036708ed61ee9..6bc848fadb8ca 100644
--- a/spec/models/ci/sources/pipeline_spec.rb
+++ b/spec/models/ci/sources/pipeline_spec.rb
@@ -44,12 +44,12 @@
     let(:source_pipeline) { create(:ci_sources_pipeline, pipeline: new_pipeline) }
 
     before do
-      stub_current_partition_id
+      stub_current_partition_id(ci_testing_partition_id_for_check_constraints)
     end
 
     it 'assigns partition_id and source_partition_id from pipeline and source_job', :aggregate_failures do
-      expect(source_pipeline.partition_id).to eq(ci_testing_partition_id)
-      expect(source_pipeline.source_partition_id).to eq(ci_testing_partition_id)
+      expect(source_pipeline.partition_id).to eq(ci_testing_partition_id_for_check_constraints)
+      expect(source_pipeline.source_partition_id).to eq(ci_testing_partition_id_for_check_constraints)
     end
   end
 end
diff --git a/spec/requests/api/commit_statuses_spec.rb b/spec/requests/api/commit_statuses_spec.rb
index 9247d9366b2a6..c49e544c17520 100644
--- a/spec/requests/api/commit_statuses_spec.rb
+++ b/spec/requests/api/commit_statuses_spec.rb
@@ -543,11 +543,12 @@ def create_status(commit, opts = {})
       end
 
       context 'with partitions', :ci_partitionable do
-        let(:current_partition_id) { ci_testing_partition_id }
+        include Ci::PartitioningHelpers
+
+        let(:current_partition_id) { ci_testing_partition_id_for_check_constraints }
 
         before do
-          allow(Ci::Pipeline)
-            .to receive(:current_partition_value) { current_partition_id }
+          stub_current_partition_id(ci_testing_partition_id_for_check_constraints)
         end
 
         it 'creates records in the current partition' do
diff --git a/spec/services/ci/create_commit_status_service_spec.rb b/spec/services/ci/create_commit_status_service_spec.rb
index ec200e24c8fb9..3c7346e3e2e7a 100644
--- a/spec/services/ci/create_commit_status_service_spec.rb
+++ b/spec/services/ci/create_commit_status_service_spec.rb
@@ -404,12 +404,13 @@
   end
 
   context 'with partitions', :ci_partitionable do
-    let(:current_partition_id) { ci_testing_partition_id }
+    include Ci::PartitioningHelpers
+
+    let(:current_partition_id) { ci_testing_partition_id_for_check_constraints }
     let(:params) { { state: 'running' } }
 
     before do
-      allow(Ci::Pipeline)
-        .to receive(:current_partition_value) { current_partition_id }
+      stub_current_partition_id(ci_testing_partition_id_for_check_constraints)
     end
 
     it 'creates records in the current partition' do
-- 
GitLab