fix(enable_banking): tell id-less rows that share a content hash apart
Enable Banking: content-hash IDs collide, losing transactions
Problem
transaction_id and entry_reference are both optional in PSD2, and some ASPSPs
send neither. EnableBankingEntry::Processor.compute_external_id already handles
that with a deterministic content hash:
content = [ date, amount, currency, direction, creditor, debtor, remittance_key ]
.map(&:to_s).join("\x1F")
"enable_banking_content_#{Digest::MD5.hexdigest(content)}"
The assumption is that those seven fields identify a transaction. For an ASPSP
that sends only the first four they do not, and the hash degrades to
date + amount + currency + direction.
Account::ProviderImportAdapter#import_transaction then does
entry = account.entries.find_or_initialize_by(external_id: external_id, source: source)
so two transactions of the same amount, same direction, on the same day are one
transaction: the second updates the first instead of creating a new row. Nothing
raises, nothing is logged, and the counts look right because the importer only ever
saw one entry to make.
How bare a payload can get
Trade Republic, through Enable Banking, returns 4 of 24 fields. The shape of
every record on such an account (figures replaced, shape as received):
{
"transaction_amount": { "currency": "EUR", "amount": "1000.000000" },
"credit_debit_indicator": "CRDT",
"status": "BOOK",
"booking_date": "2026-01-15",
"transaction_id": null, "entry_reference": null,
"creditor": null, "creditor_account": null, "creditor_agent": null,
"debtor": null, "debtor_account": null, "debtor_agent": null,
"creditor_account_additional_identification": null,
"debtor_account_additional_identification": null,
"bank_transaction_code": null, "remittance_information": null,
"reference_number": null, "reference_number_schema": null,
"merchant_category_code": null, "balance_after_transaction": null,
"value_date": null, "transaction_date": null,
"exchange_rate": null, "note": null
}
Every transaction on that account has the same shape. For comparison, three
other ASPSPs linked to the same Sure instance each populate 10 to 12 of the 24
fields and carry a provider id on most or all rows; this one populates 4 and
carries an id on none.
So the collision is not hypothetical for this provider -- it is the only thing
standing between two same-day same-amount transfers and silent data loss. The
account this was found on has two deposits of the same amount on one day in its
history; they survive only because that period was imported from CSV rather than
the feed.
The same account also shows the feed degrading over time: rows fetched under an
earlier consent carried remittance_information, rows fetched under the current
one do not. An ASPSP that is rich today can be bare tomorrow, so this is not
only about one bank.
Reproducing
Two entries in one raw_transactions_payload, identical but for nothing:
tx = { "transaction_amount" => { "amount" => "100.00", "currency" => "EUR" },
"credit_debit_indicator" => "CRDT", "status" => "BOOK",
"booking_date" => "2026-01-15" }
EnableBankingEntry::Processor.compute_external_id(tx) ==
EnableBankingEntry::Processor.compute_external_id(tx.dup) # => true
Import a payload containing both and the account gains one transaction, not two.
Change
Rows of one fetched batch that share a content hash get a suffix on the id,
worked out over the whole batch before any row is imported. Two constraints
shape it:
- Rows that carry a provider id must keep the id they have. Every other
ASPSP takes thetransaction_id/entry_referencebranch and is not touched. - An unsuffixed row must hash to exactly today's value. Entries already
imported holdenable_banking_content_<md5>from the current seven fields;
if that changed, every id-less row on every install would be re-imported as a
duplicate. So a row alone in its group, and the first of a set of identical
rows, keep the bare hash. Pinned by a literal digest in the tests.
The suffix has to be an identity, not a position: the next response may
order the rows differently, carry only one of them, or add a third. So the
identities a group already has are resolved first, from the ledger and from
the ids excluded because they were merged away: a member whose full-content id
is known keeps it; a known bare id goes to the member whose recorded fingerprint
it carries, or to the only member, or to any of a set of identical members. If a
bare entry predates fingerprints and the members are distinguishable, it is left
alone and the row imports under its own id: one visible duplicate rather than a
silent update of the wrong transaction. Only what is left gets a new id:
- rows identical in every field are interchangeable; the first keeps the
bare hash and the rest are numbered (_1,_2, ...). This is the bare-row
provider's case, and it also keeps the upgrade clean: an account that already
holds one of two such rows under the bare hash gains exactly the missing one; - rows that share the hashed fields but differ elsewhere, in a
notesay,
each take a suffix hashed from their full content (every field, keys sorted),
which no reordering or partial response can move; - every id-less row records its full-content fingerprint in
extra.enable_banking.content_fingerprint, so from now on the ledger can say
exactly which row an entry was made from.
One query per batch for the known identities of the content hashes present, and
only when the batch has id-less rows at all. compute_external_id(row, suffix:)
appends the suffix as _<suffix>; the batch processor works out the suffixes and
hands each row its own, with its fingerprint, through the per-row
EnableBankingEntry::Processor. Repeats within one response survive, repeats
across syncs still collapse.
The importer has to let the rows through
The suffixes run in EnableBankingAccount::Transactions::Processor, over the stored
payload, but EnableBankingItem::Importer decides what reaches that payload, and
it dropped the colliding rows first:
deduplicate_api_transactions(#954) collapsed rows with identical content
within one response. It now leaves alone a row with neithertransaction_id
norentry_reference; the #954 duplicates carry different
entry_references and still collapse.- a fetched row was new only if its bare id was not stored, so the second of two
same-day transfers was discarded when the first was already in. Id-less rows
are now compared per content hash by count: the hash includes the date and the
fetch covers whole dates. Among fetched rows that differ in an unhashed field,
those whose full content is not stored yet are taken; a row whose unhashed
fields merely changed is not stored twice.
Alternatives considered
- Add
balance_after_transactionto the key. It would disambiguate perfectly,
since the running balance differs between two otherwise identical rows. Null on
this provider, so it helps nowhere it is needed -- but worth adding to the key
anyway for ASPSPs that do send it. - Number repeats by response order only. Simple, but a rank is not an
identity: reorder the rows, or deliver only one of them, and each takes the
other's id. Kept only for rows identical in every field, where it cannot matter. - Count existing entries with the same digest and suffix from that. Makes the
id depend on import history rather than on the payload, so a re-sync of the same
window produces different ids. Rejected; the ledger is consulted only to keep a
full-content id a row already has. - Refuse to import rows with no identifier. Honest but worse: it would drop the
whole account.
Validation
EnableBankingEntry::ProcessorTest: a suffix is appended as_<suffix>and a
nil suffix changes nothing; an unsuffixed row's digest is pinned as a literal;
a row withtransaction_idorentry_referenceignores the suffix.EnableBankingAccount::Transactions::ProcessorTest: the same bare row twice
imports two transactions and a second pass adds nothing (fails before the
change); two rows differing only innote, re-imported in reverse order, keep
their ids and notes (fails with positional numbering); one of them arriving
alone later keeps its id and leaves the other untouched; a row first imported
alone keeps its id when a distinguishable twin arrives, and only the twin is
new; a group that goes from distinguishable to identical-only keeps every
member's id (these three fail without the ledger resolution); a merged
collision row arriving alone is still excluded (fails without the exclusion
set being consulted); a distinguishable row arriving alone after another holds
the bare id takes its own id and leaves that row's data untouched (fails
without the fallback).EnableBankingItem::ImporterIdLessTest, throughfetch_and_store_transactions:
two identical bare rows in one response reach the payload and the ledger; the
second arriving on a later sync is stored and a third sync adds nothing (both
fail without the importer change); a row whose unhashed field changed is not
stored twice; rows differing only inentry_referencestill collapse (#954).- The ambiguous legacy case writes one
warnDebugLogEntryand leaves the
legacy entry untouched. test/models/enable_banking_item,test/models/enable_banking_entry,
test/models/enable_banking_account: 145 runs, 0 failures.bin/rubocopclean.
Notes
No migration. Existing entries keep their ids by construction (constraint 2), so
the fix changes behaviour only for a batch that actually contains repeats. One
case is left as it was: an account that, before this change, collapsed two
distinguishable rows into one entry has no fingerprint on that entry, so it
cannot be told which row it was; it stays under the bare hash while both rows
import under their own ids. That account was already wrong by one transaction; it
is now right by one extra, and the stray is the collapsed entry.
compute_external_id keeps accepting the row as bare keywords as well as a hash,
since existing callers and tests do both. The importer's own fingerprint
comparisons (pending-to-booked matching) still use the bare hash, so
two identical bare rows are still one fingerprint there; that path only decides
whether a pending row was replaced, and is unchanged by this.
Summary by CodeRabbit
- Bug Fixes
- Improved importing of transactions that lack provider IDs, helping prevent duplicate or missed entries across syncs, reordered results, and partial fetches.
- Preserved distinct transactions with matching basic details when their full content differs, while keeping booked and pending transactions separate.