-
Notifications
You must be signed in to change notification settings - Fork 928
/
db.sql
94 lines (81 loc) · 1.97 KB
/
db.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
SET timezone TO 'America/Sao_Paulo';
CREATE TABLE customers (
id SERIAL PRIMARY KEY,
"limit" INTEGER NOT NULL,
balance INTEGER NOT NULL DEFAULT 0
);
INSERT INTO customers ("limit", balance)
VALUES
(1000 * 100, 0),
(800 * 100, 0),
(10000 * 100, 0),
(100000 * 100, 0),
(5000 * 100, 0);
CREATE UNLOGGED TABLE transactions (
id SERIAL PRIMARY KEY,
customer_id SMALLINT NOT NULL,
amount INTEGER NOT NULL,
type CHAR(1) NOT NULL,
description VARCHAR(10) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
ALTER TABLE
transactions
SET
(autovacuum_enabled = false);
CREATE INDEX idx_transactions ON transactions (customer_id asc);
CREATE OR REPLACE FUNCTION debit(
customer_id_tx SMALLINT,
amount_tx INT,
description_tx VARCHAR(10))
RETURNS TABLE (
new_balance INT,
success BOOL,
current_limit INT)
LANGUAGE plpgsql
AS $$
DECLARE
current_balance int;
current_limit_amount int;
BEGIN
PERFORM pg_advisory_xact_lock(customer_id_tx);
SELECT
"limit",
balance
INTO
current_limit_amount,
current_balance
FROM customers
WHERE id = customer_id_tx;
IF current_balance - amount_tx >= current_limit_amount * -1 THEN
INSERT INTO transactions VALUES(DEFAULT, customer_id_tx, amount_tx, 'd', description_tx);
RETURN QUERY
UPDATE customers
SET balance = balance - amount_tx
WHERE id = customer_id_tx
RETURNING balance, TRUE, "limit";
ELSE
RETURN QUERY SELECT current_balance, FALSE, current_limit_amount;
END IF;
END;
$$;
CREATE OR REPLACE FUNCTION credit(
customer_id_tx SMALLINT,
amount_tx INT,
description_tx VARCHAR(10))
RETURNS TABLE (
new_balance INT,
success BOOL,
current_limit INT)
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM pg_advisory_xact_lock(customer_id_tx);
INSERT INTO transactions VALUES(DEFAULT, customer_id_tx, amount_tx, 'c', description_tx);
RETURN QUERY
UPDATE customers
SET balance = balance + amount_tx
WHERE id = customer_id_tx
RETURNING balance, TRUE, "limit";
END;
$$;