From d46e4660cdcab0d51656f384b75191a7cea07afc Mon Sep 17 00:00:00 2001 From: noamcattan Date: Sun, 24 Mar 2024 15:29:38 +0200 Subject: [PATCH] schema changes --- .../20240324131640_order_comment.sql | 2 + dirs/ecommerce/migrations/atlas.sum | 3 +- dirs/ecommerce/schema.sql | 89 +++++++++++-------- .../inventory/migrations/20240324132903.sql | 7 ++ .../estore/inventory/migrations/atlas.sum | 3 +- projects/estore/inventory/schema.hcl | 21 +++++ 6 files changed, 85 insertions(+), 40 deletions(-) create mode 100644 dirs/ecommerce/migrations/20240324131640_order_comment.sql create mode 100644 projects/estore/inventory/migrations/20240324132903.sql diff --git a/dirs/ecommerce/migrations/20240324131640_order_comment.sql b/dirs/ecommerce/migrations/20240324131640_order_comment.sql new file mode 100644 index 0000000..0e7ffc8 --- /dev/null +++ b/dirs/ecommerce/migrations/20240324131640_order_comment.sql @@ -0,0 +1,2 @@ +-- Modify "orders" table +ALTER TABLE `orders` ADD COLUMN `comment` varchar(100) NULL; diff --git a/dirs/ecommerce/migrations/atlas.sum b/dirs/ecommerce/migrations/atlas.sum index 4e06e34..10f4d26 100644 --- a/dirs/ecommerce/migrations/atlas.sum +++ b/dirs/ecommerce/migrations/atlas.sum @@ -1,4 +1,4 @@ -h1:jNBWmTKllvO8Z2dWvwhVXU+3o8d3Gk565vv/a7TYdFo= +h1:lV09AXD3POxZjRa9FpmsQWjDChRmowo0CyNVOAWHus4= 20230316085611.sql h1:br6W6LPEnnsejlz/7hRm9zthwStCzjN2vZkqVPxlmvo= 20230316090502.sql h1:GfeRjkSeoCt3JVRtLQNa/r50lRfpAPXS7AqTU2ZNFgY= 20230531091333_products_categories.sql h1:59q2M59dV5dJNv4Lyb2TAJz8V6HekgkLn9z4DoL98jA= @@ -11,3 +11,4 @@ h1:jNBWmTKllvO8Z2dWvwhVXU+3o8d3Gk565vv/a7TYdFo= 20230531121807_orders.sql h1:RgIzinZdFrWrVCtrqQec/dJQ75tFQaOe+XLAHV5KzYM= 20230906125835_checkpoint.sql h1:jQaO4R0sJQ0ZOPp4Pp7pR2rQ1Cix9gaEaIt2qDomGWQ= 20240319115808_user_is_admin.sql h1:2DLidS3V0AKs8NqPAIUOlJjOUiUvVYPP0stOFSw6gS8= +20240324131640_order_comment.sql h1:TCduwlpoGZn9Zi6v5t3YzaAkeVgQX5fJ96ZbBJbR0MA= diff --git a/dirs/ecommerce/schema.sql b/dirs/ecommerce/schema.sql index f1d9088..90ef295 100644 --- a/dirs/ecommerce/schema.sql +++ b/dirs/ecommerce/schema.sql @@ -1,23 +1,28 @@ -CREATE TABLE `users` (`id` int NOT NULL, `user_name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, PRIMARY KEY (`id`)) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; - -CREATE TABLE `posts` (`id` int NOT NULL, `user_id` int NOT NULL, `title` varchar(255) NOT NULL, `body` text NOT NULL, PRIMARY KEY (`id`), INDEX `user_id` (`user_id`), CONSTRAINT `posts_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION ON DELETE CASCADE) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; - +-- Create "users" table +CREATE TABLE `users` ( + `id` int NOT NULL, + `user_name` varchar(255) NOT NULL, + `email` varchar(255) NOT NULL, + `is_admin` bool NULL DEFAULT 0, + PRIMARY KEY (`id`) +) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; +-- Create "categories" table CREATE TABLE `categories` ( `id` int NOT NULL, `category_name` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; - +-- Create "products" table CREATE TABLE `products` ( `id` int NOT NULL, - `product_name` varchar(255) NOT NULL UNIQUE, + `product_name` varchar(255) NOT NULL, `price` decimal(10,2) NOT NULL, - `category_id` int, + `category_id` int NULL, PRIMARY KEY (`id`), INDEX `category_id` (`category_id`), - CONSTRAINT `products_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE SET NULL -) CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; - + UNIQUE INDEX `product_name` (`product_name`), + CONSTRAINT `products_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON UPDATE NO ACTION ON DELETE SET NULL +) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; -- Create "product_reviews" table CREATE TABLE `product_reviews` ( `id` int NOT NULL, @@ -28,34 +33,31 @@ CREATE TABLE `product_reviews` ( PRIMARY KEY (`id`), INDEX `product_id` (`product_id`), INDEX `user_id` (`user_id`), - CONSTRAINT `product_reviews_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE, - CONSTRAINT `product_reviews_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE -) CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; - + CONSTRAINT `product_reviews_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON UPDATE NO ACTION ON DELETE CASCADE, + CONSTRAINT `product_reviews_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION ON DELETE CASCADE +) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; -- Create "comments" table CREATE TABLE `comments` ( `id` int NOT NULL, `user_id` int NOT NULL, - `review_id` int, - `parent_comment_id` int, + `review_id` int NULL, + `parent_comment_id` int NULL, `comment_text` text NOT NULL, PRIMARY KEY (`id`), - INDEX `user_id` (`user_id`), - INDEX `review_id` (`review_id`), INDEX `parent_comment_id` (`parent_comment_id`), - CONSTRAINT `comments_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE, - CONSTRAINT `comments_ibfk_2` FOREIGN KEY (`review_id`) REFERENCES `product_reviews` (`id`) ON DELETE CASCADE, - CONSTRAINT `comments_ibfk_3` FOREIGN KEY (`parent_comment_id`) REFERENCES `comments` (`id`) ON DELETE CASCADE -) CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; - + INDEX `review_id` (`review_id`), + INDEX `user_id` (`user_id`), + CONSTRAINT `comments_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION ON DELETE CASCADE, + CONSTRAINT `comments_ibfk_2` FOREIGN KEY (`review_id`) REFERENCES `product_reviews` (`id`) ON UPDATE NO ACTION ON DELETE CASCADE, + CONSTRAINT `comments_ibfk_3` FOREIGN KEY (`parent_comment_id`) REFERENCES `comments` (`id`) ON UPDATE NO ACTION ON DELETE CASCADE +) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; -- Create "fulfillment_centers" table CREATE TABLE `fulfillment_centers` ( `id` int NOT NULL, `name` varchar(255) NOT NULL, `location` varchar(255) NOT NULL, PRIMARY KEY (`id`) -) CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; - +) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; -- Create "inventory" table CREATE TABLE `inventory` ( `id` int NOT NULL, @@ -63,24 +65,25 @@ CREATE TABLE `inventory` ( `fulfillment_center_id` int NOT NULL, `quantity` int NOT NULL, PRIMARY KEY (`id`), - INDEX `product_id` (`product_id`), INDEX `fulfillment_center_id` (`fulfillment_center_id`), - CONSTRAINT `inventory_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE, - CONSTRAINT `inventory_ibfk_2` FOREIGN KEY (`fulfillment_center_id`) REFERENCES `fulfillment_centers` (`id`) ON DELETE CASCADE -) CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; - + INDEX `product_id` (`product_id`), + CONSTRAINT `inventory_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON UPDATE NO ACTION ON DELETE CASCADE, + CONSTRAINT `inventory_ibfk_2` FOREIGN KEY (`fulfillment_center_id`) REFERENCES `fulfillment_centers` (`id`) ON UPDATE NO ACTION ON DELETE CASCADE +) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; +-- Create "orders" table CREATE TABLE `orders` ( `id` int NOT NULL, `user_id` int NOT NULL, `fulfillment_center_id` int NOT NULL, `total_amount` decimal(10,2) NOT NULL, + `comment` varchar(100) NULL, PRIMARY KEY (`id`), - INDEX `user_id` (`user_id`), INDEX `fulfillment_center_id` (`fulfillment_center_id`), - CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE, - CONSTRAINT `orders_ibfk_2` FOREIGN KEY (`fulfillment_center_id`) REFERENCES `fulfillment_centers` (`id`) ON DELETE CASCADE -) CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; - + INDEX `user_id` (`user_id`), + CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION ON DELETE CASCADE, + CONSTRAINT `orders_ibfk_2` FOREIGN KEY (`fulfillment_center_id`) REFERENCES `fulfillment_centers` (`id`) ON UPDATE NO ACTION ON DELETE CASCADE +) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; +-- Create "order_items" table CREATE TABLE `order_items` ( `id` int NOT NULL, `order_id` int NOT NULL, @@ -90,6 +93,16 @@ CREATE TABLE `order_items` ( PRIMARY KEY (`id`), INDEX `order_id` (`order_id`), INDEX `product_id` (`product_id`), - CONSTRAINT `order_items_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE, - CONSTRAINT `order_items_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE -) CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; + CONSTRAINT `order_items_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON UPDATE NO ACTION ON DELETE CASCADE, + CONSTRAINT `order_items_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON UPDATE NO ACTION ON DELETE CASCADE +) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; +-- Create "posts" table +CREATE TABLE `posts` ( + `id` int NOT NULL, + `user_id` int NOT NULL, + `title` varchar(255) NOT NULL, + `body` text NOT NULL, + PRIMARY KEY (`id`), + INDEX `user_id` (`user_id`), + CONSTRAINT `posts_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION ON DELETE CASCADE +) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; diff --git a/projects/estore/inventory/migrations/20240324132903.sql b/projects/estore/inventory/migrations/20240324132903.sql new file mode 100644 index 0000000..ea622d7 --- /dev/null +++ b/projects/estore/inventory/migrations/20240324132903.sql @@ -0,0 +1,7 @@ +-- Modify "products" table +ALTER TABLE `products` ADD COLUMN `price` decimal(10,2) NOT NULL; +-- Create "price_avgs" view +CREATE VIEW `price_avgs` ( + `category_name`, + `avg_price` +) AS select `categories`.`name` AS `category_name`,avg(`products`.`price`) AS `avg_price` from (`categories` join `products` on((`categories`.`category_id` = `products`.`category_id`))) group by `categories`.`name`; diff --git a/projects/estore/inventory/migrations/atlas.sum b/projects/estore/inventory/migrations/atlas.sum index 2b73f46..7d51b8f 100644 --- a/projects/estore/inventory/migrations/atlas.sum +++ b/projects/estore/inventory/migrations/atlas.sum @@ -1,4 +1,5 @@ -h1:cpN5ezSWhHJMZt+LGqMZ/VrNhdnh7eKGE2pQmNS80DQ= +h1:etbjVQ/PE0Uj3NcNPgutwiz2nQIsQUgfLUcYzq8sqAg= 20240307092728.sql h1:BEXRAbglZQlUqXHRy4fvsEVsu7FlSfKcJ/JAkH2XgMY= 20240307104354_add_warehouses.sql h1:heT56DSu6Ka8TJo8K4F92rVxinZmt+7tbMX+y3vaJKo= 20240319121112.sql h1:0eBtqy178hkD2NHK31j5TCqopjYx8it9GiCK70U0nig= +20240324132903.sql h1:pBF+aEreDTVHB02hYA4RGPNHaak9ioUMYtMm698AtDA= diff --git a/projects/estore/inventory/schema.hcl b/projects/estore/inventory/schema.hcl index 26ac6f0..227843d 100644 --- a/projects/estore/inventory/schema.hcl +++ b/projects/estore/inventory/schema.hcl @@ -36,6 +36,11 @@ table "products" { null = true type = int } + column "price" { + null = false + type = decimal(10,2) + unsigned = false + } column "description" { null = true type = text @@ -128,6 +133,22 @@ table "warehouses" { columns = [column.region_id] } } + +view "price_avgs" { + schema = schema.default + column "category_name" { + type = text + } + column "avg_price" { + type = int + } + as = <<-SQL + SELECT categories.name category_name, AVG(products.price) avg_price + FROM categories JOIN products ON categories.category_id = products.category_id + GROUP BY categories.name + SQL +} + schema "default" { charset = "utf8mb4" collate = "utf8mb4_0900_ai_ci"