Port #3557's pending SQL refinements to main

Merged
JA
2mo ago
+209-59

Summary

This closes the drop 0 single-copy gap that #118 records against we-promise/sure#3557's pending-SQL half: "reconciled after the next weekly sync, not ported onto a stale base". #264 has merged, and fork main is level with upstream main, so the port now lands on a current base.

Fork main has the first pending-SQL change (#138) but none of the refinements #3557 took afterwards. This PR ports them with cherry-pick -x; all six applied cleanly:

#3557 commitHereWhat it changes on main
1f77fcab95a84fba3dBuilds the correlated pending SQL lazily.
74c92d1d57baedf492Builds pending SQL without borrowing a database connection at class load.
dab7c69497052bf616Shares one list of false flag values across every pending SQL builder, so they cannot quote the values differently.
44b44fc80bf1b80585Decides Monobank's stale-pending prune the way Transaction#pending? does.
1b3f564a322d4e58e9Reads pending provider metadata through one predicate (Transaction.pending_extra?). A provider value that isn't a hash can no longer raise TypeError and abort an import (CodeRabbit's finding on #3557).
380b71f7054a32e998States the leading-AND contract on pending_providers_sql.

Plus one commit of mine, 347b532f6. It removes Account::ProviderImportAdapter::PENDING_LOOKUP_SQL, which the ports left with no callers; #3557 had already deleted it. It still spelled pending with the ::boolean cast that raises on an unparseable flag. The same commit adds one blank line to transaction_test.rb so the file matches #3557's head.

Single-copy check against up/pending-sql-parity (1c89d1159)

These files are now byte-identical to #3557's head: transaction.rb, entry_search.rb, entry.rb, the Monobank, Akahu and Up processors, simplefin_item/importer.rb, goal/withdrawal_detector.rb, pending_duplicate_merges_controller.rb, the SimpleFIN pending rake task, and their tests, including transaction_test.rb.

Remaining differences, all deliberate:

  • provider_import_adapter.rb, +6 on the fork. Two fork-only features that aren't part of #3557: the cc_payment kind for credit-card payments, and stamping updated_at in the collision handler (InvestmentStatement#holdings_version keys on it).
  • portfolio/flow_classifier.rb, its test and portfolio_flow_test_helper.rb. Comment lines that reference the fork's contract gate. #262 already declared this divergence, and the gate isn't sent upstream (#118).
  • Fork-only tests: 57 lines in income_statement_test.rb, 13 in importer_stale_unmatched_pending_test.rb, and a composite-key collision stamps updated_at on the row it adopts in provider_import_adapter_test.rb.
  • Two upstream tests are missing from fork main, and this PR does not restore them: preserves existing trade fee when reimport omits it (from upstream #3507) and find_pending_transaction covers every pending-capable provider (from upstream #3386). Both are on upstream main, so an earlier sync dropped them from the fork. #279 restores them.

Behaviour changes (release note)

These apply only to malformed or false flags. Every provider writes a real boolean, and both the old rule and the new one treat it the same way.

  • An unparseable stored monobank.pending flag (e.g. "maybe") is now treated as pending and pruned. Before, the ::boolean cast raised PG::InvalidTextRepresentation and aborted the whole stale-pending prune for that sync (bf1b80585).
  • A stored pending flag of "false" is now left alone when a booked transaction reuses the pending entry's external_id. Before, it was read as pending, and the booked sync deleted it (22d4e58e9).
  • A provider namespace in extra that is not a hash now reads as not pending. Before, Hash#dig raised TypeError inside Account.transaction and aborted the import (22d4e58e9).

Validation

  • Every test file #3557 touches, plus transaction/search_test, transactions_controller_test and investment_statement_test: 495 runs, 2,342 assertions, 0 failures.
  • The ported tests catch a reverted fix:
    • With 22d4e58e9's app code reversed (the shared predicate): 1 failure and 3 errors across provider_import_adapter_test, the Monobank processor test and transaction_test.
    • With bf1b80585's reversed (the Monobank prune): 1 error.
    • Both were restored before pushing.
  • bin/rubocop on the 9 changed Ruby files: no offenses.
  • CI runs the full suite on this branch.