-
Notifications
You must be signed in to change notification settings - Fork 923
/
Copy pathinit.sql
39 lines (35 loc) · 922 Bytes
/
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
create table if not exists clients
(
id integer primary key,
"limit" bigint not null default 0,
balance bigint not null default 0,
constraint clients_balance_check check (balance >= -"limit")
);
create table if not exists transactions
(
id bigserial primary key,
client_id integer not null,
amount bigint not null,
type char not null,
description text,
created_at timestamp not null default current_timestamp
);
create index if not exists transactions_client_id_index on transactions (client_id);
insert into clients (id, "limit", balance)
values (1, 100000, 0),
(2, 80000, 0),
(3, 1000000, 0),
(4, 10000000, 0),
(5, 500000, 0);
CREATE OR REPLACE FUNCTION reset_data()
RETURNS void
LANGUAGE SQL
AS
$fn$
UPDATE clients
SET balance = 0
WHERE 1 = 1;
DELETE
FROM transactions
WHERE 1 = 1;
$fn$;