[Event::Plan] Add unique validation for event_id when active#13533
[Event::Plan] Add unique validation for event_id when active#13533manuthecoder wants to merge 11 commits into
Conversation
aasm_state = 'active'
There was a problem hiding this comment.
Pull request overview
Adds a database-level constraint to ensure each event_id can only have one active Event::Plan, aligning the DB with the existing “one active plan per event” business rule and closing #12965.
Changes:
- Add a partial unique index on
event_plans(event_id)whereaasm_state = 'active'(concurrently). - Update
db/schema.rbto include the new partial unique index. - Update
Event::Planschema comment to reflect the new index.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| db/schema.rb | Adds the partial unique index entry for active plans (but schema version needs regeneration). |
| db/migrate/20260421000000_add_unique_index_to_event_plans_when_active.rb | Introduces the concurrent partial unique index enforcing one active plan per event. |
| app/models/event/plan.rb | Updates schema annotation to document the new index. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| t.index ["event_id"], name: "index_event_plans_on_event_id" | ||
| t.index ["event_id"], name: "index_event_plans_on_event_id_when_active", unique: true, where: "aasm_state = 'active'" |
There was a problem hiding this comment.
db/schema.rb was updated to include the new index, but the schema header version still reads ActiveRecord::Schema[8.0].define(version: 2026_04_10_205826). Since this PR adds a migration dated 2026_04_21_000000, the schema version should be bumped to match the latest migration; otherwise db:schema:load may mark the DB as only migrated through 2026-04-10 and then attempt to re-run this migration (potentially failing because the index already exists). Re-run bin/rails db:migrate and commit the regenerated schema.rb so the version is correct.
The schema version was left at 2026_04_10_205826 after adding migration 20260421000000. Rails' assume_migrated_upto_version only marks migrations <= the schema version as run, so the new migration appeared pending, causing ActiveRecord::PendingMigrationError to crash all RSpec shards and causing the Reversible Migrations check to fail when db:migrate tried to re-add an index already present from db:schema:load. https://claude.ai/code/session_01QAEAfLMJTdFW7P3FuFXvxu
…-unique-when-aasm-state-is-active
…-unique-when-aasm-state-is-active
…-unique-when-aasm-state-is-active
…-unique-when-aasm-state-is-active
Remove allow_reimbursement_report and allow_stripe_card from the annotation headers — those columns don't exist in schema.rb, causing the Annotate CI check to fail with --frozen. https://claude.ai/code/session_01PjhpbK1h2Dsgi2APWjQyzE
Add index_event_plans_on_event_id_when_active to the annotation headers of all event/plan models so annotaterb --frozen passes. https://claude.ai/code/session_01PjhpbK1h2Dsgi2APWjQyzE
Adds a partial unique index so that each
event_idin event_planscan only appear once when aasm_state = 'active', enforced at both the Rails and database level, closing #12965