openvibers/openvibe.finance migrations0001_finance.sql
0001_finance.sql
| 1 | -- phase: expand |
| 2 | -- OpenVibe.Finance (plan §L.2: "your money, your rules, your data"): a person's accounts and transactions, entered by |
| 3 | -- hand or imported from a bank's CSV. Read and plan only: no money moves. |
| 4 | -- * Every row belongs to one person (owner: their usr_ subject). Nothing here is public, searched, counted in |
| 5 | -- analytics or used for training. |
| 6 | -- * Payees and notes are stored encrypted with a key for that person alone (server/crypto.js); amounts, dates and |
| 7 | -- categories stay plain so the person's own sums can be computed. |
| 8 | -- * Money is integer cents in the account's currency. |
| 9 | -- * A raw import row never changes: a correction (a category, a payee) is a revision on the transaction, and the |
| 10 | -- category says where it came from (you, a rule, the bank's file). |
| 11 | |
| 12 | CREATE TABLE finance_accounts ( |
| 13 | id text COLLATE "C" PRIMARY KEY, |
| 14 | owner text COLLATE "C" NOT NULL, |
| 15 | name text NOT NULL, |
| 16 | kind text COLLATE "C" NOT NULL CHECK (kind IN ('checking', 'savings', 'credit', 'cash', 'loan', 'investment', 'other')), |
| 17 | currency text COLLATE "C" NOT NULL DEFAULT 'USD' CHECK (currency ~ '^[A-Z]{3}$'), |
| 18 | opening_cents bigint NOT NULL DEFAULT 0, |
| 19 | created_at timestamptz NOT NULL DEFAULT now(), |
| 20 | updated_at timestamptz NOT NULL DEFAULT now(), |
| 21 | archived_at timestamptz |
| 22 | ); |
| 23 | CREATE INDEX finance_accounts_owner ON finance_accounts (owner, created_at); |
| 24 | |
| 25 | CREATE TABLE finance_imports ( |
| 26 | id text COLLATE "C" PRIMARY KEY, |
| 27 | owner text COLLATE "C" NOT NULL, |
| 28 | account_id text COLLATE "C" NOT NULL REFERENCES finance_accounts (id) ON DELETE CASCADE, |
| 29 | format text COLLATE "C" NOT NULL DEFAULT 'csv', |
| 30 | rows_read integer NOT NULL DEFAULT 0, |
| 31 | rows_added integer NOT NULL DEFAULT 0, |
| 32 | rows_skipped integer NOT NULL DEFAULT 0, |
| 33 | created_at timestamptz NOT NULL DEFAULT now() |
| 34 | ); |
| 35 | CREATE INDEX finance_imports_owner ON finance_imports (owner, created_at); |
| 36 | |
| 37 | CREATE TABLE finance_transactions ( |
| 38 | id text COLLATE "C" PRIMARY KEY, |
| 39 | owner text COLLATE "C" NOT NULL, |
| 40 | account_id text COLLATE "C" NOT NULL REFERENCES finance_accounts (id) ON DELETE CASCADE, |
| 41 | posted_on date NOT NULL, |
| 42 | amount_cents bigint NOT NULL, |
| 43 | payee_enc text NOT NULL DEFAULT '', |
| 44 | memo_enc text NOT NULL DEFAULT '', |
| 45 | category text NOT NULL DEFAULT 'Uncategorized', |
| 46 | category_from text COLLATE "C" NOT NULL DEFAULT 'none' CHECK (category_from IN ('none', 'you', 'rule', 'file')), |
| 47 | import_id text COLLATE "C" REFERENCES finance_imports (id) ON DELETE SET NULL, |
| 48 | -- The same bank row imported twice is one transaction: an HMAC (the person's key) of its account, date, amount and |
| 49 | -- payee, and its place among identical rows in the file. |
| 50 | dedupe text COLLATE "C", |
| 51 | revision integer NOT NULL DEFAULT 1, |
| 52 | created_at timestamptz NOT NULL DEFAULT now(), |
| 53 | updated_at timestamptz NOT NULL DEFAULT now() |
| 54 | ); |
| 55 | CREATE INDEX finance_transactions_account ON finance_transactions (account_id, posted_on DESC, id); |
| 56 | CREATE INDEX finance_transactions_owner_month ON finance_transactions (owner, posted_on); |
| 57 | CREATE UNIQUE INDEX finance_transactions_dedupe ON finance_transactions (account_id, dedupe) WHERE dedupe IS NOT NULL; |
| 58 |