From be044ef69df8d025c77b46ceb086483e196230a0 Mon Sep 17 00:00:00 2001 From: Ryan Cobb Date: Fri, 14 Nov 2025 11:43:09 -0700 Subject: [PATCH] Remove auto-increment from plan id --- app/models/plan.rb | 12 ++++++++++++ ...113223034_remove_auto_increment_from_plans_id.rb | 13 +++++++++++++ db/schema_migrations/20251113223034 | 1 + db/structure.sql | 2 -- ...29145056_backfill_plan_name_uid_on_plans_spec.rb | 2 +- 5 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20251113223034_remove_auto_increment_from_plans_id.rb create mode 100644 db/schema_migrations/20251113223034 diff --git a/app/models/plan.rb b/app/models/plan.rb index 90e68f3f65f519..e759c49da50b24 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -35,6 +35,8 @@ class Plan < ApplicationRecord before_validation :set_plan_name_uid + before_create :set_id_if_not_present + # This always returns an object def self.default Gitlab::SafeRequestStore.fetch(:plan_default) do @@ -80,6 +82,16 @@ def paid? private + # Manually assign ID when creating new plans. + # Database does not assign or increment id for Plans table. + # See https://gitlab.com/gitlab-org/gitlab/-/issues/571927 + def set_id_if_not_present + return if id.present? + + max_id = Plan.maximum(:id).to_i + self.id = max_id + 1 + end + def set_plan_name_uid return unless name.present? diff --git a/db/migrate/20251113223034_remove_auto_increment_from_plans_id.rb b/db/migrate/20251113223034_remove_auto_increment_from_plans_id.rb new file mode 100644 index 00000000000000..99f9e12b300058 --- /dev/null +++ b/db/migrate/20251113223034_remove_auto_increment_from_plans_id.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class RemoveAutoIncrementFromPlansId < Gitlab::Database::Migration[2.3] + milestone '18.6' + + def up + change_column_default :plans, :id, nil + end + + def down + change_column_default :plans, :id, -> { "nextval('plans_id_seq'::regclass)" } + end +end diff --git a/db/schema_migrations/20251113223034 b/db/schema_migrations/20251113223034 new file mode 100644 index 00000000000000..794fd03e6045ff --- /dev/null +++ b/db/schema_migrations/20251113223034 @@ -0,0 +1 @@ +04295a94964c6533275297076af2b39099395fc64204a70eb137c364a223ddc4 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index cfade9c0c09b1b..842123d5a3b2a7 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -31789,8 +31789,6 @@ ALTER TABLE ONLY personal_access_tokens ALTER COLUMN id SET DEFAULT nextval('per ALTER TABLE ONLY plan_limits ALTER COLUMN id SET DEFAULT nextval('plan_limits_id_seq'::regclass); -ALTER TABLE ONLY plans ALTER COLUMN id SET DEFAULT nextval('plans_id_seq'::regclass); - ALTER TABLE ONLY pm_advisories ALTER COLUMN id SET DEFAULT nextval('pm_advisories_id_seq'::regclass); ALTER TABLE ONLY pm_affected_packages ALTER COLUMN id SET DEFAULT nextval('pm_affected_packages_id_seq'::regclass); diff --git a/spec/migrations/db/post_migrate/20251029145056_backfill_plan_name_uid_on_plans_spec.rb b/spec/migrations/db/post_migrate/20251029145056_backfill_plan_name_uid_on_plans_spec.rb index 2d8fa0764e3dac..4b322ae20bca3a 100644 --- a/spec/migrations/db/post_migrate/20251029145056_backfill_plan_name_uid_on_plans_spec.rb +++ b/spec/migrations/db/post_migrate/20251029145056_backfill_plan_name_uid_on_plans_spec.rb @@ -7,7 +7,7 @@ describe '#up' do it 'backfills plan_name_uid' do # Using wrong plan_name_uid value to create a valid record for backfilling - table(:plans).create!(name: 'premium', title: 'Premium', plan_name_uid: 999) + table(:plans).create!(id: 1, name: 'premium', title: 'Premium', plan_name_uid: 999) migrate! -- GitLab