forked from jackalnom/centralcoastcauldrons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.sql
179 lines (159 loc) · 4.07 KB
/
schema.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
-- global_inventory table
create table
public.global_inventory (
id bigint generated always as identity primary key,
created_at timestamp with time zone not null default now(),
gold integer null default 100,
num_red_ml integer null default 0,
num_green_ml integer null default 0,
num_blue_ml integer null default 0,
num_dark_ml integer null default 0
);
-- initial game state:
INSERT INTO global_inventory (gold, num_red_ml, num_green_ml, num_blue_ml, num_dark_ml)
VALUES (100, 0, 0, 0, 0);
-- Version 3:
-- cart_items table
create table
public.cart_items (
item_sku text,
quantity integer,
cart_id integer,
constraint cart_items_fkey foreign key (cart_id) references carts(id)
);
-- carts table
create table
public.carts (
id bigint generated always as identity primary key,
created_at timestamp with time zone not null default now(),
customer text
);
-- catalog table
create table public.catalog (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
sku TEXT NOT NULL,
name TEXT NOT NULL,
price BIGINT NOT NULL CHECK (price BETWEEN 1 AND 500),
ml_per_barrel BIGINT NOT NULL,
num_red_ml BIGINT NOT NULL,
num_green_ml BIGINT NOT NULL,
num_blue_ml BIGINT NOT NULL,
num_dark_ml BIGINT NOT NULL,
quantity BIGINT NOT NULL CHECK (quantity BETWEEN 0 AND 10000),
CHECK ((num_red_ml + num_green_ml + num_blue_ml + num_dark_ml) = 100)
);
INSERT INTO public.catalog (sku, name, price, num_red_ml, num_green_ml, num_blue_ml, num_dark_ml, ml_per_barrel, quantity)
VALUES
('SKU1', 'Pure Red Potion', 100, 100, 0, 0, 0, 100, 0),
('SKU2', 'Pure Green Potion', 100, 0, 100, 0, 0, 100, 0),
('SKU3', 'Pure Blue Potion', 100, 0, 0, 100, 0, 100, 0),
('SKU5', 'Purple Potion', 25, 50, 0, 50, 0, 100, 0),
('SKU6', 'Cyan Potion', 25, 0, 50, 50, 0, 100, 0),
('SKU8', 'Yellow Potion', 25, 50, 50, 0, 0, 100, 0),
-- deliveries table
create table
public.deliveries (
id bigint generated always as identity primary key,
created_at timestamp with time zone not null default now(),
catalog_id bigint not null,
quantity bigint not null,
foreign key (catalog_id) references catalog(id)
);
-- Version 4:
-- inventory_transactions table
create table
public.inventory_transactions (
id bigint generated always as identity primary key,
created_at timestamp with time zone not null default current_timestamp,
description text
);
-- inventory_ledger_entries table
create table
public.inventory_ledger_entries (
id bigint generated always as identity primary key,
inventory_type varchar(255),
transaction_id integer,
change integer,
foreign key (transaction_id) references inventory_transactions (id)
);
-- Version 5:
-- Dummy customers
WITH inserted AS (
INSERT INTO carts (customer)
VALUES
('Scaramouche')
RETURNING id
)
INSERT INTO cart_items (item_sku, quantity, cart_id)
SELECT
'1 oblivion potion',
50,
id AS cart_id
FROM
inserted;
WITH inserted AS (
INSERT INTO carts (customer)
VALUES
('Cellar')
RETURNING id
)
INSERT INTO cart_items (item_sku, quantity, cart_id)
SELECT
'Pure Green Potion',
70,
id AS cart_id
FROM
inserted;
WITH inserted AS (
INSERT INTO carts (customer)
VALUES
('Barlan')
RETURNING id
)
INSERT INTO cart_items (item_sku, quantity, cart_id)
SELECT
'Cyan Potion',
41,
id AS cart_id
FROM
inserted;
WITH inserted AS (
INSERT INTO carts (customer)
VALUES
('Dellen')
RETURNING id
)
INSERT INTO cart_items (item_sku, quantity, cart_id)
SELECT
'Yellow Potion',
21,
id AS cart_id
FROM
inserted;
WITH inserted AS (
INSERT INTO carts (customer)
VALUES
('Daedor')
RETURNING id
)
INSERT INTO cart_items (item_sku, quantity, cart_id)
SELECT
'Cyan Potion',
166,
id AS cart_id
FROM
inserted;
WITH inserted AS (
INSERT INTO carts (customer)
VALUES
('Snow')
RETURNING id
)
INSERT INTO cart_items (item_sku, quantity, cart_id)
SELECT
'Purple Potion',
250,
id AS cart_id
FROM
inserted;