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

Commit c69bd04

Browse files
Merge pull request #15 from liquidweb/feature/database-table-index
Add indexes for the existing wp_woocommerce_orders table schema.
2 parents c0d1be4 + 753dcaf commit c69bd04

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
@@ -25,7 +25,7 @@ class WC_Custom_Order_Table_Install {
2525
*
2626
* @var int
2727
*/
28-
protected static $table_version = 1;
28+
protected static $table_version = 2;
2929

3030
/**
3131
* Actions to perform on plugin activation.
@@ -95,7 +95,10 @@ protected static function install_tables() {
9595
date_completed datetime DEFAULT NULL,
9696
date_paid datetime DEFAULT NULL,
9797
cart_hash varchar(32) NOT NULL,
98-
PRIMARY KEY (order_id)
98+
PRIMARY KEY (order_id),
99+
UNIQUE KEY `order_key` (`order_key`),
100+
KEY `customer_id` (`customer_id`),
101+
KEY `order_total` (`total`)
99102
) $collate;
100103
";
101104

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)