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

Commit ddd4865

Browse files
committed
Set an explicit fallback ORDER BY for the migrate command
When a test creates multiple test orders within the same second, the resulting products cause MySQL to try to guess which order should come first rather than — as I had assumed — falling back to sorting by ID. As a result, ordering products by p.post_date alone cannot reliably keep two orders sorted the same way, since MySQL may decide to give precedence to any one of the rows with matching timestamps. Further details are [available in MySQL's "ORDER BY Optimization" documentation](https://dev.mysql.com/doc/refman/5.5/en/order-by-optimization.html). This commit ensures that the `migrate` command explicitly falls back to sorting by `wp_posts.ID` in descending order, which matches the intent of "migrate the most recent orders (which are presumably more likely to be active) first, working backwards historically". Additionally, this commit cleans up instances where tests were passing integers instead of arrays to `TestCase::count_orders_in_table_with_ids()`, relying on type coercion. Fixes #83.
1 parent 65c88ef commit ddd4865

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

includes/class-woocommerce-custom-orders-table-cli.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function migrate( $args = array(), $assoc_args = array() ) {
110110
"SELECT p.ID FROM {$wpdb->posts} p LEFT JOIN " . esc_sql( $order_table ) . ' o ON p.ID = o.order_id
111111
WHERE p.post_type IN (' . implode( ', ', array_fill( 0, count( $order_types ), '%s' ) ) . ')
112112
AND o.order_id IS NULL
113-
ORDER BY p.post_date DESC
113+
ORDER BY p.post_date DESC, p.ID DESC
114114
LIMIT %d',
115115
array_merge( $order_types, array( $assoc_args['batch-size'] ) )
116116
);

tests/test-cli.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@ public function test_migrate_with_duplicate_ids() {
227227
$order = wc_get_order( $order_id );
228228
$order->get_total();
229229

230-
$this->assertEquals( 1, $this->count_orders_in_table_with_ids( $order_id ));
230+
$this->assertEquals( 1, $this->count_orders_in_table_with_ids( array( $order_id ) ) );
231231

232232
$this->cli->migrate();
233233

234-
$this->assertEquals( 1, $this->count_orders_in_table_with_ids( $order_id ));
234+
$this->assertEquals( 1, $this->count_orders_in_table_with_ids( array( $order_id ) ) );
235235
}
236236

237237
public function test_migrate_aborts_if_no_orders_require_migration() {
@@ -263,7 +263,7 @@ public function test_migrate_output_when_items_were_skipped() {
263263

264264
$this->assertEquals(
265265
2,
266-
$this->count_orders_in_table_with_ids( array( $order1->get_id(), $order3->get_id() ) ),
266+
$this->count_orders_in_table_with_ids( array( $order2->get_id(), $order3->get_id() ) ),
267267
'Expected to only see two orders in the custom table.'
268268
);
269269

tests/test-order-data-store.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public function test_loading_a_product_can_automatically_populate_from_meta() {
1313
$order_id = WC_Helper_Order::create_order()->get_id();
1414
$this->toggle_use_custom_table( true );
1515

16-
$this->assertEquals( 0, $this->count_orders_in_table_with_ids( $order_id ) );
16+
$this->assertEquals( 0, $this->count_orders_in_table_with_ids( array( $order_id ) ) );
1717

1818
$order = wc_get_order( $order_id );
1919

20-
$this->assertEquals( 1, $this->count_orders_in_table_with_ids( $order->get_id() ) );
20+
$this->assertEquals( 1, $this->count_orders_in_table_with_ids( array( $order->get_id() ) ) );
2121
}
2222

2323
/**
@@ -34,7 +34,7 @@ public function test_wc_custom_order_table_automatic_migration_filter() {
3434
$order = wc_get_order( $order_id );
3535

3636
$this->assertEmpty( $order->get_total() );
37-
$this->assertEquals( 0, $this->count_orders_in_table_with_ids( $order_id ) );
37+
$this->assertEquals( 0, $this->count_orders_in_table_with_ids( array( $order_id ) ) );
3838
}
3939

4040
public function test_delete() {

0 commit comments

Comments
 (0)