-- Phase 1 schema (SPEC 5.4, ADR 0007). The same text runs on PostgreSQL and SQLite: the tokens in braces -- become the engine's column types (DbValues.ColumnType). Every table has a UUIDv7 key and created_at in UTC. -- Tables of later phases (bars, news, models, backtests, signals, decisions) arrive with their own migration. -- Identity of the installation and fingerprint of the live database (SPEC 15.1). Exactly one row. CREATE TABLE installation ( id {uuid} PRIMARY KEY, fingerprint TEXT NOT NULL, engine TEXT NOT NULL, protected_restart {bool} NOT NULL, created_at {timestamp} NOT NULL, renewed_at {timestamp} NOT NULL ){strict}; CREATE TABLE users ( id {uuid} PRIMARY KEY, username TEXT NOT NULL UNIQUE, password_hash TEXT NOT NULL, must_change_password {bool} NOT NULL, totp_secret {bytes}, created_at {timestamp} NOT NULL, updated_at {timestamp} NOT NULL ){strict}; CREATE TABLE sessions ( id {uuid} PRIMARY KEY, user_id {uuid} NOT NULL REFERENCES users (id), token_hash TEXT NOT NULL UNIQUE, user_agent TEXT, address TEXT, created_at {timestamp} NOT NULL, last_seen_at {timestamp} NOT NULL, expires_at {timestamp} NOT NULL ){strict}; CREATE INDEX sessions_user ON sessions (user_id); -- Settings saved from the interface; startup configuration values win over them and are never stored here. CREATE TABLE config ( id {uuid} PRIMARY KEY, key TEXT NOT NULL UNIQUE, value {json} NOT NULL, created_at {timestamp} NOT NULL, updated_at {timestamp} NOT NULL ){strict}; -- Change log of settings, activations and the kill switch (SPEC 13.5, 15.4). Append-only. CREATE TABLE config_history ( id {uuid} PRIMARY KEY, key TEXT NOT NULL, old_value {json}, new_value {json}, changed_by TEXT NOT NULL, created_at {timestamp} NOT NULL ){strict}; CREATE INDEX config_history_key ON config_history (key, created_at); -- Encrypted secrets (AES-GCM with the installation's secret key). Never returned by the API. CREATE TABLE secrets ( id {uuid} PRIMARY KEY, name TEXT NOT NULL UNIQUE, ciphertext {bytes} NOT NULL, created_at {timestamp} NOT NULL, updated_at {timestamp} NOT NULL ){strict}; CREATE TABLE jobs ( id {uuid} PRIMARY KEY, name TEXT NOT NULL UNIQUE, description TEXT NOT NULL, last_due_at {timestamp}, last_started_at {timestamp}, last_finished_at {timestamp}, last_outcome TEXT, last_error TEXT, created_at {timestamp} NOT NULL, updated_at {timestamp} NOT NULL ){strict}; CREATE TABLE notifications ( id {uuid} PRIMARY KEY, channel TEXT NOT NULL, kind TEXT NOT NULL, message TEXT NOT NULL, status TEXT NOT NULL, sent_at {timestamp}, created_at {timestamp} NOT NULL ){strict}; -- The bot's diary (SPEC 13.6): short sentences in Italian with a kind and a reason code. CREATE TABLE journal ( id {uuid} PRIMARY KEY, occurred_at {timestamp} NOT NULL, kind TEXT NOT NULL, code TEXT NOT NULL, message TEXT NOT NULL, data {json}, created_at {timestamp} NOT NULL ){strict}; CREATE INDEX journal_time ON journal (occurred_at); CREATE TABLE instruments ( id {uuid} PRIMARY KEY, venue TEXT NOT NULL, symbol TEXT NOT NULL, name TEXT NOT NULL, asset_class TEXT NOT NULL, quote_currency TEXT NOT NULL, created_at {timestamp} NOT NULL, UNIQUE (venue, symbol) ){strict}; CREATE TABLE accounts ( id {uuid} PRIMARY KEY, name TEXT NOT NULL UNIQUE, broker TEXT NOT NULL, environment TEXT NOT NULL, base_currency TEXT NOT NULL, created_at {timestamp} NOT NULL ){strict}; CREATE TABLE strategy_state ( id {uuid} PRIMARY KEY, strategy TEXT NOT NULL UNIQUE, account_id {uuid} REFERENCES accounts (id), stage TEXT NOT NULL, capital {money} NOT NULL, state {json} NOT NULL, created_at {timestamp} NOT NULL, updated_at {timestamp} NOT NULL ){strict}; CREATE TABLE strategy_stage_history ( id {uuid} PRIMARY KEY, strategy TEXT NOT NULL, from_stage TEXT, to_stage TEXT NOT NULL, reason TEXT NOT NULL, created_at {timestamp} NOT NULL ){strict}; CREATE TABLE orders ( id {uuid} PRIMARY KEY, account_id {uuid} NOT NULL REFERENCES accounts (id), instrument_id {uuid} NOT NULL REFERENCES instruments (id), strategy TEXT NOT NULL, client_order_id TEXT NOT NULL UNIQUE, broker_order_id TEXT, side TEXT NOT NULL, order_type TEXT NOT NULL, quantity {money} NOT NULL, limit_price {money}, stop_price {money}, status TEXT NOT NULL, created_at {timestamp} NOT NULL, updated_at {timestamp} NOT NULL ){strict}; -- Accounting tables (fills, lots, cash_ledger) are append-only: errors are fixed with reversal rows, never UPDATE. -- The same execution, from the stream and from the fallback query, is recorded once (account + execution id). CREATE TABLE fills ( id {uuid} PRIMARY KEY, account_id {uuid} NOT NULL REFERENCES accounts (id), order_id {uuid} REFERENCES orders (id), instrument_id {uuid} NOT NULL REFERENCES instruments (id), execution_id TEXT NOT NULL, side TEXT NOT NULL, quantity {money} NOT NULL, price {money} NOT NULL, fee {money} NOT NULL, fee_currency TEXT NOT NULL, reverses_id {uuid} REFERENCES fills (id), executed_at {timestamp} NOT NULL, created_at {timestamp} NOT NULL, UNIQUE (account_id, execution_id) ){strict}; CREATE TABLE lots ( id {uuid} PRIMARY KEY, account_id {uuid} NOT NULL REFERENCES accounts (id), instrument_id {uuid} NOT NULL REFERENCES instruments (id), fill_id {uuid} NOT NULL REFERENCES fills (id), strategy TEXT NOT NULL, quantity {money} NOT NULL, price {money} NOT NULL, reverses_id {uuid} REFERENCES lots (id), created_at {timestamp} NOT NULL ){strict}; CREATE TABLE cash_ledger ( id {uuid} PRIMARY KEY, account_id {uuid} NOT NULL REFERENCES accounts (id), currency TEXT NOT NULL, amount {money} NOT NULL, kind TEXT NOT NULL, fill_id {uuid} REFERENCES fills (id), reverses_id {uuid} REFERENCES cash_ledger (id), note TEXT, occurred_at {timestamp} NOT NULL, created_at {timestamp} NOT NULL ){strict}; CREATE TABLE positions ( id {uuid} PRIMARY KEY, account_id {uuid} NOT NULL REFERENCES accounts (id), instrument_id {uuid} NOT NULL REFERENCES instruments (id), strategy TEXT NOT NULL, quantity {money} NOT NULL, average_price {money} NOT NULL, last_price {money}, stop_price {money}, opened_at {timestamp} NOT NULL, created_at {timestamp} NOT NULL, updated_at {timestamp} NOT NULL, UNIQUE (account_id, instrument_id, strategy) ){strict}; -- Equity over time: account_id and strategy both empty for the total, one of them set for its own series. CREATE TABLE equity_snapshots ( id {uuid} PRIMARY KEY, taken_at {timestamp} NOT NULL, scope TEXT NOT NULL, account_id {uuid} REFERENCES accounts (id), strategy TEXT, equity {money} NOT NULL, cash {money}, invested {money}, benchmark {money}, drawdown_pct {money}, created_at {timestamp} NOT NULL ){strict}; CREATE INDEX equity_snapshots_scope ON equity_snapshots (scope, taken_at); CREATE TABLE risk_events ( id {uuid} PRIMARY KEY, occurred_at {timestamp} NOT NULL, kind TEXT NOT NULL, severity TEXT NOT NULL, message TEXT NOT NULL, data {json}, created_at {timestamp} NOT NULL ){strict};