summaryrefslogtreecommitdiff
path: root/rust/core/migrations/2024-08-31-084439_initial_setup/up.sql
blob: 9038b54c5a7dd505e8a0f702ac9963e2e25fea84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
PRAGMA foreign_keys = ON;

CREATE TABLE accounts(
    id INTEGER NOT NULL PRIMARY KEY,
    name TEXT NOT NULL,
    opening_balance INTEGER NOT NULL,
    opening_date TEXT NOT NULL
);

CREATE TABLE budget_updates(
    id INTEGER NOT NULL PRIMARY KEY,
    category_id INTEGER NOT NULL,
    date TEXT NOT NULL,
    new_budget INTEGER NOT NULL,
    new_period INTEGER NOT NULL,
    UNIQUE (category_id, date),
    FOREIGN KEY (category_id)
        REFERENCES categories (id)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
);

CREATE TABLE categories(
    id INTEGER NOT NULL PRIMARY KEY,
    name TEXT NOT NULL
);

CREATE TABLE category_transfers(
    id INTEGER NOT NULL PRIMARY KEY,
    description TEXT NOT NULL,
    quantity INTEGER NOT NULL,
    from_category_id INTEGER NOT NULL,
    to_category_id INTEGER NOT NULL,
    FOREIGN KEY (from_category_id)
        REFERENCES categories (id)
        ON UPDATE CASCADE
        ON DELETE RESTRICT,
    FOREIGN KEY (to_category_id)
        REFERENCES categories (id)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
);

CREATE TABLE transactions(
    id INTEGER NOT NULL PRIMARY KEY,
    description TEXT NOT NULL,
    payee TEXT NOT NULL,
    quantity INTEGER NOT NULL,
    date TEXT NOT NULL,
    account_id INTEGER NOT NULL,
    FOREIGN KEY (account_id)
        REFERENCES accounts (id)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
);

CREATE TABLE transaction_categorisations(
    id INTEGER NOT NULL PRIMARY KEY,
    description TEXT NOT NULL,
    quantity INTEGER NOT NULL,
    transaction_id INTEGER NOT NULL,
    category_id INTEGER NOT NULL,
    FOREIGN KEY (transaction_id)
        REFERENCES transactions (id)
        ON UPDATE CASCADE
        ON DELETE RESTRICT,
    FOREIGN KEY (category_id)
        REFERENCES categories (id)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
);