bazzar/crates/stock_manager/migrations/202204131841_init.sql

62 lines
2.3 KiB
MySQL
Raw Normal View History

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TYPE "QuantityUnit" AS ENUM (
'g',
'dkg',
'kg',
'piece'
2022-11-29 15:18:31 +01:00
);
2022-11-29 15:18:31 +01:00
CREATE TABLE photos
(
id serial NOT NULL PRIMARY KEY,
local_path character varying NOT NULL,
file_name character varying NOT NULL,
unique_name text DEFAULT (gen_random_uuid())::text NOT NULL
);
2022-11-29 15:18:31 +01:00
CREATE TABLE products
(
id serial NOT NULL PRIMARY KEY,
"name" character varying NOT NULL,
category character varying,
deliver_days_flag integer DEFAULT 127 NOT NULL
);
2022-11-29 15:18:31 +01:00
CREATE TABLE product_variants
(
id serial NOT NULL PRIMARY KEY,
product_id integer REFERENCES products (id) ON DELETE CASCADE NOT NULL,
"name" character varying NOT NULL,
short_description character varying NOT NULL,
long_description character varying NOT NULL,
price integer NOT NULL,
quantity_unit "QuantityUnit" NOT NULL,
CONSTRAINT non_negative CHECK ((price >= 0))
);
2022-11-29 15:18:31 +01:00
CREATE TABLE stocks
(
id serial NOT NULL PRIMARY KEY,
product_variant_id integer REFERENCES product_variants (id) ON DELETE CASCADE NOT NULL,
quantity integer DEFAULT 0 NOT NULL,
quantity_unit "QuantityUnit" NOT NULL,
CONSTRAINT positive_quantity CHECK ((quantity >= 0))
);
2022-11-29 15:18:31 +01:00
CREATE TABLE product_photos
(
id serial NOT NULL PRIMARY KEY,
product_variant_id integer REFERENCES product_variants (id) ON DELETE CASCADE NOT NULL,
photo_id integer REFERENCES photos (id) NOT NULL
);
2022-11-28 17:00:19 +01:00
2022-11-29 15:18:31 +01:00
CREATE TABLE categories
(
id serial NOT NULL PRIMARY KEY,
2022-11-28 17:00:19 +01:00
parent_id int references categories (id) ON DELETE CASCADE,
2022-11-29 15:18:31 +01:00
"name" varchar not null,
"key" varchar not null,
"svg" varchar not null
2022-11-28 17:00:19 +01:00
);