diff --git a/app/models/plan.rb b/app/models/plan.rb index 90e68f3f65f519bb373db8929a71039cfb21e8ea..e759c49da50b241999c48ad9794bad446f15820d 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 0000000000000000000000000000000000000000..99f9e12b3000581a195e139d85dbd45d797fe489 --- /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 0000000000000000000000000000000000000000..794fd03e6045ffe626bceb2baaf18eb7e72cc6e4 --- /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 cfade9c0c09b1be51bcaf1f20bdd9831bdf8ba4a..842123d5a3b2a72e3c4d5af3be6ad23f822500bf 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 2d8fa0764e3daca8e270340b8587627cc3468eec..4b322ae20bca3ac8f02bfa84dce24004908b800d 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!