Skip to content

[Event::Plan] Add unique validation for event_id when active#13533

Open
manuthecoder wants to merge 11 commits into
mainfrom
mxg-add-rails-db-validation-that-event-id-is-unique-when-aasm-state-is-active
Open

[Event::Plan] Add unique validation for event_id when active#13533
manuthecoder wants to merge 11 commits into
mainfrom
mxg-add-rails-db-validation-that-event-id-is-unique-when-aasm-state-is-active

Conversation

@manuthecoder
Copy link
Copy Markdown
Contributor

Adds a partial unique index so that each ‎event_id in ‎event_plans can only appear once when ‎aasm_state = 'active', enforced at both the Rails and database level, closing #12965

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) where aasm_state = 'active' (concurrently).
  • Update db/schema.rb to include the new partial unique index.
  • Update Event::Plan schema 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.

Comment thread db/schema.rb Outdated
Comment on lines +1076 to +1077
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'"
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
claude and others added 10 commits April 24, 2026 06:39
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants