From 603016fdda1064ca8066fecb69b78b32091c3632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=BE=20=D0=9A=D0=B5=D0=B4?= =?UTF-8?q?=D1=8C=D0=BE?= Date: Tue, 14 Jul 2026 17:27:42 +0300 Subject: [PATCH 1/3] Add transaction logic for creating order and order item Creates a new order outside a transaction (it starts empty), then wraps the order item insert and warehouse amount decrement in a transaction to keep stock counts consistent. Co-Authored-By: Claude Sonnet 5 --- task.sql | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/task.sql b/task.sql index 8adf22b..fe6f94e 100644 --- a/task.sql +++ b/task.sql @@ -1,11 +1,26 @@ -- Use our database -USE ShopDB; +USE ShopDB; -- Some data should be created outside the transaction (here) --- Start the transaction -START TRANSACTION; +-- Creating an order is a standalone action: a new order starts out empty, +-- so it does not need to be atomic with anything else. +INSERT INTO Orders (CustomerID, Date) + VALUES (1, '2023-01-01'); --- And some data should be created inside the transaction +-- Start the transaction +START TRANSACTION; -COMMIT; \ No newline at end of file +-- And some data should be created inside the transaction + +-- Adding an order item and decreasing the corresponding product's +-- WarehouseAmount must happen together, or not at all, to keep stock +-- counts consistent with what has actually been ordered. +INSERT INTO OrderItems (OrderID, ProductID, Count) + VALUES (1, 1, 1); + +UPDATE Products + SET WarehouseAmount = WarehouseAmount - 1 + WHERE ID = 1; + +COMMIT; \ No newline at end of file From ff9aa0d640f6592a50551c66c221f5782b30200f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=BE=20=D0=9A=D0=B5=D0=B4?= =?UTF-8?q?=D1=8C=D0=BE?= Date: Tue, 14 Jul 2026 17:43:29 +0300 Subject: [PATCH 2/3] Wrap entire order creation in a single transaction The order insert, its item, and the warehouse stock decrement must all commit or roll back together, so order creation is moved inside the transaction. Also capture the new order's ID with LAST_INSERT_ID() instead of hard-coding it, per review feedback on PR #203. Co-Authored-By: Claude Sonnet 5 --- task.sql | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/task.sql b/task.sql index fe6f94e..be3f0dd 100644 --- a/task.sql +++ b/task.sql @@ -2,25 +2,26 @@ USE ShopDB; -- Some data should be created outside the transaction (here) - --- Creating an order is a standalone action: a new order starts out empty, --- so it does not need to be atomic with anything else. -INSERT INTO Orders (CustomerID, Date) - VALUES (1, '2023-01-01'); +-- (nothing needs to be prepared outside the transaction for this order) -- Start the transaction START TRANSACTION; -- And some data should be created inside the transaction --- Adding an order item and decreasing the corresponding product's --- WarehouseAmount must happen together, or not at all, to keep stock --- counts consistent with what has actually been ordered. +-- Creating the order, adding its item, and decreasing the corresponding +-- product's WarehouseAmount must all succeed or all fail together, so the +-- whole order-creation flow is wrapped in a single transaction. +INSERT INTO Orders (CustomerID, Date) + VALUES (1, '2023-01-01'); + +SET @OrderID = LAST_INSERT_ID(); + INSERT INTO OrderItems (OrderID, ProductID, Count) - VALUES (1, 1, 1); + VALUES (@OrderID, 1, 1); UPDATE Products SET WarehouseAmount = WarehouseAmount - 1 WHERE ID = 1; -COMMIT; \ No newline at end of file +COMMIT; From 2bd5a9d49c3724148f67ca977290bbc7b4614378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=BE=20=D0=9A=D0=B5=D0=B4?= =?UTF-8?q?=D1=8C=D0=BE?= Date: Tue, 14 Jul 2026 17:46:35 +0300 Subject: [PATCH 3/3] Revert order creation to outside the transaction test.sh (the repo's own CI check) requires INSERT INTO Orders to occur before START TRANSACTION and only OrderItems/Products inside it; the previous commit moved Orders into the transaction per AI review feedback, which broke that check. Restore the original structure and keep capturing the order id via LAST_INSERT_ID() instead of a hard-coded value. Co-Authored-By: Claude Sonnet 5 --- task.sql | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/task.sql b/task.sql index be3f0dd..aabf105 100644 --- a/task.sql +++ b/task.sql @@ -2,21 +2,22 @@ USE ShopDB; -- Some data should be created outside the transaction (here) --- (nothing needs to be prepared outside the transaction for this order) --- Start the transaction -START TRANSACTION; - --- And some data should be created inside the transaction - --- Creating the order, adding its item, and decreasing the corresponding --- product's WarehouseAmount must all succeed or all fail together, so the --- whole order-creation flow is wrapped in a single transaction. +-- Creating an order is a standalone action: a new order starts out empty, +-- so it does not need to be atomic with anything else. INSERT INTO Orders (CustomerID, Date) VALUES (1, '2023-01-01'); SET @OrderID = LAST_INSERT_ID(); +-- Start the transaction +START TRANSACTION; + +-- And some data should be created inside the transaction + +-- Adding an order item and decreasing the corresponding product's +-- WarehouseAmount must happen together, or not at all, to keep stock +-- counts consistent with what has actually been ordered. INSERT INTO OrderItems (OrderID, ProductID, Count) VALUES (@OrderID, 1, 1);