textmachine/platform/internal/pgstore/migrations/00003_usage.sql

24 lines
1.1 KiB
SQL

-- +goose Up
-- P-5 draft (D39.100 / PT-35). Money is METERED here and never leaves: the API projects a state
-- and a percentage, never a sum (D39.84). Micro-USD integers, not floats — the engine's own ledger
-- is a lower bound and we must not add rounding drift on top of it.
--
-- The platform meters from `tmctl status --json` (committed_usd) at attempt boundaries, because the
-- event stream deliberately carries no figures: the ceiling event says `halted`, nothing more.
create table usage_windows (
user_id text not null references users (id) on delete cascade,
period text not null check (period in ('day', 'week')),
started_at timestamptz not null,
ends_at timestamptz not null,
spent_micro_usd bigint not null default 0,
limit_micro_usd bigint not null,
primary key (user_id, period, started_at)
);
-- The window a user is currently inside is the only one the limits page reads.
create index usage_windows_current_idx on usage_windows (user_id, ends_at desc);
-- +goose Down
drop table usage_windows;