Converge loan code on upstream, with Extra repayments slice 1
Closes jaysbeekay/sure#475. Part of jaysbeekay/sure#470 (Extra repayments reaches fork main through upstream's own code).
Base is main. #618 merged on 3 Nov as efd06c16d. This branch already carries the sync (7644c0333, merged down in 41807b113), and main's tree equals that commit's, so the diff is this change only and the suite run on 9bbe5a459 stands.
Fork main's loan code goes back to upstream's at 3ce2afd73, except the collateral card and the amortisation API, which the owner kept on 2 Nov. Upstream has merged what this PR once waited on (#3639, #3956, #4174, #4319), so the earlier cherry-picks are superseded by upstream's code.
What you lose and keep
Removed: offset accounts, saved scenarios and their extra repayments, the persisted amortisation table and its rebuild job, the fork's day-count engine, the rate-change table, the fork's Extra repayments tab, lib/tasks/loans.rake and its CI steps, docs/loans/* and the golden-master fixtures.
Kept, on upstream's Loan:
- Collateral:
Loan::Collateralizableholds what upstream lacks, soloan.rbdiffers from upstream by one line. The column,Loan::CollateralPosition,UI::Account::CollateralPosition, the form field, the cards on loan, property and vehicle pages, and the export and import link are unchanged in behaviour. GET /api/v1/loans/:id/amortization_schedule: same URL and JSON keys, fromLoan::AmortizationReportoverloan.amortization_schedule.payments. Payments are paged in memory.
Back from upstream: down payment, insurance, repayment progress, the payoff chart, UI::PeriodPicker's options:, demo loans, Extra repayments, the day-count select, Redbark's processor, the Plaid LoanTermWriter and the SimpleFIN processor. None of these is a fork difference any more.
What the API does differently
schedule.statusis alwayscurrent.staleandmissingare gone, because nothing is stored.- A change to a loan's terms shows in the next response. There is no rebuild, and a read writes nothing and enqueues nothing.
- A loan behind its contract has
payoff_projection.projected_payoff_date: null, because upstream's projection never runs past maturity. It serialises, and a test pins it. - Each row's
interest_rateis the rate that accrued over that payment, tested against the schedule's own run across a rate change.beginning_balanceis the previous row's closing balance.
Migration notes
20261017090000_drop_fork_loan_tables_and_columns:
- Drops:
loan_extra_repayments,loan_scenarios,loan_offset_accounts,loan_amortizations, and the checkschk_loans_interest_rate_boundsandchk_loans_term_months_bounds. - Keeps:
loans.collateral_account_id, and every loan'sday_count_convention. Only the check and the default change: new loans getthirty_360, existing loans keepactual_365oractual_actual. - THE DATA IS DELETED (accepted by the owner, 16 Oct and 2 Nov). It stops first if any saved scenario, scenario extra repayment or offset link exists, naming the counts, until
FORK_LOAN_DROP_CONFIRMED=1is set. A deploy that runsdb:prepareon such an instance won't boot until someone decides. The derivedloan_amortizationsisn't guarded.
Before deploying:
- Take a database backup. The deleted rows can't be recovered any other way.
- Deploy. If the migration stops, read the counts it names and decide whether that data can go.
- Only then set
FORK_LOAN_DROP_CONFIRMED=1for one deploy, and unset it afterwards. - Read the migration's output for loans with a rate change dated before origination. Each one fails every save until that rate change is moved or removed.
20260918210000_add_day_count_convention_to_loans keeps its guard rather than upstream's exact text, so it stays a fork difference. A database from before #618 already has the column, and upstream's unguarded add would raise. With the guard, the drop migration no longer records that version by hand, and upstream's test of the unguarded form is removed. 20260908145118 keeps its guard too.
Migration evidence, on beastpc:
| Start | Result |
|---|---|
main's schema with seeded loans, an offset, a collateral link and amortisation rows | Refused without the variable, naming loan_offset_accounts: 1. With it, deleted 1 offset and 1 amortisation row. Both loans kept their day-count and the link survived |
| #618's schema, same seed | Same |
| Both of the above | Dump byte-identical to the committed schema.rb |
| Empty database | Rails 8.1's db:migrate loads schema.rb. No migrations pending |
| Down then up on seeded data | Day-counts and link intact, schema identical |
Running every migration from an empty database through the migrator directly stops at an early 2024 migration, before any of this PR's, so that isn't a path this PR can test.
Blast radius
| Caller | Outcome |
|---|---|
RetirementPlan#seed_loans | Uses the projection's first payment. A loan behind its contract has no payoff date, so it seeds no stream, as a loan behind its contract seeds no stream pins |
Plaid mortgage and student-loan processors, LoanTermWriter | Upstream's, byte for byte |
RedbarkAccount::LoanDetailsProcessor, SimplefinAccount::Liabilities::LoanProcessor | Upstream's, byte for byte |
Family::DataExporter, Family::DataImporter | Unchanged: loans export last and the collateral link imports |
Demo::Generator | Upstream's |
Account | Upstream's opening-anchor rule. Offset hooks gone; collateral hook and secured_loans kept |
Assistant::Function::GetAccounts | Upstream's payment_in_force |
Account::Chartable, UI::Account::Chart | Upstream's loan shading, plus the fork's net contributions and all-time clamp |
config/routes.rb | Web resources :loans and the API route stay |
Deleted symbols. Grep over app lib config test spec db/migrate docs/api bin .github, outside the drop migration and its test, finds none of: OffsetResolver, LoanOffsetAccount, LoanScenario, LoanExtraRepayment, LoanAmortization, LoanAmortizationRebuildJob, ExtraRepaymentComparison, InterestAccrual, RepaymentPlan, RateChangeTable, current_minimum_payment, rebuild_amortization_schedule, ensure_amortization_schedule_current, schedule_current, offset_account_ids, or the loans: rake tasks. One comment in 20260908145118 names the removed migration that created the columns. variable_rate_update_for remains because upstream's Loan has it.
Evidence
Merged origin/sync/upstream-2026-11-02: 12 conflicted files, all resolved, then loan files reset to 3ce2afd73.
bin/rails test: 15776 runs, 71623 assertions, 0 failures, 0 errors, 37 skips, on the review fixes (9c0dd7049), before one further migration test was added; that test's files were re-run after it, 39 runs, 0 failuresrake rswag:specs:swaggerize: 456 examples, 0 failures, 89 pending- RuboCop on 1163 changed files: no offenses. erb_lint on 251 changed files: no errors. Biome on 39 changed JS files: clean. Brakeman: 0 warnings
- 27 mutations caught at
e6d0fa42f, and 9 more on the review fixes, across the report, API scoping, collateral rules, the account hook, controller protection and param, all three cards, export order, import link and the migration guard test_systemis left to this PR's CI
Not done as the process asks: the API and collateral tests were written with the code rather than watched failing first, so the mutations are the evidence they tell old from new. The tests for the review fixes below did fail first.
Since 9c0dd7049: 41807b113 merges #618's 7644c0333 down, and its full suite ran 15,780 runs with 0 failures. 9bbe5a459 fixes the locale merge defect the screenshots exposed. Its test failed first, the full suite then ran 15,780 runs with 0 failures, and one reviewer found nothing.
Code review (four reviewers, on e6d0fa42f): five findings, all fixed in 9c0dd7049, which was not re-reviewed:
interest_savedwent out as a formatted amount. It's a plain number again.next_rate_change_datereadDate.currentin its body. It now takes the date.- A comment recorded the old design. Removed.
- A rollback aborted on a loan saved outside the fork's bounds since
up. The restored checks now apply to new writes only. - Upstream's
Loanrefuses a rate change dated before origination on every save of the loan and of its account, including a plain rename (reproduced). The fork never did, so such a row can already exist. The migration names those loans in its output and log and keeps the rate. A linked loan in that state will fail its sync until the rate is corrected on the form.
Screenshots
The fixture mortgage on both trees, secured by the fixture property. Before is fork main; after is this branch at 9bbe5a459.
Before (fork main) | After | |
|---|---|---|
| Loan page | ![]() | ![]() |
| Edit form | ![]() | ![]() |
| Secured property | ![]() | ![]() |
- Loan page: upstream's page, with its chart and overview cards. The "Secured by" card (value, debt secured, equity) is kept below them, and so is the link to the property.
- Edit form: upstream's form with the kept "Secured by" picker. The interest basis reads 30/360 after, because the fixture loan sets no basis and this branch changes the column default for new loans. A stored
actual_365oractual_actualis kept (Migration notes). - These screenshots caught a merge defect: the interest-basis explanation had been attached to the "Secured by" hint. It's fixed in
9bbe5a459, test first.





