Skip to content

update task.sql#141

Open
taniakolesnik wants to merge 3 commits intomate-academy:mainfrom
taniakolesnik:main
Open

update task.sql#141
taniakolesnik wants to merge 3 commits intomate-academy:mainfrom
taniakolesnik:main

Conversation

@taniakolesnik
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

This is a good start, and you've correctly used a transaction to group the database operations. However, some changes are needed to ensure the logic is correct and robust.

There are two critical issues with the current implementation:

  1. Incorrect Stock Check: The condition WHERE WarehouseAmount > @count prevents a purchase when the stock level is exactly equal to the amount requested. To allow a customer to buy the last available item, this should be WHERE WarehouseAmount >= @count.
  2. Transaction Atomicity: If there isn't enough stock, the transaction is still committed, which creates an empty order. A transaction should be an "all-or-nothing" operation. If the stock check fails, you should use ROLLBACK to cancel the entire transaction and prevent inconsistent data.

Once you've addressed these points, your solution will correctly handle the order creation process. Also, as a minor cleanup, please remember to remove the debugging SELECT statement from the final script.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

Great job correcting the stock-checking logic to use >=! That's a key improvement. However, this submission requires changes to ensure the transaction is handled correctly, which is causing the automated checks to fail.

Here are the main points to address:

  1. Transaction Scope: The INSERT INTO Orders statement must be moved before the START TRANSACTION; line. The requirement is to create an empty order first, and the transaction should only protect the atomic operations of adding an item and updating stock. This is the primary reason the build is failing.
  2. Missing ROLLBACK: If there isn't enough stock, the transaction must be explicitly canceled with ROLLBACK. Your script should use an IF/ELSE structure to either COMMIT on success or ROLLBACK on failure to ensure atomicity.

Also, please remember to remove the debugging SELECT statement from the final script.

You're very close to a solid solution. Keep up the great work!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

task.sql Outdated
Comment on lines +24 to +37
INSERT INTO Orders (`CustomerID`, `Date`)
select @customer_id, '2023-01-01' where @current_count >= @count;;

SET @order_id = LAST_INSERT_ID();

INSERT INTO OrderItems (OrderID, ProductID, Count)
SELECT @order_id, @product_id, @count
WHERE @current_count >= @count;

SELECT @current_count, @count, @current_count > @count;

UPDATE Products
SET WarehouseAmount = WarehouseAmount - @count
WHERE ID = @product_id AND WarehouseAmount >= @count;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

While this approach of using conditional INSERT and UPDATE statements is clever, it doesn't fully address the atomicity requirement and introduces a potential race condition.

If two transactions run at the same time, both could read the available stock before either one updates it. This could lead to an order being created (INSERT statements succeed) even if the stock update fails later, resulting in inconsistent data.

A more robust approach is to use an IF statement to check the stock. If there's enough stock, perform all the inserts and the update. If not, you should explicitly cancel the entire transaction using ROLLBACK, as requested in the previous review. This ensures the "all-or-nothing" principle of a transaction.

SELECT @order_id, @product_id, @count
WHERE @current_count >= @count;

SELECT @current_count, @count, @current_count > @count;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This SELECT statement appears to be for debugging. Please remember to remove such statements from the final script to keep the code clean.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

I am approving this submission to help you move forward, but please take a moment to review the feedback below as there are still a few important issues to address. You did an excellent job moving the order creation outside of the transaction block!

The main issue is that the transaction is always committed, even when there isn't enough stock. The script needs conditional logic, like an IF/ELSE block, to check the stock count. If the stock is sufficient, the transaction should be committed; otherwise, it must be canceled with ROLLBACK. Additionally, the debugging SELECT statement should be removed from the final script. You're very close to getting this perfect!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

SELECT @order_id, @product_id, @count
WHERE @current_count >= @count;

SELECT @current_count, @count, @current_count > @count;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This SELECT statement appears to be for debugging. Please remove it from the final version of the script.


-- And some data should be created inside the transaction

COMMIT; No newline at end of file
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This COMMIT statement is executed regardless of whether there was enough stock to fulfill the order. The transaction should only be committed if the operations inside it (inserting an order item and updating the product count) are successful. If there isn't enough stock, the transaction must be canceled with ROLLBACK. Consider using a conditional block (like IF/ELSE) to check the stock and then decide whether to COMMIT or ROLLBACK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants