fix(sync): order changelog FK targets (users) first during pull#10018
fix(sync): order changelog FK targets (users) first during pull#10018dannash100 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 90f8aff. Configure here.
|
🦸 Review Hero Summary Below consensus threshold (2 unique issues not confirmed by majority)
Local fix prompt (copy to your coding agent)Fix these issues identified on the pull request. One commit per issue fixed.
Fix: qualify constraint names with their schema in the SELECT query: |
e906735 to
febd3a4
Compare
|
🦸 Review Hero Summary No issues found. Looks good! Below consensus threshold (2 unique issues not confirmed by majority)
|
febd3a4 to
4d2aa80
Compare
4d2aa80 to
56a8ede
Compare
|
🦸 Review Hero Summary Below consensus threshold (3 unique issues not confirmed by majority)
Local fix prompt (copy to your coding agent)Fix these issues identified on the pull request. One commit per issue fixed.
|
|
🦸 Review Hero Summary Below consensus threshold (2 unique issues not confirmed by majority)
Local fix prompt (copy to your coding agent)Fix these issues identified on the pull request. One commit per issue fixed.
|
Changelog rows are replayed during a pull attached to the records they describe and inserted by insertChangelogRecords during that model's batch. Each row's updated_by_user_id references users(id), but sortInDependencyOrder emits ready models alphabetically, so users (u) is saved after models like invoice_price_lists (i) whose changelog rows reference not-yet-saved users — violating changes_updated_by_user_id_fkey and aborting the whole pull, which then recurs every sync because the cursor never advances. ChangeLog is DO_NOT_SYNC so its FK dependency is invisible to the sort. Derive the targets from ChangeLog's BelongsTo associations (currently just users) and treat every other model as depending on them, so they are saved first. Also bail with a clear error instead of looping forever if a dependency cycle is ever introduced. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
56a8ede to
e03b6e5
Compare

Changes
Facility sync could dead-loop with:
Why it happens. Changelog rows are replayed during a pull attached to the records they describe and inserted by
insertChangelogRecordsduring that model's batch. Each row'supdated_by_user_idreferencesusers(id). ButsortInDependencyOrderemits ready models alphabetically, sousers(u) is saved after models likeinvoice_price_lists(i) — whose changelog rows then reference users that aren't in the table yet. That violateschanges_updated_by_user_id_fkeyand aborts the whole pull transaction, which recurs every sync because the cursor only advances on success.The
ChangeLog → Userdependency is invisible to the topological sort becauseChangeLogisDO_NOT_SYNC, so it never appears as aBelongsToedge on a pulled model.Fix. Derive the dependency from
ChangeLogitself: read itsBelongsTotargets (currently justUser, viaupdated_by_user_id) and treat every other model as depending on those targets. The existing topological sort then orders them first, and it stays correct automatically iflogs.changesever gains another FK. A guard throws a clear error instead of looping forever if a dependency cycle is ever introduced.Notes
Known limit: if a facility has more users than
persistedCacheBatchSize, a changelog row in an earlier user batch could reference a user in a later user batch (user-edited-by-user across the batch boundary). This fixes the reported case (changelog on other tables referencing users) completely; that cross-user-batch edge is separate.Review Hero
🤖 Generated with Claude Code