Skip to content
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

Add HPOS CLI tool to compare an order between datastores #43173

Merged
merged 7 commits into from Jan 11, 2024
Merged

Conversation

jorgeatorres
Copy link
Member

@jorgeatorres jorgeatorres commented Dec 29, 2023

Submission Review Guidelines:

Changes proposed in this Pull Request:

This PR adds a new CLI tool wp wc hpos diff which can be used to compare an order between datastores to see which properties or metadata differ. It's slightly more user friendly than wp wc verify_cot_data for individual orders.

This PR also adds a wp wc hpos status tool that displays current values of various HPOS variables, such as whether it is enabled or not, or how many orders need to be synced/etc.

Closes #41908.

How to test the changes in this Pull Request:

Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:

  1. Make sure sync/compatibility mode is enabled on WC > Settings > Advanced > Features. Any datastore can be used.
  2. Create a few orders (if needed).
  3. Go to WC > Settings > Advanced > Features and disable compatibility mode.
  4. Take note of the ID of any existing order.
  5. Run wp wc hpos diff <order_id>. Output should be "Success: No differences found.".
  6. Go to WC > Orders and make some changes to the order. For example, add some metadata or change any detail in the billing/shipping address.
  7. Run wp wc hpos diff <order_id> again. This time the output should look something like this (depending on the changes made in the previous step):
    Warning: Differences found for order 8472:
    +------------------------+---------------------------+---------------------------+
    | property               | hpos                      | post                      |
    +------------------------+---------------------------+---------------------------+
    | date_modified          | 2024-01-01T11:13:34+00:00 | 2023-12-28T09:12:37+00:00 |
    | _billing_address_index | James Brown       US      |                           |
    | my_meta                | my_value                  |                           |
    +------------------------+---------------------------+---------------------------+
    
  8. Now run wp wc cot sync to sync orders between datastores.
  9. Run wp wc hpos diff <order_id> again and confirm, once more, that output is "Success: No differences found.".
  10. Run wp wc hpos status to take a look at your HPOS variables. It should correctly reflect whether HPOS is enabled or not and other bits of information.

Changelog entry

  • Automatically create a changelog entry from the details below.

Significance

  • Patch
  • Minor
  • Major

Type

  • Fix - Fixes an existing bug
  • Add - Adds functionality
  • Update - Update existing functionality
  • Dev - Development related task
  • Tweak - A minor adjustment to the codebase
  • Performance - Address performance issues
  • Enhancement - Improvement to existing functionality

Message

Comment

@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Dec 29, 2023
Copy link
Contributor

github-actions bot commented Dec 29, 2023

Test Results Summary

Commit SHA: e12f483

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests25900202610m 37s
E2E Tests258003026114m 2s

To view the full API test report, click here.
To view the full E2E test report, click here.
To view all test reports, visit the WooCommerce Test Reports Dashboard.

@jorgeatorres jorgeatorres marked this pull request as ready for review January 1, 2024 11:22
@jorgeatorres jorgeatorres requested review from a team and lsinger and removed request for a team January 1, 2024 11:27
Copy link
Contributor

github-actions bot commented Jan 1, 2024

Hi @lsinger,

Apart from reviewing the code changes, please make sure to review the testing instructions as well.

You can follow this guide to find out what good testing instructions should look like:
https://github.com/woocommerce/woocommerce/wiki/Writing-high-quality-testing-instructions

Copy link
Contributor

@lsinger lsinger left a comment

Choose a reason for hiding this comment

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

Tests well -- a very nice and slick experience. Very cool!

Left a few notes, but no blockers in there. LGTM! 🚢

$val2 = get_post_meta( $order_id, $key, true );
}

if ( $val1 != $val2 ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
Copy link
Contributor

Choose a reason for hiding this comment

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

We might want to add a note here why it's absolutely necessary to allow loose comparison.

$data_store_wrapper = \WC_Data_Store::load( 'order' );

// Bind $data_store to our WC_Data_Store.
( function ( $data_store ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting pattern! Could you explain why this was the best (only?) option here? Just for my personal understanding.

Copy link
Member Author

Choose a reason for hiding this comment

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

Given WC_Order is a WC_Data child class, the datastore in objects of this type has access protected so we can't directly change it from the outside.
To make things worse, that data_store variable is not directly an instance of the HPOS (or CPT) datastore, but of WC_Data_Store, which stores the details in a private variables.

WC_Data_Store is meant to be a singleton so what we could do here is add public methods to WC_Data to change its backing datastore, but that's not without its own set of problems (what happens when reading or saving data if you change the backing datastore in an object mid execution?) and I guess we don't want to increase API surface at this moment.

Now, even though datastores have a read() method that receives a "blank" object, this method's name is a bit misleading because this will not set the backing datastore on the blank object nor read all properties.
Metadata is read via $this->data_store->read_meta() which would use the "default" datastore unless we change it "somehow".

This ugly hack above is that "somehow" 😓. I believe the whole CRUD layer and datastore/object interactions in particular would benefit from a revision, but 🤷.

(Sorry if this is even more confusing)

@jorgeatorres jorgeatorres merged commit 9ce508f into trunk Jan 11, 2024
32 checks passed
@jorgeatorres jorgeatorres deleted the hpos/41908 branch January 11, 2024 16:51
@github-actions github-actions bot added this to the 8.6.0 milestone Jan 11, 2024
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label Jan 11, 2024
@alopezari alopezari added needs: internal testing Indicates if the PR requires further testing conducted by Solaris status: analysis complete Indicates if a PR has been analysed by Solaris and removed needs: analysis Indicates if the PR requires a PR testing scrub session. labels Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: internal testing Indicates if the PR requires further testing conducted by Solaris plugin: woocommerce Issues related to the WooCommerce Core plugin. status: analysis complete Indicates if a PR has been analysed by Solaris
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CLI] Provide human-friendly output for the verification command
3 participants