Skip to content

Fix flaky categories_spec destroy test#1478

Merged
maebeale merged 1 commit intomainfrom
maebeale/fix-flaky-categories-spec
Apr 5, 2026
Merged

Fix flaky categories_spec destroy test#1478
maebeale merged 1 commit intomainfrom
maebeale/fix-flaky-categories-spec

Conversation

@maebeale
Copy link
Copy Markdown
Collaborator

@maebeale maebeale commented Apr 5, 2026

Closes #1477

What is the goal of this PR and why is this important?

  • Fix an intermittently failing test: Categories as an admin DELETE destroy destroys category
  • The test fails with expected Category.count to have changed by -1, but was changed by 0

How did you approach the change?

  • Root cause: The unique index (category_type_id, position) on the categories table is incompatible with the positioning gem. During destroy/update, the gem temporarily sets position to 0 via update_all. If another record in the same scope already has position 0 (from an interrupted prior operation), MySQL raises ActiveRecord::RecordNotUnique, causing destroy! to fail
  • The unique index was added in Add drag-and-drop positioning for categories scoped by category type #712 but the positioning gem manages uniqueness itself through row-level locking — the DB constraint is redundant and fights with the gem's intermediate states
  • Fix: Migration to replace the unique composite index with a non-unique composite index (preserves query performance, removes the constraint that conflicts with the gem)

Anything else to add?

  • Reproduced by manually creating a position-0 record and confirming destroy! raises ActiveRecord::RecordNotUnique
  • The positioning gem uses position 0 and negative numbers as intermediate values during reordering — this is by design and documented in the gem's source

🤖 Generated with Claude Code

The test was intermittently failing with "expected Category.count to
have changed by -1, but was changed by 0" when run in the full suite.
Without an explicit Accept: text/html header, format negotiation
pollution from preceding specs (e.g. JSON-format requests) could cause
Rails to mishandle the request, preventing the destroy from completing.

This is the same root cause as #1470 (people_search_spec).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@maebeale maebeale merged commit a10a822 into main Apr 5, 2026
3 checks passed
@maebeale maebeale deleted the maebeale/fix-flaky-categories-spec branch April 5, 2026 12:13
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.

Fix flaky categories_spec destroy test

1 participant