Keep split parts that are transfer legs intact
A split part can already become one leg of a transfer: through the match dialog, auto-match, the "set as transfer" rule and the importer. Unsplitting, re-splitting or deleting the parent then destroyed that part together with its transfer and left the other leg behind as a standalone entry. For a loan payment that is the entry on the loan, so the loan balance stayed reduced while the bank transaction went back to being a plain expense or disappeared.
Now:
- Unsplit, re-split and deleting the parent refuse while a part is a leg of a confirmed transfer. The split editor shows "A part of this split is linked as a transfer. Remove that transfer before you edit or unsplit the split." Deleting the parent shows a matching message, bulk delete skips such a split and says so, the API returns 422 and the assistant's delete tool returns an error. Pending auto-match suggestions don't block. Syncs and imports that remove entries are not affected.
- The check first locks the parts' transfers and transactions, so a transfer confirmed or created at the same moment is seen.
Transfer#confirm!,Transfer#destroy!and editing a transfer now lock the transfer row before its transactions, the order the split check takes them in, so an unlink, edit or confirm racing an unsplit or each other no longer deadlocks. A confirm on a transfer removed in the meantime raisesRecordNotFoundinstead of giving the surviving leg a transfer kind, and an unlink that waited for a confirm reads the legs again, so it resets the kinds the confirm set.- Editing a transfer's amount, and a split part's amount, currency or kind, no longer applies to split parts: either would make the parts stop adding up to the parent. The web form already disables these fields and the API already refuses them.
This is the first step of #4955 (one mortgage debit spread over several loan parts). The next step is #4784.
Validation
- New tests: the model refusal for unsplit, re-split and delete on both transfer legs (and that a pending transfer doesn't block), the web and API split controllers, the transactions controller, bulk delete, the API transaction delete, the assistant delete tool, the transfers controller (including that an edit locks the transfer first),
confirm!on a removed transfer, an unlink of a transfer loaded before a confirm, and a non-transactional concurrency test that races unsplit and delete against confirm and unlink. Without the new lock order inTransferthe race test fails in 2 of its 3 tests; with it, it passed 10 of 10 runs. bin/rails test: 18,224 runs, 0 failures.- System tests, after merging
mainbefore this change: 391 runs. Three failures in system tests this branch doesn't touch (OnchainWalletsTest,PropertiesEditTest,ChartSidebarResizeTest); all three pass on a rerun. This change touches no views or JavaScript. - RuboCop and Brakeman are clean.
- The 422 responses are documented in rswag and
docs/api/openapi.yaml. CI runs no rspec job, and locally the rswag request specs fail on a cleanmaintoo, so these specs only document; the Minitest controller tests cover the behavior.
Summary by cubic
Keeps split parts intact when one of them is a leg of a confirmed transfer. Unsplitting, re-splitting, or deleting the split used to destroy the part and its transfer, leaving the other leg behind as a standalone entry — for a loan payment, the loan balance stayed reduced while the bank transaction became a plain expense.
Behavior changes
- Unsplit, re-split, and delete are now refused while a part is a confirmed transfer leg; the web editor and controllers show an alert, the API returns 422, the assistant function returns an error, and bulk delete skips those splits while deleting the rest. Pending auto-match suggestions don't block.
- The check locks the parts' transfers and transactions so concurrent confirms or creates are seen, and retries up to three times when Postgres aborts the action in a deadlock. Editing a transfer now locks the transfer row before its legs in the same order.
- A racing confirm no longer leaves a loan leg with a transfer kind when the transfer was removed in the meantime; an unlink that loaded the legs before a confirm reloads them so it resets the kinds the confirm set.
- Editing a transfer's amount, or a split part's amount, currency, or kind, no longer applies to split parts; either change would make the parts stop adding up to the parent.
This is the first step of #4955; the next step is #4784.
Summary by CodeRabbit
- Bug Fixes
- Editing or removing a split is blocked when one of its parts is linked to a confirmed transfer. The split remains intact, and the app explains that the transfer must be removed first.
- API requests to edit or remove these splits return an error explaining that the transfer must be removed first.
- Editing a split part no longer changes its amount, currency, or transaction type; notes can still be updated.
- Editing a transfer linked to a split part no longer changes the part’s amount or the corresponding amount on the other side of the transfer.