diff --git a/app/Migrations/create-schema.sql b/app/Migrations/create-schema.sql index 28d1d15..d5561ef 100644 --- a/app/Migrations/create-schema.sql +++ b/app/Migrations/create-schema.sql @@ -9,15 +9,9 @@ DROP TABLE IF EXISTS customers; DROP TABLE IF EXISTS cookies; -------------------------------------------- --- Recipe/Cookie related tables +-- Orders, deliveries and customers -------------------------------------------- --- Holds the different types of cookies we can make. -CREATE TABLE IF NOT EXISTS cookies ( - cookie_id INTEGER PRIMARY KEY, - cookie_name VARCHAR(50) NOT NULL UNIQUE -); - -- Our known customers, may need more fields CREATE TABLE IF NOT EXISTS customers ( customer_id INTEGER PRIMARY KEY, @@ -29,41 +23,35 @@ CREATE TABLE IF NOT EXISTS customers ( CREATE TABLE IF NOT EXISTS orders ( order_id INTEGER PRIMARY KEY, customer_id INT NOT NULL, - cookie_id INT NOT NULL, order_date DATE NOT NULL DEFAULT CURRENT_DATE CHECK (order_date >= CURRENT_DATE), - FOREIGN KEY (customer_id) REFERENCES customers(customer_id), - FOREIGN KEY (cookie_id) REFERENCES cookies(cookie_id) + expected_delivery_date DATE NOT NULL, + FOREIGN KEY (customer_id) REFERENCES customers(customer_id) +); + +CREATE TABLE IF NOT EXISTS order_spec ( + nbr_pallets INTEGER NOT NULL, + order_id INTEGER NOT NULL, + cookie_id INTEGER NOT NULL, + FOREIGN KEY (order_id) REFERENCES orders(order_id), + FOREIGN KEY (cookie_id) REFERENCES cookies(cookie_id), + PRIMARY KEY (order_id, cookie_id) ); -------------------------------------------- --- Raw materials, ingredients and recipes +-- Cookies, raw_materials and recipes -------------------------------------------- -- Notes: the unit type can be defined in terms -- of volume or weight instead. Here we choose -- to use static si-prefixes in relevant tables. --- What types of ingredients do we handle. --- Not currently used, but kept as an example. -CREATE TABLE IF NOT EXISTS ingredients ( - ingredient_id INTEGER PRIMARY KEY, - ingredient_name VARCHAR(50) NOT NULL UNIQUE, - preferred_unit VARCHAR(50) NOT NULL CHECK (preferred_unit IN ('g', 'ml')) +-- Holds the different types of cookies we can make. +CREATE TABLE IF NOT EXISTS cookies ( + cookie_id INTEGER PRIMARY KEY, + cookie_name VARCHAR(50) NOT NULL UNIQUE ); --- What ingredients are in what cookies? --- Glues together the cookies and ingredients, a 'recipe'. -CREATE TABLE IF NOT EXISTS recipe_contents ( - cookie_id INT NOT NULL, - ingredient_id INT NOT NULL, - quantity INT NOT NULL, - unit VARCHAR(50) NOT NULL CHECK (unit IN ('g', 'ml')), - PRIMARY KEY (cookie_id, ingredient_id), - FOREIGN KEY (cookie_id) REFERENCES cookies(cookie_id), - FOREIGN KEY (ingredient_id) REFERENCES ingredients(ingredient_id) -); - --- Describes ingredients and stock. --- This should reference the ingredients table, but we'll keep it simple for now. +-- What types of raw_materials do we handle. +-- raw_materials quantity tells us amount in stock CREATE TABLE IF NOT EXISTS raw_materials ( ingredient_id INTEGER PRIMARY KEY, ingredient_name VARCHAR(50) NOT NULL UNIQUE, @@ -71,7 +59,19 @@ CREATE TABLE IF NOT EXISTS raw_materials ( unit VARCHAR(50) NOT NULL CHECK (unit IN ('g', 'ml')) ); --- When did we get the ingredients? +-- What raw_materials are in what cookies? +-- Glues together the cookies and raw_materials, a 'recipe'. +CREATE TABLE IF NOT EXISTS recipe_contents ( + cookie_id INT NOT NULL, + ingredient_id INT NOT NULL, + quantity INT NOT NULL, + unit VARCHAR(50) NOT NULL CHECK (unit IN ('g', 'ml')), + PRIMARY KEY (cookie_id, ingredient_id), + FOREIGN KEY (cookie_id) REFERENCES cookies(cookie_id), + FOREIGN KEY (ingredient_id) REFERENCES raw_materials(ingredient_id) +); + +-- When did we get the raw_materials? CREATE TABLE IF NOT EXISTS raw_materials_deliveries ( delivery_id INTEGER PRIMARY KEY, ingredient_id INT NOT NULL, @@ -90,11 +90,9 @@ CREATE TABLE IF NOT EXISTS raw_materials_deliveries ( CREATE TABLE IF NOT EXISTS pallets ( pallet_id INTEGER PRIMARY KEY, cookie_id INT NOT NULL, - order_id INT, -- This should be not null status VARCHAR(50) NOT NULL CHECK (status IN ('freezer', 'delivered', 'blocked')), - production_date DATE NOT NULL, + production_date DATE NOT NULL DEFAULT NOW, FOREIGN KEY (cookie_id) REFERENCES cookies(cookie_id) - FOREIGN KEY (order_id) REFERENCES orders(order_id) ); -- Connects pallets to orders @@ -116,7 +114,6 @@ CREATE VIEW IF NOT EXISTS pallets_view AS SELECT pallets.pallet_id, cookie_name, - pallets.order_id, status, production_date, delivery_date