Skip to content

Conversation

@vedanshujain
Copy link
Contributor

@vedanshujain vedanshujain commented Mar 7, 2022

All Submissions:

Changes proposed in this Pull Request:

Closes #31766.

To add migrations, we add a general migration class that targets any combination of an entity table along with it's metadata table, described in the MetaToCustomTableMigrator class. We then use objects of this class in WPPostToCOTMigrator class providing the necessary configuration for each custom table.

How to test the changes in this Pull Request:

This PR should be tested along with #32052, #32053 and #32054. For code review, I have split it into four PRs, but for testing, it should be fine to only test the cot/migration-operational-data as it has all the commits.

We have now merged the separate PRs into this one, so all code is here and reviewing and testing just this one should be good enough.

  1. Checkout the cot/migration-operational-data branch (see PR Add migrations for operational data to custom order tables #32054).
  2. Make sure that you have {prefix}_wc_orders, {prefix}_wc_order_addresses and {prefix}_wc_order_operational_data created by following steps from Add DB table structure for custom order tables. #31811.
  3. Run this snippet:
namespace Automattic\WooCommerce\DataBase\Migrations\CustomOrderTable;

$done = false;
$migrator = new WPPostToCOTMigrator();
delete_option( 'wc_cot_migration' );
$done = $migrator->process_next_migration_batch( 500 );

Check contents of {prefix}_wc_orders, {prefix}_wc_order_addresses and {prefix}_wc_order_operational_data table and verify that first 500 order records are migrated.

  1. After the above commands you can also optionally run this to migrate all the records:
while( ! $done ) {
	$done = $migrator->process_next_migration_batch( 500 );
}

Note the PR has many TODO comments, which will be handled by different PRs as the resolution for them is not clear at this time.

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?

Changelog entry

Enhancement - Add migration for moving data from wp_posts to custom order table.

FOR PR REVIEWER ONLY:

  • I have reviewed that everything is sanitized/escaped appropriately for any SQL or XSS injection possibilities. I made sure Linting is not ignored or disabled.

@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Mar 7, 2022
@vedanshujain vedanshujain marked this pull request as ready for review March 15, 2022 11:06
@vedanshujain vedanshujain requested review from a team and jorgeatorres and removed request for a team March 15, 2022 11:17
Copy link
Member

@jorgeatorres jorgeatorres left a comment

Choose a reason for hiding this comment

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

Hi @vedanshujain!

This is brilliant work 💯. I particularly like the way you've abstracted migration of metadata in a way that can be easily re-used for each table.

Ignoring the TODO's for now, this looks good to me, with just one tiny exception (a typo) that I've flagged as that prevents the queries from working correctly.

I do have some general questions, which I'll leave not as part of the review but a comment in the PR.

*
* @return bool|\WP_Error
*/
public function process_single( $post_id ) {
Copy link
Member

Choose a reason for hiding this comment

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

This might be intentional but thought I'd ask: shouldn't process_single() also migrate other meta (addresses and op data) for the migrated order?
I know this is not being used anywhere right now, but it's still a public function and might become handy later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, I was hoping to do it for later, but let me do it now.

if ( count( $data['data'] ) !== $result ) {
// Some rows were not inserted.
// TODO: Find and log the entity ids that were not inserted.
echo 'error';
Copy link
Member

Choose a reason for hiding this comment

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

Even at this point, would it make sense to make this an exception or WP_Error instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm, so raising exceptions will break the entire migration flow, which is not ideal, and leave us in a weird state. but you are also right that logging might not be enough, let me think of better solutions here.

if ( count( $order_data['data'] ) !== $result ) {
// Some rows were not inserted.
// TODO: Find and log the entity ids that were not inserted.
echo ' error ';
Copy link
Member

Choose a reason for hiding this comment

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

See other related comments.

@jorgeatorres
Copy link
Member

Questions:

Please keep in mind that the items below might very well be already scoped,coming soon or even part of in-progress PRs, but given I'm new to the team and the COT project in particular, I thought I'd still ask them. Feel free to point me in the right direction if this has already been discussed or if there's a better place for these questions.

  1. Even though migrators have support for REPLACE operations, there's no logic right now that would choose the correct operation based on any state. For instance, deleting wc_cot_migration and re-running the migration will happily add duplicates to the tables.
  2. Related to the above, because wc_cot_migration only cares about the highest order ID that was migrated, what happens when an order with ID < highest order ID gets changed? I'm assuming eventually the COT code will add or update things on-demand, but then the point above is even more important or duplicates could be inserted.
  3. Given "orders" are migrated first and then their metadata, we could end up with orders in the orders table with no corresponding metadata if something fails. Is transaction support being worked into the migration at a later point?

Thanks!

@jorgeatorres jorgeatorres added the needs: author feedback The issue/PR needs a response from any of the parties involved in the issue. label Mar 23, 2022
@vedanshujain
Copy link
Contributor Author

Even though migrators have support for REPLACE operations, there's no logic right now that would choose the correct operation based on any state.

I was planning to do this later, if you are happy with the overall approach than I can handle this now 👍🏽

Related to the above, because wc_cot_migration only cares about the highest order ID that was migrated, what happens when an order with ID < highest order ID gets changed?

So that would be handled by a different flow ideally which is not implemented yet. Basically, we will also hook on every order save if migrations are running in the background to handle updates one at a time.

Given "orders" are migrated first and then their metadata, we could end up with orders in the orders table with no corresponding metadata if something fails. Is transaction support being worked into the migration at a later point?

Not at this point, but it's a good callout, let me see if we can handle it now. The biggest blocker in doing it is getting the order ID that was just inserted, but there should be a way.

@github-actions github-actions bot added needs: triage feedback Issues for which we requested feedback from the author and received it. and removed needs: author feedback The issue/PR needs a response from any of the parties involved in the issue. labels Mar 24, 2022
@vedanshujain vedanshujain marked this pull request as draft March 24, 2022 07:35
@vedanshujain
Copy link
Contributor Author

Marking the PR as draft again to address feedback.

@vedanshujain vedanshujain removed the needs: triage feedback Issues for which we requested feedback from the author and received it. label Mar 24, 2022
@vedanshujain vedanshujain marked this pull request as ready for review April 1, 2022 12:44
@vedanshujain
Copy link
Contributor Author

vedanshujain commented Apr 1, 2022

@jorgeatorres This is ready for review again with some caveats:

  1. Migrating metadata is implemented, but it's not yet enabled as it is not resilient currently (for example it will add duplicates).
  2. There are still a bunch of TODOs especially for surfacing errored orders, but I will be handling them in a different PR as it was getting too big.

Other than that, this PR should take care of not adding duplicates, updating instead of inserting when needed, handling interrupted migrations, etc. There is a code sniff failing although violations are fixed, I am looking into it but it should not be a blocker for review.

Copy link
Member

@jorgeatorres jorgeatorres left a comment

Choose a reason for hiding this comment

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

Hi @vedanshujain! 👋

Thanks for all your hard work on this 💯. Overall, it looks good to me, but I left a couple tiny observations. Please let me know what you think about those.

As for testing, I tested with a couple thousand orders in different configurations and didn't notice anything out of the ordinary, though we would have to test again once error handling is implemented as well as metadata migration.


namespace Automattic\WooCommerce\DataBase\Migrations;

use function DeliciousBrains\WP_Offload_Media\Aws3\JmesPath\search;
Copy link
Member

Choose a reason for hiding this comment

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

This seems to have been added by mistake as this is not in WC core nor used in this class.

* Generic migration class to move any entity, entity_meta table combination to custom table.
*/

namespace Automattic\WooCommerce\DataBase\Migrations\CustomOrderTable;
Copy link
Member

Choose a reason for hiding this comment

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

Might be a nitpick, but can we maybe use "Database" instead of "DataBase" in the namespace? It'd not only match the folder structure but also how the word is commonly used (i.e. "database"). If agreed, this would have to be modified in other classes too.

}

/**
* Process migration for metadata for given post ids.\
Copy link
Member

Choose a reason for hiding this comment

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

"\" at the end of the line.

@jorgeatorres jorgeatorres added the needs: author feedback The issue/PR needs a response from any of the parties involved in the issue. label Apr 5, 2022
@vedanshujain
Copy link
Contributor Author

@jorgeatorres Feedback addressed in c5e3150 . Do you think if we can merge this PR to the trunk, that way it will unblock work on UX implementation? I am meanwhile working on meta and better error logging in separate PRs.

@github-actions github-actions bot added needs: triage feedback Issues for which we requested feedback from the author and received it. and removed needs: author feedback The issue/PR needs a response from any of the parties involved in the issue. labels Apr 11, 2022
@jorgeatorres
Copy link
Member

Hi @vedanshujain!

Changes look good. Thank you!

Do you think if we can merge this PR to the trunk, that way it will unblock work on UX implementation?

I'm sure you have a better overview on all things COT than me, but the way I see it, it'd be best to merge this ASAP. Not only because of the UX work but also because it provides classes that might be useful for the other migrations that are still to be implemented. It also provides a small fix to the SQL schema which makes sense to get in soon.

Even if we need to make further adjustments, I'm good with merging this right now. Does that sound good to you?

Copy link
Member

@jorgeatorres jorgeatorres left a comment

Choose a reason for hiding this comment

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

LGTM 🚢. Beautiful work @vedanshujain! Thanks.

@jorgeatorres jorgeatorres merged commit e9e382a into trunk Apr 11, 2022
@jorgeatorres jorgeatorres deleted the cot/31766 branch April 11, 2022 17:30
@github-actions github-actions bot added this to the 6.5.0 milestone Apr 11, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Apr 11, 2022

Hi @jorgeatorres, thanks for merging this pull request. Please take a look at these follow-up tasks you may need to perform:

  • Add the release: add changelog label
  • Add the release: add testing instructions label

@exonianp
Copy link

exonianp commented May 1, 2022

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

Labels

needs: triage feedback Issues for which we requested feedback from the author and received it. plugin: woocommerce Issues related to the WooCommerce Core plugin.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[COT] Implement basic sync functions to move data into wc_order custom table

4 participants