mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-15 04:30:55 +08:00
Move the llm_records and llm_usage DDL out of Go constants into embedded files db/migrations/0001_llm_records.sql and 0002_llm_usage.sql (byte-for-byte the old constants). Open now applies pending migrations after schema.sql and before seedBuiltins, under the existing schema advisory lock and on the lock-holding connection. Each migration runs in its own transaction with its schema_migrations row (version, name, SHA-256 checksum, applied_at). Open fails closed when the database has a version missing from the binary, or an applied migration whose file name or checksum changed. A failing migration is rolled back and blocks startup; earlier ones stay applied. EnsureLLMRecordsTable and EnsureLLMUsageTable re-run the embedded files and keep their transaction-level schema lock; Open never calls them.
20 lines
731 B
SQL
20 lines
731 B
SQL
|
|
CREATE TABLE IF NOT EXISTS llm_usage (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
ts TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
task_id TEXT,
|
|
exploration_id BIGINT,
|
|
worker TEXT,
|
|
model TEXT,
|
|
profile_name TEXT,
|
|
latency_ms INTEGER,
|
|
input_tokens INTEGER NOT NULL DEFAULT 0,
|
|
output_tokens INTEGER NOT NULL DEFAULT 0,
|
|
cache_read INTEGER NOT NULL DEFAULT 0,
|
|
cache_write INTEGER NOT NULL DEFAULT 0,
|
|
status TEXT
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_llm_usage_task ON llm_usage(task_id);
|
|
CREATE INDEX IF NOT EXISTS idx_llm_usage_model ON llm_usage(task_id, model);
|
|
CREATE INDEX IF NOT EXISTS idx_llm_usage_exp ON llm_usage(exploration_id);
|