Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final touches to #168 #169

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 51 additions & 16 deletions tests/WP_SQLite_Translator_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,10 @@ public function testShowCreateTable1() {
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
option_name VARCHAR(255) default '',
option_value TEXT NOT NULL,
UNIQUE KEY option_name (option_name),
KEY composite (option_name, option_value)
lorem BINARY(128) default '',
ipsum TINYTEXT(32) default '',
UNIQUE KEY option_name (option_name(100)),
KEY composite (option_name(100), option_value(100))
);"
);

Expand All @@ -282,9 +284,11 @@ public function testShowCreateTable1() {
`ID` bigint NOT NULL AUTO_INCREMENT,
`option_name` varchar(255) DEFAULT '',
`option_value` text NOT NULL DEFAULT '',
`lorem` binary(128) DEFAULT '',
`ipsum` tinytext(32) DEFAULT '',
PRIMARY KEY (`ID`),
KEY `composite` (`option_name`, `option_value`),
UNIQUE KEY `option_name` (`option_name`)
KEY `composite` (`option_name`(100), `option_value`(100)),
UNIQUE KEY `option_name` (`option_name`(100))
);",
$results[0]->{'Create Table'}
);
Expand All @@ -296,8 +300,8 @@ public function testShowCreateTableQuoted() {
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
option_name VARCHAR(255) default '',
option_value TEXT NOT NULL,
UNIQUE KEY option_name (option_name),
KEY composite (option_name, option_value)
UNIQUE KEY option_name (option_name(100)),
KEY composite (option_name, option_value(100))
);"
);

Expand All @@ -312,8 +316,8 @@ public function testShowCreateTableQuoted() {
`option_name` varchar(255) DEFAULT '',
`option_value` text NOT NULL DEFAULT '',
PRIMARY KEY (`ID`),
KEY `composite` (`option_name`, `option_value`),
UNIQUE KEY `option_name` (`option_name`)
KEY `composite` (`option_name`(100), `option_value`(100)),
UNIQUE KEY `option_name` (`option_name`(100))
);",
$results[0]->{'Create Table'}
);
Expand Down Expand Up @@ -377,8 +381,8 @@ public function testCreateTablseWithIdenticalIndexNames() {
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
option_name VARCHAR(255) default '',
option_value TEXT NOT NULL,
KEY `option_name` (`option_name`),
KEY `double__underscores` (`option_name`, `ID`)
KEY `option_name` (`option_name`(100)),
KEY `double__underscores` (`option_name`(100), `ID`)
);"
);

Expand All @@ -387,8 +391,8 @@ public function testCreateTablseWithIdenticalIndexNames() {
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
option_name VARCHAR(255) default '',
option_value TEXT NOT NULL,
KEY `option_name` (`option_name`),
KEY `double__underscores` (`option_name`, `ID`)
KEY `option_name` (`option_name`(100)),
KEY `double__underscores` (`option_name`(100), `ID`)
);"
);
}
Expand All @@ -399,8 +403,8 @@ public function testShowCreateTablePreservesDoubleUnderscoreKeyNames() {
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
option_name VARCHAR(255) default '',
option_value TEXT NOT NULL,
KEY `option_name` (`option_name`),
KEY `double__underscores` (`option_name`, `ID`)
KEY `option_name` (`option_name`(100)),
KEY `double__underscores` (`option_name`(100), `ID`)
);"
);

Expand All @@ -414,8 +418,39 @@ public function testShowCreateTablePreservesDoubleUnderscoreKeyNames() {
`option_name` varchar(255) DEFAULT \'\',
`option_value` text NOT NULL DEFAULT \'\',
PRIMARY KEY (`ID`),
KEY `double__underscores` (`option_name`, `ID`),
KEY `option_name` (`option_name`)
KEY `double__underscores` (`option_name`(100), `ID`),
KEY `option_name` (`option_name`(100))
);',
$results[0]->{'Create Table'}
);
}

public function testShowCreateTableLimitsKeyLengths() {
$this->assertQuery(
'CREATE TABLE _tmp__table (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`order_id` bigint(20) unsigned DEFAULT NULL,
`meta_key` varchar(20) DEFAULT NULL,
`meta_value` text DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `meta_key_value` (`meta_key`(20),`meta_value`(82)),
KEY `order_id_meta_key_meta_value` (`order_id`,`meta_key`(100),`meta_value`(82))
);'
);

$this->assertQuery(
'SHOW CREATE TABLE _tmp__table;'
);
$results = $this->engine->get_query_results();
$this->assertEquals(
'CREATE TABLE `_tmp__table` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`order_id` bigint(20) unsigned DEFAULT NULL,
`meta_key` varchar(20) DEFAULT NULL,
`meta_value` text DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `order_id_meta_key_meta_value` (`order_id`, `meta_key`(20), `meta_value`(100)),
KEY `meta_key_value` (`meta_key`(20), `meta_value`(100))
);',
$results[0]->{'Create Table'}
);
Expand Down
22 changes: 20 additions & 2 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3724,7 +3724,8 @@ protected function get_column_definitions( $table_name, $columns ) {
* @return array An array of key definitions
*/
private function get_key_definitions( $table_name, $columns ) {
$key_definitions = array();
$key_length_limit = 100;
$key_definitions = array();

$pks = array();
foreach ( $columns as $column ) {
Expand Down Expand Up @@ -3755,7 +3756,24 @@ private function get_key_definitions( $table_name, $columns ) {
$key_definition[] = sprintf( '`%s`', $index_name );

$cols = array_map(
function ( $column ) {
function ( $column ) use ( $table_name, $key_length_limit ) {
$data_type = strtolower( $this->get_cached_mysql_data_type( $table_name, $column['name'] ) );
$data_length = $key_length_limit;

// Extract the length from the data type. Make it lower if needed.
if ( 1 === preg_match( '/^(\w+)\s*\((\d+)\)$/', $data_type, $matches ) ) {
$data_type = $matches[1]; // "varchar"
$data_length = min( $matches[2], $key_length_limit ); // "255"
}

// Set the column length for binary, text and varchar data types (including variations)
if (
str_ends_with( $data_type, 'binary' ) ||
str_ends_with( $data_type, 'text' ) ||
str_starts_with( $data_type, 'varchar' )
) {
return sprintf( '`%s`(%s)', $column['name'], $data_length );
}
return sprintf( '`%s`', $column['name'] );
},
$key['columns']
Expand Down
Loading