Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions task.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
-- Use our database
USE ShopDB;

-- Some data should be created outside the transaction (here)

-- Start the transaction
USE ShopDB;
INSERT INTO Orders (CustomerID, Date) VALUES (1, '2023-01-01');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The INSERT INTO Orders statement is executed before START TRANSACTION, so creating the order is not part of the atomic transaction with the order item insertion and warehouse update. According to the business rules and checklist item #17, consider moving this insert inside the transaction so that order creation, OrderItems, and product stock updates either all succeed or all roll back together.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a conflict here, and I'd like to clarify the correct approach.
I originally placed INSERT INTO Orders outside the transaction on purpose, because the automated test in test.sh (GitHub Actions) requires it. There's an explicit check:

insertIntoOrders=$(echo $afterTransactionStart | grep "INSERT INTO Orders" || true)
if [ -z "$insertIntoOrders" ]; then
echo "checked if Orders are updated outside of the transaction - ok"
else
echo "error: Orders are updated inside of the transaction"
exit 1
fi

This test looks for INSERT INTO Orders after START TRANSACTION and fails with exit 1 if it finds it. It requires order creation to be outside the transaction.
Your review asks for the opposite — to move INSERT INTO Orders inside the transaction. If I do that, the CI test will fail.

START TRANSACTION;

-- And some data should be created inside the transaction

INSERT INTO OrderItems (ProductID, Count, OrderID) VALUES (1, 1, LAST_INSERT_ID());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LAST_INSERT_ID() here depends on the previous insert into Orders, which currently happens outside the transaction. After moving the Orders insert into the transaction, this will correctly link the OrderItems row to the newly created order as part of the same atomic operation.

UPDATE Products SET WarehouseAmount = WarehouseAmount - 1 WHERE ID = 1;
COMMIT;
Loading