Skip to content

fix(db): preserve parent_id on SQLite upsert conflict#4

Merged
zooqueen merged 2 commits into
mainfrom
fix/sqlite-parent-id-upsert
Feb 25, 2026
Merged

fix(db): preserve parent_id on SQLite upsert conflict#4
zooqueen merged 2 commits into
mainfrom
fix/sqlite-parent-id-upsert

Conversation

@zooqueen
Copy link
Copy Markdown
Contributor

Summary

  • Fix billing balance returning $0 by preserving parent_id column during SQLite upsert operations
  • The ON CONFLICT clause in Put(), PutMulti(), and sqliteTransaction.Put() only updated data, silently dropping parent_id on conflict — ancestor queries (WHERE parent_id = '1') then returned zero results

Root Cause

Commerce uses Google Cloud Datastore-style ancestor queries for billing. Transactions are children of a synckey root (parent_id = '1'). When an entity was upserted (INSERT...ON CONFLICT DO UPDATE), the UPDATE clause only set data and updated_at, omitting parent_id. This meant any re-saved transaction lost its parent linkage and became invisible to balance queries.

Fix

Add parent_id = excluded.parent_id to all three ON CONFLICT clauses in db/sqlite.go.

Test plan

  • CI passes
  • Deploy to K8s, deposit funds, verify GET /api/v1/billing/balance returns correct amount
  • Verify existing patched transactions still visible after redeploy

The ON CONFLICT clause in Put, PutMulti, and transaction Put only
updated the data column, dropping parent_id on upsert. This caused
ancestor queries (used by billing balance) to return zero results
because transactions stored without parent_id were invisible to
the WHERE parent_id = '1' filter.

Add parent_id = excluded.parent_id to all three ON CONFLICT clauses
so parent_id is always persisted on both insert and update paths.
The seed command imported the deprecated plan model which has no
orm.Register() call, causing a panic: "type plan.Plan not registered".
Switch to the active models/plan package which registers itself
via init().
@zooqueen zooqueen merged commit d34235a into main Feb 25, 2026
4 checks passed
@zooqueen zooqueen deleted the fix/sqlite-parent-id-upsert branch February 25, 2026 18:58
hanzo-dev pushed a commit that referenced this pull request May 13, 2026
* fix(db): preserve parent_id on SQLite upsert conflict

The ON CONFLICT clause in Put, PutMulti, and transaction Put only
updated the data column, dropping parent_id on upsert. This caused
ancestor queries (used by billing balance) to return zero results
because transactions stored without parent_id were invisible to
the WHERE parent_id = '1' filter.

Add parent_id = excluded.parent_id to all three ON CONFLICT clauses
so parent_id is always persisted on both insert and update paths.

* fix(seed): use active plan model instead of deprecated one

The seed command imported the deprecated plan model which has no
orm.Register() call, causing a panic: "type plan.Plan not registered".
Switch to the active models/plan package which registers itself
via init().
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.

1 participant