openvibers/openvibe.finance migrations0004_goals_debt.sql
0004_goals_debt.sql
| 1 | -- phase: expand |
| 2 | -- Goals and debt (plan §L.2: "goals and debt"). |
| 3 | -- * A goal: an amount to reach, by a date or not; its progress is an account's balance (a savings account set aside |
| 4 | -- for it) or an amount the person keeps up to date by hand. |
| 5 | -- * Debt: a loan or a card account may say its yearly interest rate (basis points: 2499 is 24.99%) and the payment |
| 6 | -- made each month; the page works out when it is paid off and what the interest costs. |
| 7 | CREATE TABLE finance_goals ( |
| 8 | id text COLLATE "C" PRIMARY KEY, |
| 9 | owner text COLLATE "C" NOT NULL, |
| 10 | name text NOT NULL, |
| 11 | target_cents bigint NOT NULL CHECK (target_cents > 0), |
| 12 | target_on date, |
| 13 | account_id text COLLATE "C" REFERENCES finance_accounts (id) ON DELETE SET NULL, |
| 14 | saved_cents bigint NOT NULL DEFAULT 0, |
| 15 | created_at timestamptz NOT NULL DEFAULT now(), |
| 16 | updated_at timestamptz NOT NULL DEFAULT now() |
| 17 | ); |
| 18 | CREATE INDEX finance_goals_owner ON finance_goals (owner, created_at); |
| 19 | |
| 20 | ALTER TABLE finance_accounts ADD COLUMN IF NOT EXISTS apr_bps integer CHECK (apr_bps IS NULL OR (apr_bps >= 0 AND apr_bps <= 10000)); |
| 21 | ALTER TABLE finance_accounts ADD COLUMN IF NOT EXISTS payment_cents bigint CHECK (payment_cents IS NULL OR payment_cents > 0); |
| 22 |