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

Commit 753dcaf

Browse files
committed
Add indexes for the existing wp_woocommerce_orders table schema.
This commit adds indexes to the following columns in the existing schema, along with corresponding tests: * order_key (unique) - used to identify the order within WooCommerce. * customer_id - the WordPress user ID of the customer, or 0 if the order was a guest checkout. May be worth using a foreign key relationship in a later iteration. * order_total - Available for sorting via the WooCommerce > Orders screen in wp-admin The WC_Custom_Order_Table_Install::$table_version property has also been incremented.
1 parent d50e67d commit 753dcaf

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

includes/class-wc-custom-order-table-install.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WC_Custom_Order_Table_Install {
1818
*
1919
* @var int
2020
*/
21-
protected static $table_version = 1;
21+
protected static $table_version = 2;
2222

2323
/**
2424
* Actions to perform on plugin activation.
@@ -88,7 +88,10 @@ protected static function install_tables() {
8888
date_completed datetime DEFAULT NULL,
8989
date_paid datetime DEFAULT NULL,
9090
cart_hash varchar(32) NOT NULL,
91-
PRIMARY KEY (order_id)
91+
PRIMARY KEY (order_id),
92+
UNIQUE KEY `order_key` (`order_key`),
93+
KEY `customer_id` (`customer_id`),
94+
KEY `order_total` (`total`)
9295
) $collate;
9396
";
9497

tests/test-installation.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,60 @@ public function test_current_schema_version_is_not_autoloaded() {
103103
'The schema version should not be autoloaded.'
104104
);
105105
}
106+
107+
/**
108+
* Test that the generated database schema contains the expected indexes.
109+
*
110+
* @dataProvider table_index_provider()
111+
*/
112+
public function test_database_indexes( $non_unique, $key_name, $column_name ) {
113+
global $wpdb;
114+
115+
WC_Custom_Order_Table_Install::activate();
116+
117+
$table = wc_custom_order_table()->get_table_name();
118+
$indexes = $wpdb->get_results( "SHOW INDEX FROM $table", ARRAY_A );
119+
$search = array(
120+
'Non_unique' => $non_unique,
121+
'Key_name' => $key_name,
122+
'Column_name' => $column_name,
123+
);
124+
125+
// Find the index by name.
126+
foreach ( $indexes as $index ) {
127+
if ( $index['Key_name'] !== $key_name ) {
128+
continue;
129+
}
130+
131+
$this->assertEquals(
132+
$non_unique,
133+
$index['Non_unique'],
134+
sprintf(
135+
'Did not match expected non-uniqueness (Received %d, expected %d',
136+
$non_unique,
137+
$index['Non_unique']
138+
)
139+
);
140+
141+
$this->assertEquals(
142+
$column_name,
143+
$index['Column_name'],
144+
sprintf( 'Expected index "%s" on column %s.', $key_name, $column_name )
145+
);
146+
147+
// We've checked the index we've come to check, return early.
148+
return;
149+
}
150+
151+
$this->fail( sprintf( 'Could not find an index with name "%s".', $key_name ) );
152+
}
153+
154+
public function table_index_provider() {
155+
return array(
156+
'Primary key' => array( 0, 'PRIMARY', 'order_id' ),
157+
'Order key' => array( 0, 'order_key', 'order_key' ),
158+
'Customer ID' => array( 1, 'customer_id', 'customer_id' ),
159+
'Order total' => array( 1, 'order_total', 'total' ),
160+
);
161+
}
106162
}

0 commit comments

Comments
 (0)