-- Usage metering. Every billable event is one usage_events row keyed by its idempotency key; the -- triggers fold it into per-account and per-repo period aggregates in the same write, so a retried -- event (INSERT OR IGNORE on the same key) can never be counted twice. CREATE TABLE usage_events ( idempotency_key TEXT PRIMARY KEY, account_id TEXT NOT NULL, meter TEXT NOT NULL, period TEXT NOT NULL, quantity REAL NOT NULL, repo_id TEXT, attributes TEXT, created_at INTEGER NOT NULL ); CREATE INDEX usage_events_account ON usage_events (account_id, period, meter); CREATE INDEX usage_events_created ON usage_events (created_at); -- Counter meters: running totals per period. reserved holds in-flight hard-cap reservations. CREATE TABLE usage_periods ( account_id TEXT NOT NULL, meter TEXT NOT NULL, period TEXT NOT NULL, quantity REAL NOT NULL DEFAULT 0, reserved REAL NOT NULL DEFAULT 0, updated_at INTEGER NOT NULL DEFAULT 0, PRIMARY KEY (account_id, meter, period) ); CREATE TABLE usage_repo_periods ( account_id TEXT NOT NULL, repo_id TEXT NOT NULL, meter TEXT NOT NULL, period TEXT NOT NULL, quantity REAL NOT NULL DEFAULT 0, PRIMARY KEY (account_id, repo_id, meter, period) ); -- Daily series for the "over time" chart. CREATE TABLE usage_days ( account_id TEXT NOT NULL,