Change wp_woocommerce_sessions primary key

This commit changes wp_woocommerce_sessions primary key to the bigint field session_id. Before it was the char(32) field session_key. Doing this change primarily as it should reduce the occurrence of deadlocks, but also because it is not a good practice to use a char(32) field as the primary key of a table.
This commit is contained in:
Rodrigo Primo 2018-08-31 16:40:15 -03:00
parent fd722da88a
commit 2740a686dc
2 changed files with 21 additions and 2 deletions

View File

@ -115,6 +115,7 @@ class WC_Install {
),
'3.5.0' => array(
'wc_update_350_order_customer_id',
'wc_update_350_change_woocommerce_sessions_schema',
'wc_update_350_db_version',
),
);
@ -627,8 +628,8 @@ CREATE TABLE {$wpdb->prefix}woocommerce_sessions (
session_key char(32) NOT NULL,
session_value longtext NOT NULL,
session_expiry BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (session_key),
UNIQUE KEY session_id (session_id)
PRIMARY KEY (session_id),
UNIQUE KEY session_key (session_key)
) $collate;
CREATE TABLE {$wpdb->prefix}woocommerce_api_keys (
key_id BIGINT UNSIGNED NOT NULL auto_increment,

View File

@ -1975,6 +1975,24 @@ function wc_update_350_order_customer_id( $updater = false ) {
wp_cache_flush();
}
/**
* Change wp_woocommerce_sessions schema to use a bigint auto increment field
* instead of char(32) field as the primary key. Doing this change primarily as
* it should reduce the occurrence of deadlocks (see
* https://github.com/woocommerce/woocommerce/issues/20912), but also because
* it is not a good practice to use a char(32) field as the primary key of a
* table.
*
* @return void
*/
function wc_update_350_change_woocommerce_sessions_schema() {
global $wpdb;
$wpdb->query(
"ALTER TABLE `{$wpdb->prefix}woocommerce_sessions` DROP PRIMARY KEY, DROP KEY `session_id`, ADD PRIMARY KEY(`session_id`), ADD UNIQUE KEY(`session_key`)"
);
}
/**
* Update DB Version.
*/