-
Notifications
You must be signed in to change notification settings - Fork 186
Add transaction logic for creating order and order item #203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,28 @@ | ||
| -- 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 | ||
| SET @OrderID = LAST_INSERT_ID(); | ||
|
|
||
| COMMIT; | ||
| -- 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the task description, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| VALUES (@OrderID, 1, 1); | ||
|
|
||
| UPDATE Products | ||
| SET WarehouseAmount = WarehouseAmount - 1 | ||
|
Comment on lines
+24
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You currently update |
||
| WHERE ID = 1; | ||
|
|
||
| COMMIT; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You hard-code use of
OrderID = 1for the new order. This will only work if the newly created order’sIDis guaranteed to be 1, which the requirements do not state. To ensure the order item is attached to the order you just inserted, consider capturing the new order’s ID (e.g., viaLAST_INSERT_ID()into a variable) and using that value in theOrderItemsinsert.