-
Notifications
You must be signed in to change notification settings - Fork 923
/
Copy pathinit.sql
69 lines (60 loc) · 1.69 KB
/
init.sql
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
CREATE UNLOGGED TABLE customers (
id SERIAL PRIMARY KEY,
"limit" INTEGER NOT NULL,
balance INTEGER NOT NULL DEFAULT 0
);
CREATE UNLOGGED TABLE transactions (
id SERIAL PRIMARY KEY,
customer_id INTEGER NOT NULL,
"value" INTEGER NOT NULL,
"type" CHAR(1) NOT NULL,
"description" VARCHAR(10) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
CONSTRAINT fk_transactions_customer_id
FOREIGN KEY (customer_id) REFERENCES customers(id)
);
CREATE INDEX ix_transactions_customer_id_created_at ON transactions (customer_id, created_at DESC);
CREATE FUNCTION debit(customer_id INTEGER, trx_value INTEGER, trx_description TEXT)
RETURNS SETOF INTEGER
LANGUAGE plpgsql
AS $BODY$
DECLARE customer_balance INTEGER;
DECLARE customer_limit INTEGER;
BEGIN
SELECT
balance - trx_value,
"limit"
INTO customer_balance, customer_limit
FROM customers
WHERE id = customer_id
FOR UPDATE;
IF customer_balance < (-customer_limit) THEN RETURN; END IF;
INSERT INTO transactions (customer_id, "value", "type", "description")
VALUES (customer_id, trx_value, 'd', trx_description);
RETURN QUERY
UPDATE customers
SET balance = customer_balance
WHERE id = customer_id
RETURNING balance;
END;
$BODY$;
CREATE FUNCTION credit(customer_id INTEGER, trx_value INTEGER, trx_description TEXT)
RETURNS SETOF INTEGER
LANGUAGE plpgsql
AS $BODY$
BEGIN
INSERT INTO transactions (customer_id, "value", "type", "description")
VALUES (customer_id, trx_value, 'c', trx_description);
RETURN QUERY
UPDATE customers
SET balance = balance + trx_value
WHERE id = customer_id
RETURNING balance;
END;
$BODY$;
INSERT INTO customers (id, "limit") values
(1, 1000 * 100),
(2, 800 * 100),
(3, 10000 * 100),
(4, 100000 * 100),
(5, 5000 * 100);