refactor(usage): address round-4 review on PR #871

- hooks: document why user_id in metadata is safe — it stays tenant-local
  (the CP rollup aggregates GROUP BY (day, metric) into usage_daily, which
  has no metadata column, so it never reaches Stripe) and is retained to
  keep Deck #67's future per-user attribution derivable from the app DB.
- migration: instantiate the SQLite-side column types (sa.Text() etc.) for
  visual parity with the instantiated Postgres types.
- tests: assert the WARNING contract in the unserializable-metadata test
  too; add an autouse fixture that resets UsageEventStore._shared_instance
  so a stray shared() call can't leak across tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-06-07 15:42:12 +02:00
co-authored by Claude Opus 4.8
parent 3a8ea893c6
commit 9c2f9fac46
4 changed files with 43 additions and 9 deletions
@@ -44,7 +44,7 @@ def upgrade() -> None:
# both backends (see usage/store.py).
sa.Column(
"event_id",
postgresql.UUID(as_uuid=False) if is_pg else sa.Text,
postgresql.UUID(as_uuid=False) if is_pg else sa.Text(),
primary_key=True,
),
# Operation completion time (UTC). Real TIMESTAMPTZ on Postgres so the
@@ -52,18 +52,18 @@ def upgrade() -> None:
# TIMESTAMP on SQLite (stored as ISO text, queryable in tests).
sa.Column(
"occurred_at",
postgresql.TIMESTAMP(timezone=True) if is_pg else sa.TIMESTAMP,
postgresql.TIMESTAMP(timezone=True) if is_pg else sa.TIMESTAMP(),
nullable=False,
),
# Catalog metric: 'embeddings_queries' or 'pages_chunks'.
sa.Column("metric", sa.Text, nullable=False),
sa.Column("value", sa.BigInteger, nullable=False),
sa.Column("metric", sa.Text(), nullable=False),
sa.Column("value", sa.BigInteger(), nullable=False),
# Rawest unit per request (provider, model, tokens, doc_type, ...).
# JSONB on Postgres so the CP can slice on dimensions later; TEXT
# (json.dumps) on SQLite.
sa.Column(
"metadata",
postgresql.JSONB() if is_pg else sa.Text,
postgresql.JSONB() if is_pg else sa.Text(),
nullable=True,
),
)