2022-04-14 08:07:59 +02:00
|
|
|
CREATE EXTENSION "uuid-ossp";
|
|
|
|
|
|
|
|
CREATE TYPE "Role" AS ENUM (
|
2022-04-15 17:04:23 +02:00
|
|
|
'admin',
|
|
|
|
'user'
|
2022-04-14 08:07:59 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TYPE "OrderStatus" AS ENUM (
|
2022-04-15 17:04:23 +02:00
|
|
|
'confirmed',
|
|
|
|
'cancelled',
|
|
|
|
'delivered',
|
|
|
|
'payed',
|
|
|
|
'require_refund',
|
|
|
|
'refunded'
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TYPE "QuantityUnit" AS ENUM (
|
|
|
|
'g',
|
|
|
|
'dkg',
|
|
|
|
'kg',
|
|
|
|
'piece'
|
2022-04-14 08:07:59 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE accounts
|
|
|
|
(
|
|
|
|
id serial not null primary key,
|
|
|
|
email varchar not null unique,
|
|
|
|
login varchar not null unique,
|
|
|
|
pass_hash varchar not null,
|
2022-04-15 17:04:23 +02:00
|
|
|
role "Role" not null default 'user'
|
2022-04-14 08:07:59 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE products
|
|
|
|
(
|
|
|
|
id serial not null primary key,
|
|
|
|
name varchar not null unique,
|
|
|
|
short_description varchar not null,
|
|
|
|
long_description varchar not null,
|
|
|
|
category varchar,
|
|
|
|
price_major int not null,
|
|
|
|
price_minor int not null,
|
|
|
|
CONSTRAINT positive_price_minor check ( price_major >= 0 )
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE stocks
|
|
|
|
(
|
2022-04-15 17:04:23 +02:00
|
|
|
id serial not null primary key,
|
|
|
|
product_id int references products (id) not null unique,
|
|
|
|
quantity int not null default 0,
|
|
|
|
quantity_unit "QuantityUnit" not null,
|
2022-04-14 08:07:59 +02:00
|
|
|
CONSTRAINT positive_quantity check ( quantity >= 0 )
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE account_orders
|
|
|
|
(
|
|
|
|
id serial not null primary key,
|
|
|
|
buyer_id int references accounts (id) not null,
|
2022-04-15 17:04:23 +02:00
|
|
|
status "OrderStatus" not null default 'confirmed'
|
2022-04-14 08:07:59 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE order_items
|
|
|
|
(
|
2022-04-15 17:04:23 +02:00
|
|
|
id serial not null primary key,
|
|
|
|
product_id int references products (id) not null,
|
|
|
|
order_id int references account_orders (id),
|
|
|
|
quantity int not null default 0,
|
|
|
|
quantity_unit "QuantityUnit" not null,
|
2022-04-14 08:07:59 +02:00
|
|
|
CONSTRAINT positive_quantity check ( quantity >= 0 )
|
|
|
|
);
|
|
|
|
|
2022-04-16 07:31:19 +02:00
|
|
|
CREATE TABLE "statistics"
|
2022-04-14 08:07:59 +02:00
|
|
|
(
|
2022-04-14 21:40:26 +02:00
|
|
|
id serial not null primary key,
|
|
|
|
url varchar not null,
|
|
|
|
clicks int not null default 0,
|
|
|
|
date DATE not null default now(),
|
2022-04-14 08:07:59 +02:00
|
|
|
CONSTRAINT positive_clicks check ( clicks >= 0 )
|
|
|
|
);
|