diff --git a/app/Migrations/create-schema.sql b/app/Migrations/create-schema.sql index 4078831..20663a8 100644 --- a/app/Migrations/create-schema.sql +++ b/app/Migrations/create-schema.sql @@ -4,50 +4,41 @@ -- Our known customers, may need more fields CREATE TABLE IF NOT EXISTS customers ( - customer_id int PRIMARY KEY, - customer_name varchar(100), - customer_address varchar(255) + customer_id INT PRIMARY KEY, + customer_name VARCHAR(50) NOT NULL, + customer_address VARCHAR(50) NOT NULL, ); -- Orders from customers. --- Keep in mind that the delivery_date may be NULL CREATE TABLE IF NOT EXISTS orders ( - order_id int PRIMARY KEY, - customer_id int, - order_date date DEFAULT NOW, - delivery_date date, -- Set when the order hits the truck - FOREIGN KEY (customer_id) REFERENCES customers(customer_id) + order_id INT 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 customer(customer_id), + FOREIGN KEY (cookie_id) REFERENCES cookie(cookie_id) ); -------------------------------------------- -- Recipe/Cookie related tables -------------------------------------------- --- Recipes for all the cookies (essentially a list of cookies) -CREATE TABLE IF NOT EXISTS recipes ( - recipe_id int PRIMARY KEY, - recipe_name varchar(100) UNIQUE -- Cookie name -); - --- "The company has a raw materials warehouse in which --- all ingredients used in their production are stored." - -- Describes ingredients and stock. --- Each ingredient has 'amount' of 'unit' in stock -CREATE TABLE IF NOT EXISTS ingredients ( - ingredient_id int PRIMARY KEY, - ingredient_name varchar(100), - amount int, - unit varchar(50) +CREATE TABLE IF NOT EXISTS raw_materials ( + ingredient_id INT PRIMARY KEY, + ingredient_name VARCHAR(50) NOT NULL UNIQUE, + ingredient_quantity INT NOT NULL, + unit VARCHAR(50) NOT NULL CHECK (unit IN ('g', 'ml')) ); --- Describes what ingredients goes into what recipe --- Each recipe requires 'amount' of 'ingredient' -CREATE TABLE IF NOT EXISTS recipe_contents ( - recipe_id int, - ingredient_id int, - amount int, - PRIMARY KEY (recipe_id, ingredient_id) +-- When did we get the ingredients? +CREATE TABLE IF NOT EXISTS raw_materials_deliveries ( + delivery_id INT PRIMARY KEY, + ingredient_id INT NOT NULL, + delivery_date DATE NOT NULL, + delivery_quantity INT NOT NULL, + unit VARCHAR(50) NOT NULL CHECK (unit IN ('g', 'ml')), + FOREIGN KEY (ingredient_id) REFERENCES raw_materials(ingredient_id) ); -------------------------------------------- @@ -56,28 +47,20 @@ CREATE TABLE IF NOT EXISTS recipe_contents ( -- Pallets are used to store cookies for delivery CREATE TABLE IF NOT EXISTS pallets ( - pallet_id int PRIMARY KEY, - recipe_id int, - order_id int, - FOREIGN KEY (recipe_id) REFERENCES recipes(recipe_id), - FOREIGN KEY (order_id) REFERENCES Orders(order_id) -); - --- What does the pallet contain? -CREATE TABLE IF NOT EXISTS pallet_contents ( - pallet_id int, - ingredient_id int, - amount int, - PRIMARY KEY (pallet_id, ingredient_id), - FOREIGN KEY (pallet_id) REFERENCES pallets(pallet_id), - FOREIGN KEY (ingredient_id) REFERENCES ingredients(ingredient_id) + pallet_id INT PRIMARY KEY, + cookie_id INT NOT NULL, + order_id INT NOT NULL, + status VARCHAR(50) NOT NULL CHECK (status IN ('freezer', 'delivered', 'blocked')), + production_date DATE NOT NULL, + FOREIGN KEY (cookie_id) REFERENCES cookie(cookie_id) + FOREIGN KEY (order_id) REFERENCES orders(order_id) ); -- Has an order been delivered? --- When the truck is loaded, a delivery is considered done -CREATE TABLE IF NOT EXISTS delivery_bill ( - delivery_id int PRIMARY KEY, - order_id int, - delivery_date date DEFAULT NOW, - FOREIGN KEY (order_id) REFERENCES Orders(order_id) +CREATE TABLE IF NOT EXISTS deliveries ( + delivery_id INT PRIMARY KEY, + pallet_id INT NOT NULL, + customer_name VARCHAR(50) NOT NULL, + delivery_date DATE NOT NULL DEFAULT CURRENT_DATE CHECK (delivery_date >= CURRENT_DATE), + FOREIGN KEY (pallet_id) REFERENCES pallets(pallet_id) ); \ No newline at end of file