stock-bot/database/postgres/init/13-strategy-recovery.sql

23 lines
1.5 KiB
SQL

-- Append-only market/execution inputs, bounded strategy checkpoints, transactional order outbox.
-- Legacy event fingerprints have no payload and cannot be fabricated into replayable history.
CREATE TABLE IF NOT EXISTS simulation_market_inputs (
account_id TEXT NOT NULL REFERENCES simulation_accounts(id), id TEXT NOT NULL,
sequence BIGINT NOT NULL, status TEXT NOT NULL CHECK(status IN ('pending','applied')),
payload JSONB NOT NULL, PRIMARY KEY(account_id,id), UNIQUE(account_id,sequence)
);
CREATE INDEX IF NOT EXISTS simulation_pending_inputs ON simulation_market_inputs(account_id,status,sequence);
CREATE TABLE IF NOT EXISTS simulation_strategy_journal (
account_id TEXT NOT NULL REFERENCES simulation_accounts(id), sequence BIGINT NOT NULL,
payload JSONB NOT NULL, PRIMARY KEY(account_id,sequence)
);
CREATE TABLE IF NOT EXISTS simulation_strategy_checkpoints (
account_id TEXT NOT NULL REFERENCES simulation_accounts(id), strategy_id TEXT NOT NULL,
payload JSONB NOT NULL, PRIMARY KEY(account_id,strategy_id)
);
CREATE TABLE IF NOT EXISTS simulation_strategy_intents (
account_id TEXT NOT NULL, client_order_id TEXT NOT NULL, strategy_id TEXT NOT NULL,
sequence BIGINT NOT NULL, status TEXT NOT NULL CHECK(status IN ('pending','dispatched')),
payload JSONB NOT NULL, PRIMARY KEY(account_id,client_order_id),
FOREIGN KEY(account_id,strategy_id) REFERENCES simulation_strategy_checkpoints(account_id,strategy_id)
);
CREATE INDEX IF NOT EXISTS simulation_pending_intents ON simulation_strategy_intents(account_id,status,sequence);