Remove unique constraint from order_key to prevent empty key conflict. (#37594)

This commit is contained in:
Barry Hughes 2023-04-13 08:50:23 -07:00 committed by GitHub
commit fdd01353df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 2 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Remove unique constraint from order_key, since orders can be created with empty order keys, which then conflict with the constraint.

View File

@ -847,7 +847,7 @@ WHERE
$wpdb->prepare(
"SELECT {$orders_table}.id FROM {$orders_table}
INNER JOIN {$op_table} ON {$op_table}.order_id = {$orders_table}.id
WHERE {$op_table}.order_key = %s",
WHERE {$op_table}.order_key = %s AND {$op_table}.order_key != ''",
$order_key
)
);
@ -2412,7 +2412,7 @@ CREATE TABLE $operational_data_table_name (
discount_total_amount decimal(26, 8) NULL,
recorded_sales tinyint(1) NULL,
UNIQUE KEY order_id (order_id),
UNIQUE KEY order_key (order_key)
KEY order_key (order_key)
) $collate;
CREATE TABLE $meta_table (
id bigint(20) unsigned auto_increment primary key,

View File

@ -746,4 +746,21 @@ WHERE order_id = {$order_id} AND meta_key = 'non_unique_key_1' AND meta_value in
$this->assertEmpty( $errors );
}
/**
* @testDox Test migration for multiple null order_key meta value.
*/
public function test_order_key_null_multiple() {
$order1 = OrderHelper::create_order();
$order2 = OrderHelper::create_order();
delete_post_meta( $order1->get_id(), '_order_key' );
delete_post_meta( $order2->get_id(), '_order_key' );
$this->sut->migrate_order( $order1->get_id() );
$this->sut->migrate_order( $order2->get_id() );
$errors = $this->sut->verify_migrated_orders( array( $order1->get_id(), $order2->get_id() ) );
$this->assertEmpty( $errors );
}
}