Skip to content

fix(database): include meta_data in GetBalanceByIDLite - #351

Open
vjymisal0 wants to merge 2 commits into
blnkfinance:mainfrom
vjymisal0:fix/balance-lite-missing-metadata
Open

fix(database): include meta_data in GetBalanceByIDLite#351
vjymisal0 wants to merge 2 commits into
blnkfinance:mainfrom
vjymisal0:fix/balance-lite-missing-metadata

Conversation

@vjymisal0

Copy link
Copy Markdown

Bug

GetBalanceByIDLite (database/balance.go) selects balance_id, indicator, currency, ledger_id, balance, credit_balance, debit_balance, inflight_balance, inflight_credit_balance, inflight_debit_balance, created_at, version, track_fund_lineage, allocation_strategy, identity_idmeta_data is not in the list, so every *model.Balance it returns has MetaData == nil, no matter what's stored in Postgres.

This function is the default path in getSourceAndDestination (transaction_execution.go) used to look up the source/destination balance for a transaction when EnableQueuedChecks is off. postTransactionActions then hands that balance straight to search.NewIndexBatch / batch.AddDependency("balances", ...), which re-upserts it into Typesense. Because MetaData is nil on the struct (not because it was actually cleared in the DB), the balance's real metadata gets wiped from the search index on every transaction touching that balance.

Repro from the issue:

  1. Create a balance with meta_data.
  2. Confirm it's indexed correctly via /search/balances.
  3. Run any transaction against that balance.
  4. Search again — meta_data is now empty, even though SELECT meta_data FROM blnk.balances WHERE balance_id = ... still shows it in Postgres.

Fix

Add meta_data to the SELECT and scan it into a []byte, then unmarshal into balance.MetaData when non-empty — the same nil-safe pattern already used by GetSourceDestination and GetAllBalances elsewhere in this file, so this brings GetBalanceByIDLite in line with its siblings rather than introducing a new pattern.

Scope note

GetBalancesByIDsLite (the bulk version, used by transaction_coalescing.go and lineage_allocation.go) has the identical gap — its SELECT also omits meta_data. I left it untouched since issue #256 is specifically about the single-lookup path and I wanted to keep this PR focused; happy to open a follow-up for the bulk path if useful.

Testing

  • Updated the three existing GetBalanceByIDLite sqlmock tests for the new query text/columns.
  • Added TestGetBalanceByIDLite_Success assertion that meta_data round-trips (was previously not asserted at all, which is how this shipped).
  • Added TestGetBalanceByIDLite_NilMetaData for the NULL meta_data case.
  • go test ./database/... passes (three env-dependent _RealDB tests skip locally, no Postgres running — unrelated to this change).
  • go vet ./... and golangci-lint run ./database/... are clean on the changed files (one pre-existing, unrelated staticcheck note elsewhere in balance.go at line ~1455, outside this diff).
  • gofmt clean.

Fixes #256

GetBalanceByIDLite's SELECT omitted meta_data, so every balance it
returned had MetaData == nil regardless of what was stored. This
function backs the source/destination lookups in
getSourceAndDestination (transaction_execution.go), whose result is
passed straight to the Typesense index batch in
postTransactionActions. The nil MetaData overwrites the balance's
real metadata in the search index on every transaction, even though
the row in Postgres is untouched.

Add meta_data to the query and scan/unmarshal it into balance.MetaData,
following the same nil-safe pattern already used in
GetSourceDestination and GetAllBalances.

GetBalancesByIDsLite (the bulk sibling of this function, used by
transaction_coalescing.go and lineage_allocation.go) has the same gap
and is a good candidate for a follow-up fix, but is out of scope here
since issue blnkfinance#256 is specifically about the single-balance lookup path.

Fixes blnkfinance#256
Copilot AI lite review requested due to automatic review settings August 9, 2026 06:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@CLAassistant

CLAassistant commented Aug 9, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@jerry-enebeli

Copy link
Copy Markdown
Collaborator

Hi @vjymisal0, thanks for the PR. There are some failing tests. Could you please look into them?

…anceByIDLite

The previous commit added meta_data as a 16th column to the
GetBalanceByIDLite query/Scan, and updated database/balance_test.go's
mocks accordingly, but missed the sqlmock expectations in the
top-level account_test.go and transaction_test.go, which still
supplied 15 columns. This caused CI's test and race jobs to fail
with "sql: expected 15 destination arguments in Scan, not 16".

Add "meta_data" to every affected NewRows()/AddRow() pair and to the
balanceQuery regex literals in transaction_test.go so they match the
16-column query GetBalanceByIDLite now issues.
@vjymisal0
vjymisal0 force-pushed the fix/balance-lite-missing-metadata branch from ba26db3 to b9c9e47 Compare August 15, 2026 02:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Balance metadata on typesense is empty after recording a transaction with the balance.

4 participants