From 3cec70422bd4097b3c7f371915cab101c521ac3f Mon Sep 17 00:00:00 2001 From: Gregory Havenga <11164960-ghavenga@users.noreply.gitlab.com> Date: Wed, 22 Oct 2025 14:41:03 +0200 Subject: [PATCH 1/4] Add migration to remove id column from vulnerability_reads table The vulnerability_reads table has both an `id` column and uses `vulnerability_id` as the primary key. This causes confusion and issues with Rails 7.2 bulk insert operations where Rails treats `id` as a special attribute that maps to the primary key. Since the `id` column is not used as the primary key and all operations use `vulnerability_id`, we can safely remove the `id` column to simplify the model and avoid Rails 7.2 compatibility issues. Related to: https://gitlab.com/gitlab-org/gitlab/-/issues/357262 --- ...move_id_column_from_vulnerability_reads.rb | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 db/migrate/20251022124000_remove_id_column_from_vulnerability_reads.rb diff --git a/db/migrate/20251022124000_remove_id_column_from_vulnerability_reads.rb b/db/migrate/20251022124000_remove_id_column_from_vulnerability_reads.rb new file mode 100644 index 00000000000000..a54caf35afa913 --- /dev/null +++ b/db/migrate/20251022124000_remove_id_column_from_vulnerability_reads.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +class RemoveIdColumnFromVulnerabilityReads < Gitlab::Database::Migration[2.3] + milestone '18.6' + + def up + remove_column :vulnerability_reads, :id + end + + def down + # Note: This down migration recreates the id column but does not restore the original data. + # The id values will be auto-generated and may not match the original values. + add_column :vulnerability_reads, :id, :bigint, null: false + + # Add a sequence for the id column + execute <<~SQL + CREATE SEQUENCE IF NOT EXISTS vulnerability_reads_id_seq; + ALTER TABLE vulnerability_reads ALTER COLUMN id SET DEFAULT nextval('vulnerability_reads_id_seq'); + SELECT setval('vulnerability_reads_id_seq', COALESCE(MAX(vulnerability_id), 1)) FROM vulnerability_reads; + SQL + end +end -- GitLab From f4082e0ed91f94d0808cd5982b46fa8e8a82aed2 Mon Sep 17 00:00:00 2001 From: Gregory Havenga Date: Wed, 22 Oct 2025 16:20:16 +0200 Subject: [PATCH 2/4] Drop id column, ensure there is a primary key --- ...move_id_column_from_vulnerability_reads.rb | 22 ------------------- ...move_id_column_from_vulnerability_reads.rb | 15 +++++++++++++ db/schema_migrations/20251022124000 | 1 + db/structure.sql | 14 +----------- 4 files changed, 17 insertions(+), 35 deletions(-) delete mode 100644 db/migrate/20251022124000_remove_id_column_from_vulnerability_reads.rb create mode 100644 db/post_migrate/20251022124000_remove_id_column_from_vulnerability_reads.rb create mode 100644 db/schema_migrations/20251022124000 diff --git a/db/migrate/20251022124000_remove_id_column_from_vulnerability_reads.rb b/db/migrate/20251022124000_remove_id_column_from_vulnerability_reads.rb deleted file mode 100644 index a54caf35afa913..00000000000000 --- a/db/migrate/20251022124000_remove_id_column_from_vulnerability_reads.rb +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -class RemoveIdColumnFromVulnerabilityReads < Gitlab::Database::Migration[2.3] - milestone '18.6' - - def up - remove_column :vulnerability_reads, :id - end - - def down - # Note: This down migration recreates the id column but does not restore the original data. - # The id values will be auto-generated and may not match the original values. - add_column :vulnerability_reads, :id, :bigint, null: false - - # Add a sequence for the id column - execute <<~SQL - CREATE SEQUENCE IF NOT EXISTS vulnerability_reads_id_seq; - ALTER TABLE vulnerability_reads ALTER COLUMN id SET DEFAULT nextval('vulnerability_reads_id_seq'); - SELECT setval('vulnerability_reads_id_seq', COALESCE(MAX(vulnerability_id), 1)) FROM vulnerability_reads; - SQL - end -end diff --git a/db/post_migrate/20251022124000_remove_id_column_from_vulnerability_reads.rb b/db/post_migrate/20251022124000_remove_id_column_from_vulnerability_reads.rb new file mode 100644 index 00000000000000..2507597232e079 --- /dev/null +++ b/db/post_migrate/20251022124000_remove_id_column_from_vulnerability_reads.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class RemoveIdColumnFromVulnerabilityReads < Gitlab::Database::Migration[2.3] + milestone '18.6' + + def up + remove_column :vulnerability_reads, :id, if_exists: true + + execute('ALTER TABLE vulnerability_reads ADD PRIMARY KEY (vulnerability_id);') + end + + def down + # no-op - If we remove it, there's no point putting it back + end +end diff --git a/db/schema_migrations/20251022124000 b/db/schema_migrations/20251022124000 new file mode 100644 index 00000000000000..a5a7163af1caa4 --- /dev/null +++ b/db/schema_migrations/20251022124000 @@ -0,0 +1 @@ +a70fa532b1f7b8f31d9dc860683ba40bd474d78e04b66948064f9436baf8e241 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index 64d8af58f2481b..7b1a0a71b515af 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -28593,7 +28593,6 @@ CREATE TABLE vulnerability_partial_scans ( ); CREATE TABLE vulnerability_reads ( - id bigint NOT NULL, vulnerability_id bigint NOT NULL, project_id bigint NOT NULL, scanner_id bigint NOT NULL, @@ -28623,15 +28622,6 @@ CREATE TABLE vulnerability_reads ( CONSTRAINT check_f5ba7c2496 CHECK ((traversal_ids IS NOT NULL)) ); -CREATE SEQUENCE vulnerability_reads_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - -ALTER SEQUENCE vulnerability_reads_id_seq OWNED BY vulnerability_reads.id; - CREATE TABLE vulnerability_remediation_uploads ( id bigint NOT NULL, size bigint NOT NULL, @@ -31807,8 +31797,6 @@ ALTER TABLE ONLY vulnerability_occurrence_identifiers ALTER COLUMN id SET DEFAUL ALTER TABLE ONLY vulnerability_occurrences ALTER COLUMN id SET DEFAULT nextval('vulnerability_occurrences_id_seq'::regclass); -ALTER TABLE ONLY vulnerability_reads ALTER COLUMN id SET DEFAULT nextval('vulnerability_reads_id_seq'::regclass); - ALTER TABLE ONLY vulnerability_remediations ALTER COLUMN id SET DEFAULT nextval('vulnerability_remediations_id_seq'::regclass); ALTER TABLE ONLY vulnerability_scanners ALTER COLUMN id SET DEFAULT nextval('vulnerability_scanners_id_seq'::regclass); @@ -35533,7 +35521,7 @@ ALTER TABLE ONLY vulnerability_partial_scans ADD CONSTRAINT vulnerability_partial_scans_pkey PRIMARY KEY (scan_id); ALTER TABLE ONLY vulnerability_reads - ADD CONSTRAINT vulnerability_reads_pkey PRIMARY KEY (id); + ADD CONSTRAINT vulnerability_reads_pkey PRIMARY KEY (vulnerability_id); ALTER TABLE ONLY vulnerability_remediation_uploads ADD CONSTRAINT vulnerability_remediation_uploads_pkey PRIMARY KEY (id, model_type); -- GitLab From ed7de8ea47d47667d269b47c2870728a7cd10310 Mon Sep 17 00:00:00 2001 From: Gregory Havenga Date: Thu, 23 Oct 2025 11:15:38 +0200 Subject: [PATCH 3/4] Ignore vulnerability_reads id column before removal --- ee/app/models/vulnerabilities/read.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/ee/app/models/vulnerabilities/read.rb b/ee/app/models/vulnerabilities/read.rb index 2cac5e118730dd..a40b4bfbcd039c 100644 --- a/ee/app/models/vulnerabilities/read.rb +++ b/ee/app/models/vulnerabilities/read.rb @@ -13,6 +13,7 @@ class Read < ::SecApplicationRecord include ::Elastic::ApplicationVersionedSearch ignore_column :namespace_id, remove_with: '17.7', remove_after: '2024-11-21' + ignore_column :id, remove_with: '18.7', remove_after: '2025-11-15' declarative_enum DismissalReasonEnum -- GitLab From fcf92edb9eff854dc04d16d95f183e5733383c8e Mon Sep 17 00:00:00 2001 From: Gregory Havenga <11164960-ghavenga@users.noreply.gitlab.com> Date: Thu, 23 Oct 2025 11:24:33 +0200 Subject: [PATCH 4/4] Remove change not intended for this MR --- ee/app/models/vulnerabilities/read.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/ee/app/models/vulnerabilities/read.rb b/ee/app/models/vulnerabilities/read.rb index a40b4bfbcd039c..2cac5e118730dd 100644 --- a/ee/app/models/vulnerabilities/read.rb +++ b/ee/app/models/vulnerabilities/read.rb @@ -13,7 +13,6 @@ class Read < ::SecApplicationRecord include ::Elastic::ApplicationVersionedSearch ignore_column :namespace_id, remove_with: '17.7', remove_after: '2024-11-21' - ignore_column :id, remove_with: '18.7', remove_after: '2025-11-15' declarative_enum DismissalReasonEnum -- GitLab