From 8ad8cc506c2088e804f470fc6112528700a84d67 Mon Sep 17 00:00:00 2001 From: ai-bot Date: Sat, 14 Nov 2026 12:03:15 +0000 Subject: [PATCH] Add schema.sql with the DDL from docs/spec.md section 4 --- schema.sql | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 schema.sql diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..8202be8 --- /dev/null +++ b/schema.sql @@ -0,0 +1,119 @@ +CREATE TABLE IF NOT EXISTS schema_version ( + version INTEGER NOT NULL +); + +CREATE TABLE IF NOT EXISTS settings ( + key TEXT PRIMARY KEY, + value TEXT NOT NULL +); + +CREATE TABLE IF NOT EXISTS admin ( + id INTEGER PRIMARY KEY CHECK (id = 1), + username TEXT NOT NULL, + password_hash TEXT NOT NULL, + salt TEXT NOT NULL, + iterations INTEGER NOT NULL, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL +); + +CREATE TABLE IF NOT EXISTS sessions ( + token_hash TEXT PRIMARY KEY, + created_at TEXT NOT NULL, + expires_at TEXT NOT NULL +); + +-- An account the owner holds coins on: an exchange, a wallet, or a non-taxable +-- account belonging to someone else. +CREATE TABLE IF NOT EXISTS markets ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL UNIQUE, + kind TEXT NOT NULL CHECK (kind IN ('exchange', 'wallet', 'other')), + taxable INTEGER NOT NULL DEFAULT 1 CHECK (taxable IN (0, 1)), + custody_market_id INTEGER REFERENCES markets(id), + note TEXT NOT NULL DEFAULT '', + sort_order INTEGER NOT NULL DEFAULT 0, + archived INTEGER NOT NULL DEFAULT 0 CHECK (archived IN (0, 1)) +); + +-- NOK per 1 BTC at a year-end date, in ore. date is the 1 January of the year the +-- rate belongs to, e.g. '2026-01-01' is the rate for tax year 2025. +CREATE TABLE IF NOT EXISTS prices ( + asset TEXT NOT NULL DEFAULT 'BTC', + date TEXT NOT NULL, + nok_per_btc_ore INTEGER NOT NULL, + source TEXT NOT NULL DEFAULT '', + PRIMARY KEY (asset, date) +); + +CREATE TABLE IF NOT EXISTS transactions ( + id INTEGER PRIMARY KEY, + date TEXT NOT NULL, + type TEXT NOT NULL CHECK (type IN ( + 'trade', 'income', 'spend', 'loss', + 'transfer_out', 'transfer_in', + 'gift_received', 'gift_given', 'admin_cost')), + market_id INTEGER NOT NULL REFERENCES markets(id), + taxable INTEGER NOT NULL DEFAULT 1 CHECK (taxable IN (0, 1)), + note TEXT NOT NULL DEFAULT '', + -- leg_in = what the account receives, leg_out = what the account gives up. + -- Exactly one of the two columns of a leg is non-null; a leg may be absent. + leg_in_sats INTEGER, + leg_in_ore INTEGER, + leg_out_sats INTEGER, + leg_out_ore INTEGER, + -- NOK value of the BTC leg where no leg carries it: income, spend proceeds, + -- gift received, and (for documentation only) a loss. + value_ore INTEGER, + fee_sats INTEGER NOT NULL DEFAULT 0, + fee_ore INTEGER NOT NULL DEFAULT 0, + to_market_id INTEGER REFERENCES markets(id), + transfer_group TEXT, + generated INTEGER NOT NULL DEFAULT 0 CHECK (generated IN (0, 1)), + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + CHECK (leg_in_sats IS NULL OR leg_in_ore IS NULL), + CHECK (leg_out_sats IS NULL OR leg_out_ore IS NULL), + CHECK (leg_in_sats IS NULL OR leg_in_sats > 0), + CHECK (leg_out_sats IS NULL OR leg_out_sats > 0) +); + +-- A lot is a quantity of sats with one acquisition date and one cost basis. +CREATE TABLE IF NOT EXISTS lots ( + id INTEGER PRIMARY KEY, + market_id INTEGER NOT NULL REFERENCES markets(id), + acquired_date TEXT NOT NULL, + origin_tx_id INTEGER REFERENCES transactions(id), + source_lot_id INTEGER REFERENCES lots(id), + origin_seq INTEGER NOT NULL DEFAULT 0, + sats_original INTEGER NOT NULL, + sats_remaining INTEGER NOT NULL, + cost_ore_original INTEGER NOT NULL, + cost_ore_remaining INTEGER NOT NULL, + note TEXT NOT NULL DEFAULT '' +); + +-- Which lots an outgoing transaction consumed. auto = 0 means the owner picked it. +CREATE TABLE IF NOT EXISTS lot_consumptions ( + id INTEGER PRIMARY KEY, + tx_id INTEGER NOT NULL REFERENCES transactions(id) ON DELETE CASCADE, + lot_id INTEGER NOT NULL REFERENCES lots(id), + sats INTEGER NOT NULL, + cost_ore INTEGER NOT NULL, + auto INTEGER NOT NULL DEFAULT 1 CHECK (auto IN (0, 1)) +); + +-- A hand-picked lot selection, stored by origin so it survives a ledger rebuild. +CREATE TABLE IF NOT EXISTS lot_picks ( + id INTEGER PRIMARY KEY, + tx_id INTEGER NOT NULL REFERENCES transactions(id) ON DELETE CASCADE, + origin_tx_id INTEGER NOT NULL, + origin_seq INTEGER NOT NULL DEFAULT 0, + sats INTEGER NOT NULL +); + +CREATE INDEX IF NOT EXISTS idx_tx_date ON transactions(date); +CREATE INDEX IF NOT EXISTS idx_tx_market ON transactions(market_id, date); +CREATE INDEX IF NOT EXISTS idx_lot_market ON lots(market_id, acquired_date); +CREATE INDEX IF NOT EXISTS idx_cons_tx ON lot_consumptions(tx_id); +CREATE INDEX IF NOT EXISTS idx_cons_lot ON lot_consumptions(lot_id);