-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
32 lines (25 loc) · 868 Bytes
/
Copy pathschema.sql
File metadata and controls
32 lines (25 loc) · 868 Bytes
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
BEGIN;
DROP TABLE IF EXISTS ledger_transactions CASCADE;
DROP TABLE IF EXISTS accounts CASCADE;
DROP SEQUENCE IF EXISTS payment_seq;
CREATE TABLE accounts (
account_id TEXT PRIMARY KEY,
created_at BIGINT NOT NULL,
balance BIGINT NOT NULL DEFAULT 0
);
CREATE TABLE ledger_transactions (
transaction_id BIGSERIAL PRIMARY KEY,
account_id TEXT NOT NULL REFERENCES accounts(account_id) ON DELETE RESTRICT,
timestamp BIGINT NOT NULL,
operation TEXT NOT NULL,
amount BIGINT NOT NULL,
payment_ref TEXT NULL,
deposited BOOLEAN NULL
);
CREATE SEQUENCE payment_seq START 1;
CREATE INDEX idx_ledger_account_time
ON ledger_transactions(account_id, timestamp);
CREATE INDEX idx_cashback_due
ON ledger_transactions(timestamp)
WHERE operation = 'cashback' AND deposited = FALSE;
COMMIT;