From b7fefff8c9934c72cecfeb4cfd487c51efa2e220 Mon Sep 17 00:00:00 2001 From: sholaq Date: Tue, 4 Nov 2025 10:46:39 +0000 Subject: [PATCH 01/11] Adds noteable migration to workflows --- ...0251104103536_add_noteable_to_workflows.rb | 27 +++++++++++++++++++ db/schema_migrations/20251104103536 | 1 + db/structure.sql | 3 +++ ee/app/models/ai/duo_workflows/workflow.rb | 1 + .../models/ai/duo_workflows/workflow_spec.rb | 1 + 5 files changed, 33 insertions(+) create mode 100644 db/migrate/20251104103536_add_noteable_to_workflows.rb create mode 100644 db/schema_migrations/20251104103536 diff --git a/db/migrate/20251104103536_add_noteable_to_workflows.rb b/db/migrate/20251104103536_add_noteable_to_workflows.rb new file mode 100644 index 00000000000000..885fe2a3358358 --- /dev/null +++ b/db/migrate/20251104103536_add_noteable_to_workflows.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class AddNoteableToWorkflows < Gitlab::Database::Migration[2.3] + disable_ddl_transaction! + + milestone '18.6' + + INDEX_NAME = 'index_duo_workflows_workflows_on_noteable' + + def up + add_column :duo_workflows_workflows, :noteable_type, :text, if_not_exists: true + add_column :duo_workflows_workflows, :noteable_id, :bigint, if_not_exists: true + + add_text_limit :duo_workflows_workflows, :noteable_type, 255 + + add_concurrent_index :duo_workflows_workflows, + [:noteable_type, :noteable_id], + name: INDEX_NAME + end + + def down + remove_concurrent_index_by_name :duo_workflows_workflows, INDEX_NAME + + remove_column :duo_workflows_workflows, :noteable_type, if_exists: true + remove_column :duo_workflows_workflows, :noteable_id, if_exists: true + end +end diff --git a/db/schema_migrations/20251104103536 b/db/schema_migrations/20251104103536 new file mode 100644 index 00000000000000..9ec6fd7f056350 --- /dev/null +++ b/db/schema_migrations/20251104103536 @@ -0,0 +1 @@ +8c7f79cb69ea17338738561e68aae87a87e3f098a79ff25353fd9f69e3c57986 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index 39ec20faa13dc5..bd3e099305437d 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -16650,6 +16650,9 @@ CREATE TABLE duo_workflows_workflows ( environment smallint, namespace_id bigint, ai_catalog_item_version_id bigint, + noteable_type text, + noteable_id bigint, + CONSTRAINT check_02c7816e54 CHECK ((char_length(noteable_type) <= 255)), CONSTRAINT check_30ca07a4ef CHECK ((char_length(goal) <= 16384)), CONSTRAINT check_3a9162f1ae CHECK ((char_length(image) <= 2048)), CONSTRAINT check_73884a5839 CHECK ((num_nonnulls(namespace_id, project_id) = 1)), diff --git a/ee/app/models/ai/duo_workflows/workflow.rb b/ee/app/models/ai/duo_workflows/workflow.rb index 194f4a0cbdcaa1..563c6c691f65c5 100644 --- a/ee/app/models/ai/duo_workflows/workflow.rb +++ b/ee/app/models/ai/duo_workflows/workflow.rb @@ -14,6 +14,7 @@ class Workflow < ::ApplicationRecord belongs_to :project, optional: true belongs_to :namespace, optional: true belongs_to :ai_catalog_item_version, optional: true, class_name: 'Ai::Catalog::ItemVersion' + belongs_to :noteable, polymorphic: true, optional: true has_many :checkpoints, class_name: 'Ai::DuoWorkflows::Checkpoint' has_many :checkpoint_writes, class_name: 'Ai::DuoWorkflows::CheckpointWrite' diff --git a/ee/spec/models/ai/duo_workflows/workflow_spec.rb b/ee/spec/models/ai/duo_workflows/workflow_spec.rb index 3eb7095501510b..3330af8b8bd6f1 100644 --- a/ee/spec/models/ai/duo_workflows/workflow_spec.rb +++ b/ee/spec/models/ai/duo_workflows/workflow_spec.rb @@ -14,6 +14,7 @@ it { is_expected.to have_many(:checkpoint_writes).class_name('Ai::DuoWorkflows::CheckpointWrite') } it { is_expected.to belong_to(:project).optional } it { is_expected.to belong_to(:namespace).optional } + it { is_expected.to belong_to(:noteable).optional } it { is_expected.to belong_to(:ai_catalog_item_version).optional } it { is_expected.to belong_to(:ai_catalog_item_version).class_name('Ai::Catalog::ItemVersion') } -- GitLab From bd5922b31e4c39de1d14d8a2593fdab0572e1e55 Mon Sep 17 00:00:00 2001 From: sholaq Date: Tue, 4 Nov 2025 12:54:12 +0000 Subject: [PATCH 02/11] Adds noteable_id to ignored_fk_columns_map --- spec/db/schema_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/db/schema_spec.rb b/spec/db/schema_spec.rb index 7e6336988fdaa8..253bd6734c42ab 100644 --- a/spec/db/schema_spec.rb +++ b/spec/db/schema_spec.rb @@ -114,6 +114,7 @@ deployments: %w[deployable_id user_id], deployment_merge_requests: %w[project_id], draft_notes: %w[discussion_id commit_id], + duo_workflows_workflows: %w[noteable_id], epics: %w[updated_by_id last_edited_by_id state_id], events: %w[target_id], forked_project_links: %w[forked_from_project_id], -- GitLab From f11d3bf9dbef9d8452a796aff4a8bfb0d3500441 Mon Sep 17 00:00:00 2001 From: sholaq Date: Tue, 4 Nov 2025 15:16:19 +0000 Subject: [PATCH 03/11] Update structure.sql with index_duo_workflows_workflows_on_noteable --- db/structure.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/db/structure.sql b/db/structure.sql index bd3e099305437d..c0eddfa799202a 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -40252,6 +40252,8 @@ CREATE INDEX index_duo_workflows_workflows_on_ai_catalog_item_version_id ON duo_ CREATE INDEX index_duo_workflows_workflows_on_namespace_id ON duo_workflows_workflows USING btree (namespace_id); +CREATE INDEX index_duo_workflows_workflows_on_noteable ON duo_workflows_workflows USING btree (noteable_type, noteable_id); + CREATE INDEX index_duo_workflows_workflows_on_project_id ON duo_workflows_workflows USING btree (project_id); CREATE INDEX index_duo_workflows_workflows_on_user_id ON duo_workflows_workflows USING btree (user_id); -- GitLab From 776475a2f03e74b0b748d13d98d5446db94becc1 Mon Sep 17 00:00:00 2001 From: Shola Quadri Date: Thu, 6 Nov 2025 19:03:48 +0000 Subject: [PATCH 04/11] Apply 1 suggestion(s) to 1 file(s) Co-authored-by: Max Orefice --- db/migrate/20251104103536_add_noteable_to_workflows.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/db/migrate/20251104103536_add_noteable_to_workflows.rb b/db/migrate/20251104103536_add_noteable_to_workflows.rb index 885fe2a3358358..188308c2c00b98 100644 --- a/db/migrate/20251104103536_add_noteable_to_workflows.rb +++ b/db/migrate/20251104103536_add_noteable_to_workflows.rb @@ -8,8 +8,10 @@ class AddNoteableToWorkflows < Gitlab::Database::Migration[2.3] INDEX_NAME = 'index_duo_workflows_workflows_on_noteable' def up - add_column :duo_workflows_workflows, :noteable_type, :text, if_not_exists: true - add_column :duo_workflows_workflows, :noteable_id, :bigint, if_not_exists: true + with_lock_retries do + add_column :duo_workflows_workflows, :noteable_type, :text, if_not_exists: true + add_column :duo_workflows_workflows, :noteable_id, :bigint, if_not_exists: true + end add_text_limit :duo_workflows_workflows, :noteable_type, 255 -- GitLab From 518239c07826407730de7d8e074ce03df8945f1a Mon Sep 17 00:00:00 2001 From: Shola Quadri Date: Thu, 6 Nov 2025 19:04:16 +0000 Subject: [PATCH 05/11] Apply 1 suggestion(s) to 1 file(s) Co-authored-by: Max Orefice --- db/migrate/20251104103536_add_noteable_to_workflows.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/db/migrate/20251104103536_add_noteable_to_workflows.rb b/db/migrate/20251104103536_add_noteable_to_workflows.rb index 188308c2c00b98..6acbbd4585d9e0 100644 --- a/db/migrate/20251104103536_add_noteable_to_workflows.rb +++ b/db/migrate/20251104103536_add_noteable_to_workflows.rb @@ -21,9 +21,9 @@ def up end def down - remove_concurrent_index_by_name :duo_workflows_workflows, INDEX_NAME - - remove_column :duo_workflows_workflows, :noteable_type, if_exists: true - remove_column :duo_workflows_workflows, :noteable_id, if_exists: true + with_lock_retries do + remove_column :duo_workflows_workflows, :noteable_type, if_exists: true + remove_column :duo_workflows_workflows, :noteable_id, if_exists: true + end end end -- GitLab From abc5461f8bf14419bbc6773686d47b884e6e85a1 Mon Sep 17 00:00:00 2001 From: sholaq Date: Fri, 7 Nov 2025 21:17:41 +0000 Subject: [PATCH 06/11] Rolls back polymorphic migration --- db/schema_migrations/20251104103536 | 1 - db/structure.sql | 5 ----- ee/app/models/ai/duo_workflows/workflow.rb | 1 - ee/spec/models/ai/duo_workflows/workflow_spec.rb | 1 - spec/db/schema_spec.rb | 1 - 5 files changed, 9 deletions(-) delete mode 100644 db/schema_migrations/20251104103536 diff --git a/db/schema_migrations/20251104103536 b/db/schema_migrations/20251104103536 deleted file mode 100644 index 9ec6fd7f056350..00000000000000 --- a/db/schema_migrations/20251104103536 +++ /dev/null @@ -1 +0,0 @@ -8c7f79cb69ea17338738561e68aae87a87e3f098a79ff25353fd9f69e3c57986 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index c0eddfa799202a..39ec20faa13dc5 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -16650,9 +16650,6 @@ CREATE TABLE duo_workflows_workflows ( environment smallint, namespace_id bigint, ai_catalog_item_version_id bigint, - noteable_type text, - noteable_id bigint, - CONSTRAINT check_02c7816e54 CHECK ((char_length(noteable_type) <= 255)), CONSTRAINT check_30ca07a4ef CHECK ((char_length(goal) <= 16384)), CONSTRAINT check_3a9162f1ae CHECK ((char_length(image) <= 2048)), CONSTRAINT check_73884a5839 CHECK ((num_nonnulls(namespace_id, project_id) = 1)), @@ -40252,8 +40249,6 @@ CREATE INDEX index_duo_workflows_workflows_on_ai_catalog_item_version_id ON duo_ CREATE INDEX index_duo_workflows_workflows_on_namespace_id ON duo_workflows_workflows USING btree (namespace_id); -CREATE INDEX index_duo_workflows_workflows_on_noteable ON duo_workflows_workflows USING btree (noteable_type, noteable_id); - CREATE INDEX index_duo_workflows_workflows_on_project_id ON duo_workflows_workflows USING btree (project_id); CREATE INDEX index_duo_workflows_workflows_on_user_id ON duo_workflows_workflows USING btree (user_id); diff --git a/ee/app/models/ai/duo_workflows/workflow.rb b/ee/app/models/ai/duo_workflows/workflow.rb index 563c6c691f65c5..194f4a0cbdcaa1 100644 --- a/ee/app/models/ai/duo_workflows/workflow.rb +++ b/ee/app/models/ai/duo_workflows/workflow.rb @@ -14,7 +14,6 @@ class Workflow < ::ApplicationRecord belongs_to :project, optional: true belongs_to :namespace, optional: true belongs_to :ai_catalog_item_version, optional: true, class_name: 'Ai::Catalog::ItemVersion' - belongs_to :noteable, polymorphic: true, optional: true has_many :checkpoints, class_name: 'Ai::DuoWorkflows::Checkpoint' has_many :checkpoint_writes, class_name: 'Ai::DuoWorkflows::CheckpointWrite' diff --git a/ee/spec/models/ai/duo_workflows/workflow_spec.rb b/ee/spec/models/ai/duo_workflows/workflow_spec.rb index 3330af8b8bd6f1..3eb7095501510b 100644 --- a/ee/spec/models/ai/duo_workflows/workflow_spec.rb +++ b/ee/spec/models/ai/duo_workflows/workflow_spec.rb @@ -14,7 +14,6 @@ it { is_expected.to have_many(:checkpoint_writes).class_name('Ai::DuoWorkflows::CheckpointWrite') } it { is_expected.to belong_to(:project).optional } it { is_expected.to belong_to(:namespace).optional } - it { is_expected.to belong_to(:noteable).optional } it { is_expected.to belong_to(:ai_catalog_item_version).optional } it { is_expected.to belong_to(:ai_catalog_item_version).class_name('Ai::Catalog::ItemVersion') } diff --git a/spec/db/schema_spec.rb b/spec/db/schema_spec.rb index 253bd6734c42ab..7e6336988fdaa8 100644 --- a/spec/db/schema_spec.rb +++ b/spec/db/schema_spec.rb @@ -114,7 +114,6 @@ deployments: %w[deployable_id user_id], deployment_merge_requests: %w[project_id], draft_notes: %w[discussion_id commit_id], - duo_workflows_workflows: %w[noteable_id], epics: %w[updated_by_id last_edited_by_id state_id], events: %w[target_id], forked_project_links: %w[forked_from_project_id], -- GitLab From eec2990ceb5e0c2fff766567a6f3eeb01d2d40f4 Mon Sep 17 00:00:00 2001 From: sholaq Date: Fri, 7 Nov 2025 21:36:23 +0000 Subject: [PATCH 07/11] Implements FK approach --- ...0251104103536_add_noteable_to_workflows.rb | 29 ------------ ...0251107212441_add_noteable_to_workflows.rb | 46 +++++++++++++++++++ db/schema_migrations/20251107212441 | 1 + db/structure.sql | 15 +++++- ee/app/models/ai/duo_workflows/workflow.rb | 2 + .../models/ai/duo_workflows/workflow_spec.rb | 2 + 6 files changed, 65 insertions(+), 30 deletions(-) delete mode 100644 db/migrate/20251104103536_add_noteable_to_workflows.rb create mode 100644 db/migrate/20251107212441_add_noteable_to_workflows.rb create mode 100644 db/schema_migrations/20251107212441 diff --git a/db/migrate/20251104103536_add_noteable_to_workflows.rb b/db/migrate/20251104103536_add_noteable_to_workflows.rb deleted file mode 100644 index 6acbbd4585d9e0..00000000000000 --- a/db/migrate/20251104103536_add_noteable_to_workflows.rb +++ /dev/null @@ -1,29 +0,0 @@ -# frozen_string_literal: true - -class AddNoteableToWorkflows < Gitlab::Database::Migration[2.3] - disable_ddl_transaction! - - milestone '18.6' - - INDEX_NAME = 'index_duo_workflows_workflows_on_noteable' - - def up - with_lock_retries do - add_column :duo_workflows_workflows, :noteable_type, :text, if_not_exists: true - add_column :duo_workflows_workflows, :noteable_id, :bigint, if_not_exists: true - end - - add_text_limit :duo_workflows_workflows, :noteable_type, 255 - - add_concurrent_index :duo_workflows_workflows, - [:noteable_type, :noteable_id], - name: INDEX_NAME - end - - def down - with_lock_retries do - remove_column :duo_workflows_workflows, :noteable_type, if_exists: true - remove_column :duo_workflows_workflows, :noteable_id, if_exists: true - end - end -end diff --git a/db/migrate/20251107212441_add_noteable_to_workflows.rb b/db/migrate/20251107212441_add_noteable_to_workflows.rb new file mode 100644 index 00000000000000..2e909ae4a53287 --- /dev/null +++ b/db/migrate/20251107212441_add_noteable_to_workflows.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +class AddNoteableToWorkflows < Gitlab::Database::Migration[2.3] + disable_ddl_transaction! + + milestone '18.6' + + TABLE = :duo_workflows_workflows + CONSTRAINT_NAME = 'check_workflows_single_noteable' + ISSUE_ID_INDEX = 'index_duo_workflows_workflows_on_issue_id' + MR_ID_INDEX = 'index_duo_workflows_workflows_on_merge_request_id' + + def up + with_lock_retries do + add_column TABLE, :issue_id, :bigint, if_not_exists: true + add_column TABLE, :merge_request_id, :bigint, if_not_exists: true + end + + add_concurrent_index TABLE, :issue_id, name: ISSUE_ID_INDEX + add_concurrent_index TABLE, :merge_request_id, name: MR_ID_INDEX + + add_concurrent_foreign_key TABLE, :issues, + column: :issue_id, on_delete: :cascade + add_concurrent_foreign_key TABLE, :merge_requests, + column: :merge_request_id, on_delete: :cascade + + add_check_constraint TABLE, + 'num_nonnulls(issue_id, merge_request_id) <= 1', + CONSTRAINT_NAME + end + + def down + remove_check_constraint TABLE, CONSTRAINT_NAME + + remove_foreign_key_if_exists TABLE, column: :issue_id + remove_foreign_key_if_exists TABLE, column: :merge_request_id + + remove_concurrent_index_by_name TABLE, ISSUE_ID_INDEX + remove_concurrent_index_by_name TABLE, MR_ID_INDEX + + with_lock_retries do + remove_column TABLE, :issue_id, if_exists: true + remove_column TABLE, :merge_request_id, if_exists: true + end + end +end diff --git a/db/schema_migrations/20251107212441 b/db/schema_migrations/20251107212441 new file mode 100644 index 00000000000000..c8370e81bb4fcd --- /dev/null +++ b/db/schema_migrations/20251107212441 @@ -0,0 +1 @@ +7883b12df85ec12420172a3da325b2c0ff75f99936f974f008fc1a86e7f628b7 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index 39ec20faa13dc5..b4a9b21fd0c19d 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -16650,10 +16650,13 @@ CREATE TABLE duo_workflows_workflows ( environment smallint, namespace_id bigint, ai_catalog_item_version_id bigint, + issue_id bigint, + merge_request_id bigint, CONSTRAINT check_30ca07a4ef CHECK ((char_length(goal) <= 16384)), CONSTRAINT check_3a9162f1ae CHECK ((char_length(image) <= 2048)), CONSTRAINT check_73884a5839 CHECK ((num_nonnulls(namespace_id, project_id) = 1)), - CONSTRAINT check_ec723e2a1a CHECK ((char_length(workflow_definition) <= 255)) + CONSTRAINT check_ec723e2a1a CHECK ((char_length(workflow_definition) <= 255)), + CONSTRAINT check_workflows_single_noteable CHECK ((num_nonnulls(issue_id, merge_request_id) <= 1)) ); CREATE SEQUENCE duo_workflows_workflows_id_seq @@ -40247,6 +40250,10 @@ CREATE INDEX index_duo_workflows_events_on_workflow_id ON duo_workflows_events U CREATE INDEX index_duo_workflows_workflows_on_ai_catalog_item_version_id ON duo_workflows_workflows USING btree (ai_catalog_item_version_id); +CREATE INDEX index_duo_workflows_workflows_on_issue_id ON duo_workflows_workflows USING btree (issue_id); + +CREATE INDEX index_duo_workflows_workflows_on_merge_request_id ON duo_workflows_workflows USING btree (merge_request_id); + CREATE INDEX index_duo_workflows_workflows_on_namespace_id ON duo_workflows_workflows USING btree (namespace_id); CREATE INDEX index_duo_workflows_workflows_on_project_id ON duo_workflows_workflows USING btree (project_id); @@ -50242,6 +50249,9 @@ ALTER TABLE ONLY packages_helm_file_metadata ALTER TABLE ONLY bulk_import_failures ADD CONSTRAINT fk_dad28985ee FOREIGN KEY (namespace_id) REFERENCES namespaces(id) ON DELETE CASCADE; +ALTER TABLE ONLY duo_workflows_workflows + ADD CONSTRAINT fk_daffbfef9a FOREIGN KEY (issue_id) REFERENCES issues(id) ON DELETE CASCADE; + ALTER TABLE ONLY project_topics ADD CONSTRAINT fk_db13576296 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE; @@ -50413,6 +50423,9 @@ ALTER TABLE ONLY merge_requests_compliance_violations ALTER TABLE ONLY issue_emails ADD CONSTRAINT fk_ed0f4c4b51 FOREIGN KEY (namespace_id) REFERENCES namespaces(id) ON DELETE CASCADE; +ALTER TABLE ONLY duo_workflows_workflows + ADD CONSTRAINT fk_ed58162ace FOREIGN KEY (merge_request_id) REFERENCES merge_requests(id) ON DELETE CASCADE; + ALTER TABLE ONLY cluster_agent_migrations ADD CONSTRAINT fk_ed8ffda028 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE; diff --git a/ee/app/models/ai/duo_workflows/workflow.rb b/ee/app/models/ai/duo_workflows/workflow.rb index 194f4a0cbdcaa1..c0f61ddc142e6f 100644 --- a/ee/app/models/ai/duo_workflows/workflow.rb +++ b/ee/app/models/ai/duo_workflows/workflow.rb @@ -14,6 +14,8 @@ class Workflow < ::ApplicationRecord belongs_to :project, optional: true belongs_to :namespace, optional: true belongs_to :ai_catalog_item_version, optional: true, class_name: 'Ai::Catalog::ItemVersion' + belongs_to :issue, optional: true + belongs_to :merge_request, optional: true has_many :checkpoints, class_name: 'Ai::DuoWorkflows::Checkpoint' has_many :checkpoint_writes, class_name: 'Ai::DuoWorkflows::CheckpointWrite' diff --git a/ee/spec/models/ai/duo_workflows/workflow_spec.rb b/ee/spec/models/ai/duo_workflows/workflow_spec.rb index 3eb7095501510b..3e9af9234dd073 100644 --- a/ee/spec/models/ai/duo_workflows/workflow_spec.rb +++ b/ee/spec/models/ai/duo_workflows/workflow_spec.rb @@ -14,6 +14,8 @@ it { is_expected.to have_many(:checkpoint_writes).class_name('Ai::DuoWorkflows::CheckpointWrite') } it { is_expected.to belong_to(:project).optional } it { is_expected.to belong_to(:namespace).optional } + it { is_expected.to belong_to(:issue).optional } + it { is_expected.to belong_to(:merge_request).optional } it { is_expected.to belong_to(:ai_catalog_item_version).optional } it { is_expected.to belong_to(:ai_catalog_item_version).class_name('Ai::Catalog::ItemVersion') } -- GitLab From 380bf47b3345a1e6408b4a0fbb6c00fd58da0232 Mon Sep 17 00:00:00 2001 From: sholaq Date: Tue, 11 Nov 2025 11:02:46 +0000 Subject: [PATCH 08/11] Removes combined migration --- ...0251107212441_add_noteable_to_workflows.rb | 46 ------------------- db/schema_migrations/20251107212441 | 1 - db/structure.sql | 15 +----- 3 files changed, 1 insertion(+), 61 deletions(-) delete mode 100644 db/migrate/20251107212441_add_noteable_to_workflows.rb delete mode 100644 db/schema_migrations/20251107212441 diff --git a/db/migrate/20251107212441_add_noteable_to_workflows.rb b/db/migrate/20251107212441_add_noteable_to_workflows.rb deleted file mode 100644 index 2e909ae4a53287..00000000000000 --- a/db/migrate/20251107212441_add_noteable_to_workflows.rb +++ /dev/null @@ -1,46 +0,0 @@ -# frozen_string_literal: true - -class AddNoteableToWorkflows < Gitlab::Database::Migration[2.3] - disable_ddl_transaction! - - milestone '18.6' - - TABLE = :duo_workflows_workflows - CONSTRAINT_NAME = 'check_workflows_single_noteable' - ISSUE_ID_INDEX = 'index_duo_workflows_workflows_on_issue_id' - MR_ID_INDEX = 'index_duo_workflows_workflows_on_merge_request_id' - - def up - with_lock_retries do - add_column TABLE, :issue_id, :bigint, if_not_exists: true - add_column TABLE, :merge_request_id, :bigint, if_not_exists: true - end - - add_concurrent_index TABLE, :issue_id, name: ISSUE_ID_INDEX - add_concurrent_index TABLE, :merge_request_id, name: MR_ID_INDEX - - add_concurrent_foreign_key TABLE, :issues, - column: :issue_id, on_delete: :cascade - add_concurrent_foreign_key TABLE, :merge_requests, - column: :merge_request_id, on_delete: :cascade - - add_check_constraint TABLE, - 'num_nonnulls(issue_id, merge_request_id) <= 1', - CONSTRAINT_NAME - end - - def down - remove_check_constraint TABLE, CONSTRAINT_NAME - - remove_foreign_key_if_exists TABLE, column: :issue_id - remove_foreign_key_if_exists TABLE, column: :merge_request_id - - remove_concurrent_index_by_name TABLE, ISSUE_ID_INDEX - remove_concurrent_index_by_name TABLE, MR_ID_INDEX - - with_lock_retries do - remove_column TABLE, :issue_id, if_exists: true - remove_column TABLE, :merge_request_id, if_exists: true - end - end -end diff --git a/db/schema_migrations/20251107212441 b/db/schema_migrations/20251107212441 deleted file mode 100644 index c8370e81bb4fcd..00000000000000 --- a/db/schema_migrations/20251107212441 +++ /dev/null @@ -1 +0,0 @@ -7883b12df85ec12420172a3da325b2c0ff75f99936f974f008fc1a86e7f628b7 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index b4a9b21fd0c19d..39ec20faa13dc5 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -16650,13 +16650,10 @@ CREATE TABLE duo_workflows_workflows ( environment smallint, namespace_id bigint, ai_catalog_item_version_id bigint, - issue_id bigint, - merge_request_id bigint, CONSTRAINT check_30ca07a4ef CHECK ((char_length(goal) <= 16384)), CONSTRAINT check_3a9162f1ae CHECK ((char_length(image) <= 2048)), CONSTRAINT check_73884a5839 CHECK ((num_nonnulls(namespace_id, project_id) = 1)), - CONSTRAINT check_ec723e2a1a CHECK ((char_length(workflow_definition) <= 255)), - CONSTRAINT check_workflows_single_noteable CHECK ((num_nonnulls(issue_id, merge_request_id) <= 1)) + CONSTRAINT check_ec723e2a1a CHECK ((char_length(workflow_definition) <= 255)) ); CREATE SEQUENCE duo_workflows_workflows_id_seq @@ -40250,10 +40247,6 @@ CREATE INDEX index_duo_workflows_events_on_workflow_id ON duo_workflows_events U CREATE INDEX index_duo_workflows_workflows_on_ai_catalog_item_version_id ON duo_workflows_workflows USING btree (ai_catalog_item_version_id); -CREATE INDEX index_duo_workflows_workflows_on_issue_id ON duo_workflows_workflows USING btree (issue_id); - -CREATE INDEX index_duo_workflows_workflows_on_merge_request_id ON duo_workflows_workflows USING btree (merge_request_id); - CREATE INDEX index_duo_workflows_workflows_on_namespace_id ON duo_workflows_workflows USING btree (namespace_id); CREATE INDEX index_duo_workflows_workflows_on_project_id ON duo_workflows_workflows USING btree (project_id); @@ -50249,9 +50242,6 @@ ALTER TABLE ONLY packages_helm_file_metadata ALTER TABLE ONLY bulk_import_failures ADD CONSTRAINT fk_dad28985ee FOREIGN KEY (namespace_id) REFERENCES namespaces(id) ON DELETE CASCADE; -ALTER TABLE ONLY duo_workflows_workflows - ADD CONSTRAINT fk_daffbfef9a FOREIGN KEY (issue_id) REFERENCES issues(id) ON DELETE CASCADE; - ALTER TABLE ONLY project_topics ADD CONSTRAINT fk_db13576296 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE; @@ -50423,9 +50413,6 @@ ALTER TABLE ONLY merge_requests_compliance_violations ALTER TABLE ONLY issue_emails ADD CONSTRAINT fk_ed0f4c4b51 FOREIGN KEY (namespace_id) REFERENCES namespaces(id) ON DELETE CASCADE; -ALTER TABLE ONLY duo_workflows_workflows - ADD CONSTRAINT fk_ed58162ace FOREIGN KEY (merge_request_id) REFERENCES merge_requests(id) ON DELETE CASCADE; - ALTER TABLE ONLY cluster_agent_migrations ADD CONSTRAINT fk_ed8ffda028 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE; -- GitLab From 56df5f585cdb6f6345545eb2fe168e827ecdd524 Mon Sep 17 00:00:00 2001 From: sholaq Date: Tue, 11 Nov 2025 11:24:07 +0000 Subject: [PATCH 09/11] Splits combined migrations --- ...add_issue_id_to_duo_workflows_workflows.rb | 28 +++++++++++++++++++ ...e_request_id_to_duo_workflows_workflows.rb | 28 +++++++++++++++++++ ...e_constraint_to_duo_workflows_workflows.rb | 19 +++++++++++++ db/schema_migrations/20251111101929 | 1 + db/schema_migrations/20251111102913 | 1 + db/schema_migrations/20251111104032 | 1 + db/structure.sql | 15 +++++++++- 7 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20251111101929_add_issue_id_to_duo_workflows_workflows.rb create mode 100644 db/migrate/20251111102913_add_merge_request_id_to_duo_workflows_workflows.rb create mode 100644 db/migrate/20251111104032_add_single_noteable_constraint_to_duo_workflows_workflows.rb create mode 100644 db/schema_migrations/20251111101929 create mode 100644 db/schema_migrations/20251111102913 create mode 100644 db/schema_migrations/20251111104032 diff --git a/db/migrate/20251111101929_add_issue_id_to_duo_workflows_workflows.rb b/db/migrate/20251111101929_add_issue_id_to_duo_workflows_workflows.rb new file mode 100644 index 00000000000000..7705b6af8948fd --- /dev/null +++ b/db/migrate/20251111101929_add_issue_id_to_duo_workflows_workflows.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +class AddIssueIdToDuoWorkflowsWorkflows < Gitlab::Database::Migration[2.3] + disable_ddl_transaction! + + milestone '18.6' + + INDEX_NAME = 'index_duo_workflows_workflows_on_issue_id' + TABLE = :duo_workflows_workflows + + def up + with_lock_retries do + add_column TABLE, :issue_id, :bigint, null: true, if_not_exists: true + end + + add_concurrent_index TABLE, :issue_id, name: INDEX_NAME + add_concurrent_foreign_key TABLE, :issues, column: :issue_id, on_delete: :cascade + end + + def down + remove_foreign_key_if_exists TABLE, :issues, column: :issue_id + remove_concurrent_index_by_name TABLE, INDEX_NAME + + with_lock_retries do + remove_column TABLE, :issue_id, :bigint, null: true, if_exists: true + end + end +end diff --git a/db/migrate/20251111102913_add_merge_request_id_to_duo_workflows_workflows.rb b/db/migrate/20251111102913_add_merge_request_id_to_duo_workflows_workflows.rb new file mode 100644 index 00000000000000..1126bc5cd70294 --- /dev/null +++ b/db/migrate/20251111102913_add_merge_request_id_to_duo_workflows_workflows.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +class AddMergeRequestIdToDuoWorkflowsWorkflows < Gitlab::Database::Migration[2.3] + disable_ddl_transaction! + + milestone '18.6' + + INDEX_NAME = 'index_duo_workflows_workflows_on_merge_request_id' + TABLE = :duo_workflows_workflows + + def up + with_lock_retries do + add_column TABLE, :merge_request_id, :bigint, null: true, if_not_exists: true + end + + add_concurrent_index TABLE, :merge_request_id, name: INDEX_NAME + add_concurrent_foreign_key TABLE, :merge_requests, column: :merge_request_id, on_delete: :cascade + end + + def down + remove_foreign_key_if_exists TABLE, :merge_requests, column: :merge_request_id + remove_concurrent_index_by_name TABLE, INDEX_NAME + + with_lock_retries do + remove_column TABLE, :merge_request_id, :bigint, null: true, if_exists: true + end + end +end diff --git a/db/migrate/20251111104032_add_single_noteable_constraint_to_duo_workflows_workflows.rb b/db/migrate/20251111104032_add_single_noteable_constraint_to_duo_workflows_workflows.rb new file mode 100644 index 00000000000000..9f2700183c93d4 --- /dev/null +++ b/db/migrate/20251111104032_add_single_noteable_constraint_to_duo_workflows_workflows.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class AddSingleNoteableConstraintToDuoWorkflowsWorkflows < Gitlab::Database::Migration[2.3] + disable_ddl_transaction! + + milestone '18.6' + + CONSTRAINT_NAME = 'check_workflows_single_noteable' + + def up + add_check_constraint :duo_workflows_workflows, + 'num_nonnulls(issue_id, merge_request_id) <= 1', + CONSTRAINT_NAME + end + + def down + remove_check_constraint :duo_workflows_workflows, CONSTRAINT_NAME + end +end diff --git a/db/schema_migrations/20251111101929 b/db/schema_migrations/20251111101929 new file mode 100644 index 00000000000000..34f201659d77c4 --- /dev/null +++ b/db/schema_migrations/20251111101929 @@ -0,0 +1 @@ +18ce72e2b1e04d94471abd60d5b81f6edb0271bb15edc0608302a973b9611cb3 \ No newline at end of file diff --git a/db/schema_migrations/20251111102913 b/db/schema_migrations/20251111102913 new file mode 100644 index 00000000000000..0acb348749d734 --- /dev/null +++ b/db/schema_migrations/20251111102913 @@ -0,0 +1 @@ +48b6fb18dbbba95e9fe0d79daf4094b3b24c9c897d0e36a643f09a82241cc976 \ No newline at end of file diff --git a/db/schema_migrations/20251111104032 b/db/schema_migrations/20251111104032 new file mode 100644 index 00000000000000..8b848d9ce1225e --- /dev/null +++ b/db/schema_migrations/20251111104032 @@ -0,0 +1 @@ +a4c55ca116143f952fd08dc3f767ea6ae00edf27938ad8d51817903166174e59 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index 39ec20faa13dc5..b4a9b21fd0c19d 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -16650,10 +16650,13 @@ CREATE TABLE duo_workflows_workflows ( environment smallint, namespace_id bigint, ai_catalog_item_version_id bigint, + issue_id bigint, + merge_request_id bigint, CONSTRAINT check_30ca07a4ef CHECK ((char_length(goal) <= 16384)), CONSTRAINT check_3a9162f1ae CHECK ((char_length(image) <= 2048)), CONSTRAINT check_73884a5839 CHECK ((num_nonnulls(namespace_id, project_id) = 1)), - CONSTRAINT check_ec723e2a1a CHECK ((char_length(workflow_definition) <= 255)) + CONSTRAINT check_ec723e2a1a CHECK ((char_length(workflow_definition) <= 255)), + CONSTRAINT check_workflows_single_noteable CHECK ((num_nonnulls(issue_id, merge_request_id) <= 1)) ); CREATE SEQUENCE duo_workflows_workflows_id_seq @@ -40247,6 +40250,10 @@ CREATE INDEX index_duo_workflows_events_on_workflow_id ON duo_workflows_events U CREATE INDEX index_duo_workflows_workflows_on_ai_catalog_item_version_id ON duo_workflows_workflows USING btree (ai_catalog_item_version_id); +CREATE INDEX index_duo_workflows_workflows_on_issue_id ON duo_workflows_workflows USING btree (issue_id); + +CREATE INDEX index_duo_workflows_workflows_on_merge_request_id ON duo_workflows_workflows USING btree (merge_request_id); + CREATE INDEX index_duo_workflows_workflows_on_namespace_id ON duo_workflows_workflows USING btree (namespace_id); CREATE INDEX index_duo_workflows_workflows_on_project_id ON duo_workflows_workflows USING btree (project_id); @@ -50242,6 +50249,9 @@ ALTER TABLE ONLY packages_helm_file_metadata ALTER TABLE ONLY bulk_import_failures ADD CONSTRAINT fk_dad28985ee FOREIGN KEY (namespace_id) REFERENCES namespaces(id) ON DELETE CASCADE; +ALTER TABLE ONLY duo_workflows_workflows + ADD CONSTRAINT fk_daffbfef9a FOREIGN KEY (issue_id) REFERENCES issues(id) ON DELETE CASCADE; + ALTER TABLE ONLY project_topics ADD CONSTRAINT fk_db13576296 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE; @@ -50413,6 +50423,9 @@ ALTER TABLE ONLY merge_requests_compliance_violations ALTER TABLE ONLY issue_emails ADD CONSTRAINT fk_ed0f4c4b51 FOREIGN KEY (namespace_id) REFERENCES namespaces(id) ON DELETE CASCADE; +ALTER TABLE ONLY duo_workflows_workflows + ADD CONSTRAINT fk_ed58162ace FOREIGN KEY (merge_request_id) REFERENCES merge_requests(id) ON DELETE CASCADE; + ALTER TABLE ONLY cluster_agent_migrations ADD CONSTRAINT fk_ed8ffda028 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE; -- GitLab From 67e7608c721a041a93c45c3e1dd32d0512e823e8 Mon Sep 17 00:00:00 2001 From: Shola Quadri Date: Tue, 11 Nov 2025 14:38:33 +0000 Subject: [PATCH 10/11] Apply 1 suggestion(s) to 1 file(s) Co-authored-by: Max Orefice --- .../20251111101929_add_issue_id_to_duo_workflows_workflows.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/db/migrate/20251111101929_add_issue_id_to_duo_workflows_workflows.rb b/db/migrate/20251111101929_add_issue_id_to_duo_workflows_workflows.rb index 7705b6af8948fd..a5394856615878 100644 --- a/db/migrate/20251111101929_add_issue_id_to_duo_workflows_workflows.rb +++ b/db/migrate/20251111101929_add_issue_id_to_duo_workflows_workflows.rb @@ -18,9 +18,6 @@ def up end def down - remove_foreign_key_if_exists TABLE, :issues, column: :issue_id - remove_concurrent_index_by_name TABLE, INDEX_NAME - with_lock_retries do remove_column TABLE, :issue_id, :bigint, null: true, if_exists: true end -- GitLab From 4c7b25f8d8b95c0efc1a44caaa495054b6590523 Mon Sep 17 00:00:00 2001 From: Shola Quadri Date: Tue, 11 Nov 2025 14:38:40 +0000 Subject: [PATCH 11/11] Apply 1 suggestion(s) to 1 file(s) Co-authored-by: Max Orefice --- ...11102913_add_merge_request_id_to_duo_workflows_workflows.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/db/migrate/20251111102913_add_merge_request_id_to_duo_workflows_workflows.rb b/db/migrate/20251111102913_add_merge_request_id_to_duo_workflows_workflows.rb index 1126bc5cd70294..f48a824611ac69 100644 --- a/db/migrate/20251111102913_add_merge_request_id_to_duo_workflows_workflows.rb +++ b/db/migrate/20251111102913_add_merge_request_id_to_duo_workflows_workflows.rb @@ -18,9 +18,6 @@ def up end def down - remove_foreign_key_if_exists TABLE, :merge_requests, column: :merge_request_id - remove_concurrent_index_by_name TABLE, INDEX_NAME - with_lock_retries do remove_column TABLE, :merge_request_id, :bigint, null: true, if_exists: true end -- GitLab