aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/sql/1_create-database.up.sql
blob: d4566fc626baa88888b763b80f6ecf826be95325 (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
72
73
74
75
76
77
78
79
CREATE TABLE product_types(
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL
);

CREATE TABLE products(
    id INTEGER PRIMARY KEY,
    type_id INTEGER NOT NULL REFERENCES product_types(id),
    height INTEGER NOT NULL,
    width INTEGER NOT NULL,
    depth INTEGER NOT NULL,
    max_storage_duration INTERVAL NULL
);

CREATE TYPE PARTNER_TYPE AS ENUM('SUPPLIER', 'ISSUER');

CREATE TABLE partners(
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    tax_number TEXT NOT NULL,
    type PARTNER_TYPE NOT NULL,
    address TEXT NOT NULL
);

CREATE TABLE partner_contacts(
    id INTEGER PRIMARY KEY,
    partner_id INTEGER NOT NULL REFERENCES partners(id),
    surname TEXT NOT NULL,
    name TEXT NOT NULL,
    position TEXT NOT NULL,
    phone TEXT NOT NULL,
    email TEXT NOT NULL
);

CREATE TABLE storage_slots(
    id INTEGER PRIMARY KEY,
    location TEXT NOT NULL,
    height INTEGER NOT NULL,
    width INTEGER NOT NULL,
    depth INTEGER NOT NULL
);

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

CREATE TABLE product_slots(
    id INTEGER PRIMARY KEY,
    product_id INTEGER NOT NULL REFERENCES products(id),
    slot_id INTEGER NOT NULL REFERENCES storage_slots(id),
    amount INTEGER NOT NULL CHECK (amount > 0),
    status_id INTEGER NOT NULL REFERENCES product_storage_statues(id),
    placement_time TIMESTAMP NOT NULL
);

CREATE TYPE OPERATION_TYPE AS ENUM('SUPPLY', 'ISSUE');

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

CREATE TABLE operations(
    id INTEGER PRIMARY KEY,
    partner_id INTEGER NOT NULL REFERENCES partners(id),
    responsible_id INTEGER NOT NULL REFERENCES partner_contacts(id),
    type OPERATION_TYPE NOT NULL,
    status_id INTEGER NOT NULL REFERENCES operation_statues(id),
    date_created TIMESTAMP NOT NULL,
    date_finished TIMESTAMP NULL
);

CREATE TABLE operation_products(
    id INTEGER PRIMARY KEY,
    operation_id INTEGER NOT NULL REFERENCES operations(id),
    product_id INTEGER NOT NULL REFERENCES products(id),
    amount INTEGER NOT NULL CHECK (amount > 0)
);