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)
-
Migration to remove sequence default
- Remove the
nextval()default fromplans.idcolumn - Keep all constraints (PRIMARY KEY, NOT NULL, UNIQUE)
- No data changes needed
- Remove the
-
Update Plan model
- Add
before_validationcallback to set ID when creating new plans - Use
Plan.maximum(:id).to_i + 1for ID generation - Existing
safe_find_or_create_byinPlan.defaultandPlan.freealready handles race conditions
- Add
-
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
defaultplan (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 🤖