Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Prevent duplicate IDs on order/refund save #64

Merged
merged 6 commits into from
May 25, 2018

Conversation

stevegrunwell
Copy link
Contributor

On a few different occasions (#49, #47, #58), users have reported errors similar to:

Error: A database error occurred while migrating order XXX: Duplicate entry 'XXX' for key 'PRIMARY'

These errors were the result of WC_Order_Data_Store_Custom_Table::$creating not always being set properly, so the plugin would sometimes try to INSERT rather than UPDATE the corresponding database row.

Additionally, since the WC_Order_Refund_Data_Store_Custom_Table from #52 — which this PR depends upon — was largely copied from WC_Order_Data_Store_Custom_Table (I really can't wait until we can use PHP traits in WordPress!), that class would have run into the same issue.

This PR simplifies the logic around the data stores' update_post_meta() methods, explicitly checking whether or not a corresponding database row exists and, if not, explicitly inserting it. This enables us to remove the less-than-ideal $creating properties and avoid duplicate IDs.

Blocked by #52. Fixes #47, fixes #49.

Since the WPDB class doesn't have anything like insertOrUpdate(), this tiny query takes an order ID and tells us whether or not there's an entry in the custom orders table for that ID (since each order/refund) has exactly one row.
…om orders table

There's a not-so-great convention of `$this->creating` from earlier in development, but this value can be deceiving. Until we're able to refactor it out, verify that any requests for inserting rows into the custom orders table check that a row doesn't already exist.
… property

Instead of trusting a property that isn't always accurate, explicitly see if a row exists before trying to insert.
As of a few minutes ago, the WordPress build tools should be fixed, enabling us to continue our tests against trunk.

Reference: https://core.trac.wordpress.org/ticket/43055
@bswatson bswatson merged commit e7c8b52 into develop May 25, 2018
@bswatson bswatson deleted the fix/duplicate-ids-on-update-post-meta branch May 25, 2018 14:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
2 participants