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

Commit

Permalink
Merge pull request #65 from liquidweb/feature/table-optimizations
Browse files Browse the repository at this point in the history
Database table optimizations
  • Loading branch information
bswatson authored Sep 12, 2018
2 parents 35b834a + 4b6a892 commit 715fce3
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 47 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
]
},
"config": {
"sort-packages": true,
"platform": {
"php": "7.0"
}
Expand Down
1 change: 0 additions & 1 deletion includes/class-wc-order-data-store-custom-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ protected function update_post_meta( &$order ) {
'billing_state' => $order->get_billing_state( 'edit' ),
'billing_postcode' => $order->get_billing_postcode( 'edit' ),
'billing_country' => $order->get_billing_country( 'edit' ),

'billing_email' => $order->get_billing_email( 'edit' ),
'billing_phone' => $order->get_billing_phone( 'edit' ),

Expand Down
92 changes: 46 additions & 46 deletions includes/class-woocommerce-custom-orders-table-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,52 +54,52 @@ protected static function install_tables() {
$collate = $wpdb->get_charset_collate();
$tables = "
CREATE TABLE {$table} (
order_id BIGINT UNSIGNED NOT NULL,
order_key varchar(100),
customer_id BIGINT UNSIGNED NOT NULL,
billing_index varchar(255),
billing_first_name varchar(100),
billing_last_name varchar(100),
billing_company varchar(100),
billing_address_1 varchar(200),
billing_address_2 varchar(200),
billing_city varchar(100),
billing_state varchar(100),
billing_postcode varchar(100),
billing_country varchar(100),
billing_email varchar(200) NOT NULL,
billing_phone varchar(200),
shipping_index varchar(255),
shipping_first_name varchar(100),
shipping_last_name varchar(100),
shipping_company varchar(100),
shipping_address_1 varchar(200),
shipping_address_2 varchar(200),
shipping_city varchar(100),
shipping_state varchar(100),
shipping_postcode varchar(100),
shipping_country varchar(100),
payment_method varchar(100),
payment_method_title varchar(100),
discount_total varchar(100) NOT NULL DEFAULT 0,
discount_tax varchar(100) NOT NULL DEFAULT 0,
shipping_total varchar(100) NOT NULL DEFAULT 0,
shipping_tax varchar(100) NOT NULL DEFAULT 0,
cart_tax varchar(100) NOT NULL DEFAULT 0,
total varchar(100) NOT NULL DEFAULT 0,
version varchar(16) NOT NULL,
currency varchar(3) NOT NULL,
prices_include_tax varchar(3) NOT NULL,
transaction_id varchar(200) NOT NULL,
customer_ip_address varchar(40),
customer_user_agent varchar(200),
created_via varchar(200) NOT NULL,
date_completed varchar(20) DEFAULT NULL,
date_paid varchar(20) DEFAULT NULL,
cart_hash varchar(32),
amount varchar(100),
refunded_by BIGINT UNSIGNED,
reason text,
order_id BIGINT UNSIGNED NOT NULL COMMENT 'Order post ID',
order_key varchar(100) DEFAULT NULL COMMENT 'Unique order key',
customer_id BIGINT UNSIGNED NOT NULL COMMENT 'Customer ID. Will be 0 for guests.',
billing_index varchar(255) DEFAULT NULL COMMENT 'Billing fields, concatenated for search',
billing_first_name varchar(100) DEFAULT NULL COMMENT 'Billing first name',
billing_last_name varchar(100) DEFAULT NULL COMMENT 'Billing last name',
billing_company varchar(100) DEFAULT NULL COMMENT 'Billing company',
billing_address_1 varchar(255) DEFAULT NULL COMMENT 'Billing street address',
billing_address_2 varchar(200) DEFAULT NULL COMMENT 'Billing extended address',
billing_city varchar(100) DEFAULT NULL COMMENT 'Billing city/locality',
billing_state varchar(100) DEFAULT NULL COMMENT 'Billing state/province/locale',
billing_postcode varchar(20) DEFAULT NULL COMMENT 'Billing postal code',
billing_country char(2) DEFAULT NULL COMMENT 'Billing country (ISO 3166-1 Alpha-2)',
billing_email varchar(200) NOT NULL COMMENT 'Billing email address',
billing_phone varchar(200) DEFAULT NULL COMMENT 'Billing phone number',
shipping_index varchar(255) DEFAULT NULL COMMENT 'Shipping fields, concatenated for search',
shipping_first_name varchar(100) DEFAULT NULL COMMENT 'Shipping first name',
shipping_last_name varchar(100) DEFAULT NULL COMMENT 'Shipping last name',
shipping_company varchar(100) DEFAULT NULL COMMENT 'Shipping company',
shipping_address_1 varchar(255) DEFAULT NULL COMMENT 'Shipping street address',
shipping_address_2 varchar(200) DEFAULT NULL COMMENT 'Shipping extended address',
shipping_city varchar(100) DEFAULT NULL COMMENT 'Shipping city/locality',
shipping_state varchar(100) DEFAULT NULL COMMENT 'Shipping state/province/locale',
shipping_postcode varchar(20) DEFAULT NULL COMMENT 'Shipping postal code',
shipping_country char(2) DEFAULT NULL COMMENT 'Shipping country (ISO 3166-1 Alpha-2)',
payment_method varchar(100) DEFAULT NULL COMMENT 'Payment method ID',
payment_method_title varchar(100) DEFAULT NULL COMMENT 'Payment method title',
discount_total varchar(100) NOT NULL DEFAULT 0 COMMENT 'Discount total',
discount_tax varchar(100) NOT NULL DEFAULT 0 COMMENT 'Discount tax',
shipping_total varchar(100) NOT NULL DEFAULT 0 COMMENT 'Shipping total',
shipping_tax varchar(100) NOT NULL DEFAULT 0 COMMENT 'Shipping tax',
cart_tax varchar(100) NOT NULL DEFAULT 0 COMMENT 'Cart tax',
total varchar(100) NOT NULL DEFAULT 0 COMMENT 'Order total',
version varchar(16) NOT NULL COMMENT 'Version of WooCommerce when the order was made',
currency char(3) NOT NULL COMMENT 'Currency the order was created with',
prices_include_tax varchar(3) NOT NULL COMMENT 'Did the prices include tax during checkout?',
transaction_id varchar(200) NOT NULL COMMENT 'Unique transaction ID',
customer_ip_address varchar(40) DEFAULT NULL COMMENT 'The customer\'s IP address',
customer_user_agent varchar(200) DEFAULT NULL COMMENT 'The customer\'s User-Agent string',
created_via varchar(200) NOT NULL COMMENT 'Order creation method',
date_completed varchar(20) DEFAULT NULL COMMENT 'Date the order was completed',
date_paid varchar(20) DEFAULT NULL COMMENT 'Date the order was paid',
cart_hash varchar(32) DEFAULT NULL COMMENT 'Hash of cart items to ensure orders are not modified',
amount varchar(100) DEFAULT NULL COMMENT 'The refund amount',
refunded_by BIGINT UNSIGNED DEFAULT NULL COMMENT 'The ID of the user who issued the refund',
reason text DEFAULT NULL COMMENT 'The reason for the refund being issued',
PRIMARY KEY (order_id),
UNIQUE KEY `order_key` (`order_key`),
KEY `customer_id` (`customer_id`),
Expand Down
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// Finally, Start up the WP testing environment.
require_once dirname( __DIR__ ) . '/vendor/autoload.php';
require_once $_bootstrap;
require_once __DIR__ . '/compat.php';
require_once __DIR__ . '/testcase.php';
require_once dirname( __DIR__ ) . '/woocommerce-custom-orders-table.php';

Expand Down
52 changes: 52 additions & 0 deletions tests/compat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* This file contains work-arounds for older versions of the WooCommerce test suite.
*
* @package WooCommerce_Custom_Orders_Table
* @author Liquid Web
*/

/**
* Convert all instances of "USA" in tests to use the ISO 3166-1 alpha-2 (2 character) standard,
* (e.g. "US").
*
* @ticket https://github.com/woocommerce/woocommerce/pull/20222
*/
if ( version_compare( WC_VERSION, '3.4.4', '<=' ) ) {
add_filter( 'woocommerce_before_order_object_save', 'tests_truncate_order_data_store_country_codes' );
add_filter( 'woocommerce_order_query_args', 'tests_truncate_order_query_country_codes' );
}

/**
* Truncate instances of "USA" to "US" for billing/shipping country codes.
*
* @param WC_Order $order The order object.
*
* @return WC_Order The filtered WC_Order object.
*/
function tests_truncate_order_data_store_country_codes( $order ) {
foreach ( array( 'billing_country', 'shipping_country' ) as $prop ) {
if ( 'USA' === call_user_func( array( $order, 'get_' . $prop ) ) ) {
call_user_func( array( $order, 'set_' . $prop ), 'US' );
}
}

return $order;
}

/**
* When conducting a WC_Order_Query, shorten "USA" to "US".
*
* @param array $args An array of query arguments.
*
* @return array The filtered query arguments array.
*/
function tests_truncate_order_query_country_codes( $args ) {
foreach ( array( 'billing_country', 'shipping_country' ) as $prop ) {
if ( isset( $args[ $prop ] ) && 'USA' === $args[ $prop ] ) {
$args[ $prop ] = 'US';
}
}

return $args;
}
98 changes: 98 additions & 0 deletions tests/test-installation.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,104 @@ public function test_database_indexes( $non_unique, $key_name, $column_name ) {
$this->fail( sprintf( 'Could not find an index with name "%s".', $key_name ) );
}

/**
* Test the lengths of CHAR fields.
*
* @testWith ["billing_country", 2]
* ["shipping_country", 2]
* ["currency", 3]
*
* @link https://github.com/liquidweb/woocommerce-custom-orders-table/issues/48
*/
public function test_char_length( $column, $length ) {
global $wpdb;

$this->assert_column_has_type(
$column,
sprintf( 'char(%d)', $length ),
sprintf( 'Column "%s" did not match the expected CHAR length of %d.', $column, $length )
);
}

/**
* Test the lengths of VARCHAR fields.
*
* @global $wpdb
*
* @testWith ["order_key", 100]
* ["billing_index", 255]
* ["billing_first_name", 100]
* ["billing_last_name", 100]
* ["billing_company", 100]
* ["billing_address_1", 255]
* ["billing_address_2", 200]
* ["billing_city", 100]
* ["billing_state", 100]
* ["billing_postcode", 20]
* ["billing_email", 200]
* ["billing_phone", 200]
* ["shipping_index", 255]
* ["shipping_first_name", 100]
* ["shipping_last_name", 100]
* ["shipping_company", 100]
* ["shipping_address_1", 255]
* ["shipping_address_2", 200]
* ["shipping_city", 100]
* ["shipping_state", 100]
* ["shipping_postcode", 20]
* ["payment_method", 100]
* ["payment_method_title", 100]
* ["discount_total", 100]
* ["discount_tax", 100]
* ["shipping_total", 100]
* ["shipping_tax", 100]
* ["cart_tax", 100]
* ["total", 100]
* ["version", 16]
* ["prices_include_tax", 3]
* ["transaction_id", 200]
* ["customer_ip_address", 40]
* ["customer_user_agent", 200]
* ["created_via", 200]
* ["date_completed", 20]
* ["date_paid", 20]
* ["cart_hash", 32]
* ["amount", 100]
*
* @link https://github.com/liquidweb/woocommerce-custom-orders-table/issues/48
*/
public function test_varchar_length( $column, $length ) {
global $wpdb;

$this->assert_column_has_type(
$column,
sprintf( 'varchar(%d)', $length ),
sprintf( 'Column "%s" did not match the expected VARCHAR length of %d.', $column, $length )
);
}

/**
* Assert the type of a given column matches expectations.
*
* @global $wpdb
*
* @param string $column The column name to find.
* @param string $expected The expected column type, e.g. "varchar(255)".
* @param string $message Optional. An error message to display if the assertion fails.
*/
protected function assert_column_has_type( $column, $expected, $message = '' ) {
global $wpdb;

$this->assertSame(
$expected,
$wpdb->get_row( $wpdb->prepare(
'SHOW COLUMNS FROM ' . esc_sql( wc_custom_order_table()->get_table_name() ) . ' WHERE Field = %s',
$column
) )->Type,
$message
);
}

/**
* Determine if the custom orders table exists.
*
Expand Down

0 comments on commit 715fce3

Please sign in to comment.