bazzar/migrations/202204131841_init.sql

79 lines
2.1 KiB
MySQL
Raw Normal View History

2022-04-14 08:07:59 +02:00
CREATE EXTENSION "uuid-ossp";
CREATE TYPE "Role" AS ENUM (
'admin',
'user'
2022-04-14 08:07:59 +02:00
);
CREATE TYPE "OrderStatus" AS ENUM (
'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,
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
(
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,
status "OrderStatus" not null default 'confirmed'
2022-04-14 08:07:59 +02:00
);
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,
2022-04-14 08:07:59 +02:00
CONSTRAINT positive_quantity check ( quantity >= 0 )
);
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 )
);