Skip to content

Remove Auto-increment Sequence from Plans Table

Problem

The plans table uses auto-increment IDs which creates inconsistencies across Cells in the new architecture. Each Cell could have different IDs for the same plan (e.g., 'premium' could be ID 4 in one cell, ID 5 in another).

Solution

Remove the auto-increment sequence from the plans.id column while keeping existing IDs unchanged.

Implementation (High-Level)

  1. Migration to remove sequence default
    • Remove the nextval() default from plans.id column
    • Keep all constraints (PRIMARY KEY, NOT NULL, UNIQUE)
    • No data changes needed
  2. Update Plan model
    • Add before_validation callback to set ID when creating new plans
    • Use Plan.maximum(:id).to_i + 1 for ID generation
    • Existing safe_find_or_create_by in Plan.default and Plan.free already handles race conditions
  3. No changes needed to referencing tables
    • All foreign keys remain valid
    • No data migration required

Why This Is Safe

  • Production (GitLab.com): All plans already exist, never recreated
  • Self-managed: Only uses default plan (rarely created)
  • New Cells: Copy plans table from legacy cell during setup
  • Race conditions: Handled by unique constraint + safe_find_or_create_by

Benefits

  • Minimal code changes (~10 lines)
  • No data migration needed
  • Easy rollback (just restore the sequence default)
  • Ensures consistent plan IDs across all Cells

Risks

  • Minimal: Plans are rarely created (only during initial setup)
  • Handled: Any race condition fails safely with unique constraint violation
Edited by 🤖 GitLab Bot 🤖