AkahuSync/sql/database definitions.sql
2026-05-13 19:32:50 +12:00

85 lines
2.8 KiB
SQL

CREATE table IF NOT EXISTS accounts(
id SERIAL PRIMARY KEY,
accountnum VARCHAR UNIQUE NOT NULL,
accountname VARCHAR UNIQUE NOT NULL,
REFERENCES organizations(id) ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS organizations(
id SERIAL PRIMARY KEY,
orgname VARCHAR(50) UNIQUE NOT NULL
)
-- how can I normalize this further? going to end up with lots of nulls in the transactions table, but not sure if it's worth it to have separate tables for each transaction type (e.g. fund transactions, card transactions, etc.)
CREATE TABLE IF NOT EXISTS transactions(
id SERIAL PRIMARY KEY,
datetime DATE NOT NULL,
description VARCHAR NOT NULL,
amount REAL NOT NULL,
accountid INT NOT NULL REFERENCES accounts(id) ON DELETE SET NULL,
orgid INT NOT NULL REFERENCES organizations(id) ON DELETE SET NULL
vendorid INT REFERENCES vendors(id) ON DELETE SET NULL
)
CREATE TABLE IF NOT EXISTS snapshots(
id SERIAL PRIMARY KEY,
datetime DATE NOT NULL,
accountid INT NOT NULL REFERENCES accounts(id) ON DELETE SET NULL,
balance REAL NOT NULL,
orgid INT NOT NULL REFERENCES organizations(id) ON DELETE SET NULL
)
CREATE TABLE IF NOT EXISTS syncs(
id SERIAL PRIMARY KEY,
datetime DATE NOT NULL,
accountid INT NOT NULL REFERENCES accounts(id) ON DELETE SET NULL,
orgid INT NOT NULL REFERENCES organizations(id) ON DELETE SET NULL
)
CREATE TABLE IF NOT EXISTS vendors(
id SERIAL PRIMARY KEY,
vendorname VARCHAR NOT NULL,
orgid INT NOT NULL REFERENCES organizations(id) ON DELETE SET NULL
)
CREATE TABLE IF NOT EXISTS rawtransactions(
id SERIAL PRIMARY KEY,
data JSONB NOT NULL,
received_at TIMESTAMPTZ NOT NULL DEFAULT now(),
source VARCHAR(100),
accountid INT REFERENCES accounts(id) ON DELETE SET NULL,
orgid INT REFERENCES organizations(id) ON DELETE SET NULL,
raw_sha256 CHAR(64) UNIQUE,
processed BOOLEAN NOT NULL DEFAULT FALSE
);
CREATE TABLE IF NOT EXISTS funds(
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
datetime DATE NOT NULL,
amount REAL NOT NULL,
accountid INT NOT NULL REFERENCES accounts(id) ON DELETE SET NULL,
orgid INT NOT NULL REFERENCES organizations(id) ON DELETE SET NULL,
fundid VARCHAR(100) NOT NULL,
value REAL NOT NULL,
shares REAL NOT NULL,
currencyid INT REFERENCES currency(id) ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS currency(
id SERIAL PRIMARY KEY,
currencycode CHAR(3) UNIQUE NOT NULL,
currencyname VARCHAR(50) NOT NULL
);
--assets, liabilities, equity, income, expenses
CREATE TABLE IF NOT EXISTS accounttypes(
id SERIAL PRIMARY KEY,
typename VARCHAR(100) NOT NULL,
includeinnetworth BOOLEAN NOT NULL
);
CREATE INDEX IF NOT EXISTS rawtransactions_data_idx ON rawtransactions USING GIN (data);
CREATE INDEX IF NOT EXISTS rawtransactions_received_at_idx ON rawtransactions (received_at);
CREATE INDEX IF NOT EXISTS rawtransactions_account_org_idx ON rawtransactions (orgid, accountid);