-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
projects/inventory: track regional inventory
- Loading branch information
Showing
6 changed files
with
280 additions
and
1 deletion.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
projects/estore/inventory/migrations/20240307113646_add_items.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- Create "inventory_items" table | ||
CREATE TABLE `inventory_items` ( | ||
`inventory_id` int NOT NULL AUTO_INCREMENT, | ||
`product_id` int NULL, | ||
`warehouse_id` int NULL, | ||
`quantity` int NULL DEFAULT 0, | ||
PRIMARY KEY (`inventory_id`), | ||
INDEX `product_id` (`product_id`), | ||
INDEX `warehouse_id` (`warehouse_id`), | ||
CONSTRAINT `inventory_items_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`product_id`) ON UPDATE NO ACTION ON DELETE NO ACTION, | ||
CONSTRAINT `inventory_items_ibfk_2` FOREIGN KEY (`warehouse_id`) REFERENCES `warehouses` (`warehouse_id`) ON UPDATE NO ACTION ON DELETE NO ACTION | ||
) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; |
25 changes: 25 additions & 0 deletions
25
projects/estore/inventory/migrations/20240307114027_add_orders_shipments.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
-- Create "orders" table | ||
CREATE TABLE `orders` ( | ||
`order_id` int NOT NULL AUTO_INCREMENT, | ||
`product_id` int NULL, | ||
`quantity` int NOT NULL, | ||
`order_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP, | ||
`customer_info` text NULL, | ||
`status` varchar(100) NULL, | ||
PRIMARY KEY (`order_id`), | ||
INDEX `product_id` (`product_id`), | ||
CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`product_id`) ON UPDATE NO ACTION ON DELETE NO ACTION | ||
) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; | ||
-- Create "shipments" table | ||
CREATE TABLE `shipments` ( | ||
`shipment_id` int NOT NULL AUTO_INCREMENT, | ||
`product_id` int NULL, | ||
`warehouse_id` int NULL, | ||
`quantity` int NOT NULL, | ||
`shipment_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP, | ||
PRIMARY KEY (`shipment_id`), | ||
INDEX `product_id` (`product_id`), | ||
INDEX `warehouse_id` (`warehouse_id`), | ||
CONSTRAINT `shipments_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`product_id`) ON UPDATE NO ACTION ON DELETE NO ACTION, | ||
CONSTRAINT `shipments_ibfk_2` FOREIGN KEY (`warehouse_id`) REFERENCES `warehouses` (`warehouse_id`) ON UPDATE NO ACTION ON DELETE NO ACTION | ||
) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; |
10 changes: 10 additions & 0 deletions
10
projects/estore/inventory/migrations/20240307114116_region_inventory.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-- Create "inventory_by_region" table | ||
CREATE TABLE `inventory_by_region` ( | ||
`region_id` int NOT NULL, | ||
`product_id` int NOT NULL, | ||
`total_quantity` int NULL, | ||
PRIMARY KEY (`region_id`, `product_id`), | ||
INDEX `product_id` (`product_id`), | ||
CONSTRAINT `inventory_by_region_ibfk_1` FOREIGN KEY (`region_id`) REFERENCES `regions` (`region_id`) ON UPDATE NO ACTION ON DELETE NO ACTION, | ||
CONSTRAINT `inventory_by_region_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `products` (`product_id`) ON UPDATE NO ACTION ON DELETE NO ACTION | ||
) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; |
26 changes: 26 additions & 0 deletions
26
projects/estore/inventory/migrations/20240307114304_inventory_tracking_trigger.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
-- Create trigger "after_order_insert" | ||
Check failure on line 1 in projects/estore/inventory/migrations/20240307114304_inventory_tracking_trigger.sql GitHub Actions / setup
|
||
CREATE TRIGGER `after_order_insert` AFTER INSERT ON `orders` FOR EACH ROW BEGIN | ||
DECLARE regionId INT; | ||
|
||
-- Assuming each product is linked to a single warehouse, which is a simplification. | ||
-- This might need a more complex logic based on how your warehouses and orders are structured. | ||
SELECT w.region_id INTO regionId | ||
FROM warehouses w | ||
JOIN inventory_items ii ON w.warehouse_id = ii.warehouse_id | ||
WHERE ii.product_id = NEW.product_id | ||
LIMIT 1; -- This is a simplification and might need adjustment. | ||
|
||
UPDATE inventory_by_region | ||
SET total_quantity = total_quantity - NEW.quantity | ||
WHERE region_id = regionId AND product_id = NEW.product_id; | ||
END; | ||
-- Create trigger "after_shipment_insert" | ||
CREATE TRIGGER `after_shipment_insert` AFTER INSERT ON `shipments` FOR EACH ROW BEGIN | ||
DECLARE regionId INT; | ||
|
||
SELECT region_id INTO regionId FROM warehouses WHERE warehouse_id = NEW.warehouse_id; | ||
|
||
INSERT INTO inventory_by_region (region_id, product_id, total_quantity) | ||
VALUES (regionId, NEW.product_id, NEW.quantity) | ||
ON DUPLICATE KEY UPDATE total_quantity = total_quantity + NEW.quantity; | ||
END; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
h1:OD80uv7vCyCGr2x6dS4rLVaCkoj7JyydbcT6gxQAShI= | ||
h1:TXS4jLbko59JU5wbXzE89sr5uGQvxCMAm+oMEdtAnmU= | ||
20240307092728.sql h1:BEXRAbglZQlUqXHRy4fvsEVsu7FlSfKcJ/JAkH2XgMY= | ||
20240307104354_add_warehouses.sql h1:heT56DSu6Ka8TJo8K4F92rVxinZmt+7tbMX+y3vaJKo= | ||
20240307113646_add_items.sql h1:T6WAjSdbbQiI3RmiM7amcNZGjsNy6re8QjYkWXKCJXw= | ||
20240307114027_add_orders_shipments.sql h1:j6t5n5vZ8dSHHvd1HcR3ObFC1+e1I1X5Zmt/EsryURc= | ||
20240307114116_region_inventory.sql h1:pCnxwZ1bw8sn4v+G0gyhot1Hs+i4BaPr4EbGUugkS3M= | ||
20240307114304_inventory_tracking_trigger.sql h1:iiEGHpZYWyR5jsW44pLcEt7cU3VfSrFC61Te0aPQLNk= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters