Skip to content

chore(db): backfill purchase_history.cloud_account_id from (provider, external_id) #22

Closed

Description

@cristim

Follow-up to the account-filter representation fix

The account-filter fix (rows 305/306/384/431/452, issues LeanerCloud/cloud-commitments-cli#701/#498/#866) resolves filtering by matching BOTH purchase_history.cloud_account_id (UUID) and account_id (provider external id) at query time, because cloud_account_id is NULL on many rows (migration 000011 added it without a backfill; direct-execute / ambient / legacy write paths leave it NULL).

This issue tracks the optional one-time backfill to converge historical rows onto the UUID column so future code can rely on it:

UPDATE purchase_history ph
SET cloud_account_id = ca.id
FROM cloud_accounts ca
WHERE ph.account_id = ca.external_id
  AND ph.provider   = ca.provider
  AND ph.cloud_account_id IS NULL;

Notes / risks

  • Must match on (provider, external_id) (the cloud_accounts UNIQUE(provider, external_id) constraint), NOT external_id alone, to avoid cross-provider/tenant collisions.
  • Not required for correctness (the resolve-at-query fix handles it); this is cleanup/convergence only.
  • After backfill + a soak period, the dual-column predicate could be simplified back to UUID-only (separate change).

Acceptance criteria

  • Migration backfills cloud_account_id for all rows with a resolvable (provider, external_id).
  • Rows with no matching cloud_accounts entry are left NULL (and still filterable via the external-id branch).
  • A test verifies the backfill maps correctly and does not mis-assign across providers.

Activity

  1. IAmJSD commented on Oct 20, 2026

    @IAmJSD
    Collaborator

    Claiming: working on this

  2. self-assigned this
    on Oct 20, 2026
  3. added a commit that references this issue on Oct 25, 2026
    abd3b17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions