bazzar/db/migrate/202204131841_init.sql

79 lines
2.1 KiB
SQL

CREATE EXTENSION "uuid-ossp";
CREATE TYPE "Role" AS ENUM (
'admin',
'user'
);
CREATE TYPE "OrderStatus" AS ENUM (
'confirmed',
'cancelled',
'delivered',
'payed',
'require_refund',
'refunded'
);
CREATE TYPE "QuantityUnit" AS ENUM (
'g',
'dkg',
'kg',
'piece'
);
CREATE TABLE accounts
(
id serial not null primary key,
email varchar not null unique,
login varchar not null unique,
pass_hash varchar not null,
role "Role" not null default 'user'
);
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
(
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,
CONSTRAINT positive_quantity check ( quantity >= 0 )
);
CREATE TABLE account_orders
(
id serial not null primary key,
buyer_id int references accounts (id) not null,
status "OrderStatus" not null default 'confirmed'
);
CREATE TABLE order_items
(
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,
CONSTRAINT positive_quantity check ( quantity >= 0 )
);
CREATE TABLE statistics
(
id serial not null primary key,
url varchar not null,
clicks int not null default 0,
date DATE not null default now(),
CONSTRAINT positive_clicks check ( clicks >= 0 )
);