Skip to content

Fix #49: author merge trips the unique provider-id indexes while the source still owns them - #66

Open
sebclark wants to merge 1 commit into
Chaptarr:developfrom
sebclark:fix-author-merge-unique-provider-id
Open

Fix #49: author merge trips the unique provider-id indexes while the source still owns them#66
sebclark wants to merge 1 commit into
Chaptarr:developfrom
sebclark:fix-author-merge-unique-provider-id

Conversation

@sebclark

Copy link
Copy Markdown
Contributor

Fixes #49

The bug

Exactly as diagnosed in the issue: TryMergeAuthorsWithoutDataLoss copies the source's provider ids onto the survivor and calls UpdateAuthor(target) before the transaction deletes the source row — so the source still owns the same values and the save trips the unique index. All five provider-id columns are covered by unique indexes (UX_Authors_HardcoverAuthorId, GoodreadsAuthorId, AudnexusAuthorId, OpenLibraryAuthorId, GoogleBooksAuthorId), so any of them can block the merge, and every bulk sync retries and fails the same way.

Fix

The existing early UpdateAuthor(target) isn't accidental — it's the method's abort-safety (a failure leaves the source intact), so the fix keeps it rather than reordering it away:

  1. MergeIntoSurvivor computes the merged state as before
  2. The survivor is persisted without the contested provider ids (collision-free; a failure here still aborts cleanly with the source untouched)
  3. Inside the existing merge transaction, a new helper ReleaseAndTransferUniqueProviderIds clears the ids on the source row and applies the merged ids to the survivor via SQL, then the children are reassigned and the source deleted — all committed atomically, so the unique indexes never see two rows holding the same value and any failure rolls the whole handoff back

Tests

New AuthorMergeUniqueProviderIdFixture running against a real SQLite database with the real five unique indexes and the issue's exact data shape (source 416 owning az:B000APO0PQ, survivor 1577):

  • saving_the_survivor_while_the_source_still_owns_the_id_reproduces_issue_49 — pins the failure mode: UNIQUE constraint failed: Authors.AudnexusAuthorId
  • handoff_should_transfer_all_unique_provider_ids_without_violating_the_indexes — clean transfer + source deletion
  • rollback_should_leave_the_source_untouched — abort-safety preserved

Books suite: 568/568 passing.

…till owns them

Persist the survivor without the contested provider ids first (preserving the
abort-safety the method is named for), then hand the ids over inside the merge
transaction: release them on the source row, apply them to the survivor, and
delete the source atomically.
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.

[BUG] Duplicate author merge repeatedly fails on unique AudnexusAuthorId

1 participant