Skip to content

Commit 191864c

Browse files
authored
Merge branch 'develop' into ast-sqlite-driver
2 parents 335388b + 271163b commit 191864c

File tree

5 files changed

+148
-24
lines changed

5 files changed

+148
-24
lines changed

.gitattributes

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
composer.json export-ignore
66
phpcs.xml.dist export-ignore
77
phpunit.xml.dist export-ignore
8+
/.github export-ignore
89
/grammar-tools export-ignore
910
/tests export-ignore
1011
/wp-includes/mysql export-ignore
1112
/wp-includes/parser export-ignore
1213
/wp-includes/sqlite-ast export-ignore
13-
wp-includes/sqlite/class-wp-sqlite-crosscheck-db.php export-ignore
14+
/wp-includes/sqlite/class-wp-sqlite-crosscheck-db.php export-ignore

load.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: SQLite Database Integration
44
* Description: SQLite database driver drop-in.
55
* Author: The WordPress Team
6-
* Version: 2.1.15
6+
* Version: 2.1.16
77
* Requires PHP: 7.0
88
* Textdomain: sqlite-database-integration
99
*

readme.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
=== SQLite Database Integration ===
22

3-
Contributors: wordpressdotorg, aristath
3+
Contributors: wordpressdotorg, aristath, janjakes, zieladam, berislav.grgicak, bpayton, zaerl
44
Requires at least: 6.4
55
Tested up to: 6.6.1
66
Requires PHP: 7.0
7-
Stable tag: 2.1.15
7+
Stable tag: 2.1.16
88
License: GPLv2 or later
99
License URI: https://www.gnu.org/licenses/gpl-2.0.html
1010
Tags: performance, database

tests/WP_SQLite_Translator_Tests.php

+92-16
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ public function testShowCreateTable1() {
267267
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
268268
option_name VARCHAR(255) default '',
269269
option_value TEXT NOT NULL,
270-
UNIQUE KEY option_name (option_name),
271-
KEY composite (option_name, option_value)
270+
UNIQUE KEY option_name (option_name(100)),
271+
KEY composite (option_name(100), option_value(100))
272272
);"
273273
);
274274

@@ -283,8 +283,49 @@ public function testShowCreateTable1() {
283283
`option_name` varchar(255) DEFAULT '',
284284
`option_value` text NOT NULL DEFAULT '',
285285
PRIMARY KEY (`ID`),
286-
KEY `composite` (`option_name`, `option_value`),
287-
UNIQUE KEY `option_name` (`option_name`)
286+
KEY `composite` (`option_name`(100), `option_value`(100)),
287+
UNIQUE KEY `option_name` (`option_name`(100))
288+
);",
289+
$results[0]->{'Create Table'}
290+
);
291+
}
292+
293+
public function testShowCreateTableWithEmptyDatetimeDefault() {
294+
$this->assertQuery(
295+
"CREATE TABLE _tmp_table (
296+
ID BIGINT PRIMARY KEY AUTO_INCREMENT,
297+
timestamp1 datetime NOT NULL,
298+
timestamp2 date NOT NULL,
299+
timestamp3 time NOT NULL,
300+
timestamp4 timestamp NOT NULL,
301+
timestamp5 year NOT NULL,
302+
notempty1 datetime DEFAULT '1999-12-12 12:12:12',
303+
notempty2 date DEFAULT '1999-12-12',
304+
notempty3 time DEFAULT '12:12:12',
305+
notempty4 year DEFAULT '2024',
306+
notempty5 timestamp DEFAULT '1734539165',
307+
);"
308+
);
309+
310+
$this->assertQuery(
311+
'SHOW CREATE TABLE _tmp_table;'
312+
);
313+
$results = $this->engine->get_query_results();
314+
315+
$this->assertEquals(
316+
"CREATE TABLE `_tmp_table` (
317+
`ID` bigint AUTO_INCREMENT,
318+
`timestamp1` datetime NOT NULL,
319+
`timestamp2` date NOT NULL,
320+
`timestamp3` time NOT NULL,
321+
`timestamp4` timestamp NOT NULL,
322+
`timestamp5` year NOT NULL,
323+
`notempty1` datetime DEFAULT '1999-12-12 12:12:12',
324+
`notempty2` date DEFAULT '1999-12-12',
325+
`notempty3` time DEFAULT '12:12:12',
326+
`notempty4` year DEFAULT '2024',
327+
`notempty5` timestamp DEFAULT '1734539165',
328+
PRIMARY KEY (`ID`)
288329
);",
289330
$results[0]->{'Create Table'}
290331
);
@@ -296,8 +337,8 @@ public function testShowCreateTableQuoted() {
296337
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
297338
option_name VARCHAR(255) default '',
298339
option_value TEXT NOT NULL,
299-
UNIQUE KEY option_name (option_name),
300-
KEY composite (option_name, option_value)
340+
UNIQUE KEY option_name (option_name(100)),
341+
KEY composite (option_name, option_value(100))
301342
);"
302343
);
303344

@@ -312,8 +353,8 @@ public function testShowCreateTableQuoted() {
312353
`option_name` varchar(255) DEFAULT '',
313354
`option_value` text NOT NULL DEFAULT '',
314355
PRIMARY KEY (`ID`),
315-
KEY `composite` (`option_name`, `option_value`),
316-
UNIQUE KEY `option_name` (`option_name`)
356+
KEY `composite` (`option_name`(100), `option_value`(100)),
357+
UNIQUE KEY `option_name` (`option_name`(100))
317358
);",
318359
$results[0]->{'Create Table'}
319360
);
@@ -377,8 +418,8 @@ public function testCreateTablseWithIdenticalIndexNames() {
377418
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
378419
option_name VARCHAR(255) default '',
379420
option_value TEXT NOT NULL,
380-
KEY `option_name` (`option_name`),
381-
KEY `double__underscores` (`option_name`, `ID`)
421+
KEY `option_name` (`option_name`(100)),
422+
KEY `double__underscores` (`option_name`(100), `ID`)
382423
);"
383424
);
384425

@@ -387,8 +428,8 @@ public function testCreateTablseWithIdenticalIndexNames() {
387428
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
388429
option_name VARCHAR(255) default '',
389430
option_value TEXT NOT NULL,
390-
KEY `option_name` (`option_name`),
391-
KEY `double__underscores` (`option_name`, `ID`)
431+
KEY `option_name` (`option_name`(100)),
432+
KEY `double__underscores` (`option_name`(100), `ID`)
392433
);"
393434
);
394435
}
@@ -399,8 +440,8 @@ public function testShowCreateTablePreservesDoubleUnderscoreKeyNames() {
399440
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
400441
option_name VARCHAR(255) default '',
401442
option_value TEXT NOT NULL,
402-
KEY `option_name` (`option_name`),
403-
KEY `double__underscores` (`option_name`, `ID`)
443+
KEY `option_name` (`option_name`(100)),
444+
KEY `double__underscores` (`option_name`(100), `ID`)
404445
);"
405446
);
406447

@@ -414,8 +455,43 @@ public function testShowCreateTablePreservesDoubleUnderscoreKeyNames() {
414455
`option_name` varchar(255) DEFAULT \'\',
415456
`option_value` text NOT NULL DEFAULT \'\',
416457
PRIMARY KEY (`ID`),
417-
KEY `double__underscores` (`option_name`, `ID`),
418-
KEY `option_name` (`option_name`)
458+
KEY `double__underscores` (`option_name`(100), `ID`),
459+
KEY `option_name` (`option_name`(100))
460+
);',
461+
$results[0]->{'Create Table'}
462+
);
463+
}
464+
465+
public function testShowCreateTableLimitsKeyLengths() {
466+
$this->assertQuery(
467+
'CREATE TABLE _tmp__table (
468+
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
469+
`order_id` bigint(20) unsigned DEFAULT NULL,
470+
`meta_key` varchar(20) DEFAULT NULL,
471+
`meta_value` text DEFAULT NULL,
472+
`meta_data` mediumblob DEFAULT NULL,
473+
PRIMARY KEY (`id`),
474+
KEY `meta_key_value` (`meta_key`(20),`meta_value`(82)),
475+
KEY `order_id_meta_key_meta_value` (`order_id`,`meta_key`(100),`meta_value`(82)),
476+
KEY `order_id_meta_key_meta_data` (`order_id`,`meta_key`(100),`meta_data`(100))
477+
);'
478+
);
479+
480+
$this->assertQuery(
481+
'SHOW CREATE TABLE _tmp__table;'
482+
);
483+
$results = $this->engine->get_query_results();
484+
$this->assertEquals(
485+
'CREATE TABLE `_tmp__table` (
486+
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
487+
`order_id` bigint(20) unsigned DEFAULT NULL,
488+
`meta_key` varchar(20) DEFAULT NULL,
489+
`meta_value` text DEFAULT NULL,
490+
`meta_data` mediumblob DEFAULT NULL,
491+
PRIMARY KEY (`id`),
492+
KEY `order_id_meta_key_meta_data` (`order_id`, `meta_key`(20), `meta_data`(100)),
493+
KEY `order_id_meta_key_meta_value` (`order_id`, `meta_key`(20), `meta_value`(100)),
494+
KEY `meta_key_value` (`meta_key`(20), `meta_value`(100))
419495
);',
420496
$results[0]->{'Create Table'}
421497
);

wp-includes/sqlite/class-wp-sqlite-translator.php

+51-4
Original file line numberDiff line numberDiff line change
@@ -3693,16 +3693,17 @@ protected function get_column_definitions( $table_name, $columns ) {
36933693
$auto_increment_column = $this->get_autoincrement_column( $table_name );
36943694
$column_definitions = array();
36953695
foreach ( $columns as $column ) {
3696+
$mysql_type = $this->get_cached_mysql_data_type( $table_name, $column->name );
36963697
$is_auto_incr = $auto_increment_column && strtolower( $auto_increment_column ) === strtolower( $column->name );
36973698
$definition = array();
36983699
$definition[] = '`' . $column->name . '`';
3699-
$definition[] = $this->get_cached_mysql_data_type( $table_name, $column->name ) ?? $column->name;
3700+
$definition[] = $mysql_type ?? $column->name;
37003701

37013702
if ( '1' === $column->notnull ) {
37023703
$definition[] = 'NOT NULL';
37033704
}
37043705

3705-
if ( null !== $column->dflt_value && '' !== $column->dflt_value && ! $is_auto_incr ) {
3706+
if ( $this->column_has_default( $column, $mysql_type ) && ! $is_auto_incr ) {
37063707
$definition[] = 'DEFAULT ' . $column->dflt_value;
37073708
}
37083709

@@ -3724,7 +3725,8 @@ protected function get_column_definitions( $table_name, $columns ) {
37243725
* @return array An array of key definitions
37253726
*/
37263727
private function get_key_definitions( $table_name, $columns ) {
3727-
$key_definitions = array();
3728+
$key_length_limit = 100;
3729+
$key_definitions = array();
37283730

37293731
$pks = array();
37303732
foreach ( $columns as $column ) {
@@ -3755,7 +3757,25 @@ private function get_key_definitions( $table_name, $columns ) {
37553757
$key_definition[] = sprintf( '`%s`', $index_name );
37563758

37573759
$cols = array_map(
3758-
function ( $column ) {
3760+
function ( $column ) use ( $table_name, $key_length_limit ) {
3761+
$data_type = strtolower( $this->get_cached_mysql_data_type( $table_name, $column['name'] ) );
3762+
$data_length = $key_length_limit;
3763+
3764+
// Extract the length from the data type. Make it lower if needed. Skip 'unsigned' parts and whitespace.
3765+
if ( 1 === preg_match( '/^(\w+)\s*\(\s*(\d+)\s*\)/', $data_type, $matches ) ) {
3766+
$data_type = $matches[1]; // "varchar"
3767+
$data_length = min( $matches[2], $key_length_limit ); // "255"
3768+
}
3769+
3770+
// Set the data length to the varchar and text key lengths
3771+
// char, varchar, varbinary, tinyblob, tinytext, blob, text, mediumblob, mediumtext, longblob, longtext
3772+
if ( str_ends_with( $data_type, 'char' ) ||
3773+
str_ends_with( $data_type, 'text' ) ||
3774+
str_ends_with( $data_type, 'blob' ) ||
3775+
str_starts_with( $data_type, 'var' )
3776+
) {
3777+
return sprintf( '`%s`(%s)', $column['name'], $data_length );
3778+
}
37593779
return sprintf( '`%s`', $column['name'] );
37603780
},
37613781
$key['columns']
@@ -3858,6 +3878,33 @@ function ( $row ) use ( $name_map ) {
38583878
);
38593879
}
38603880

3881+
/**
3882+
* Checks if column should define the default.
3883+
*
3884+
* @param stdClass $column The table column
3885+
* @param string $mysql_type The MySQL data type
3886+
*
3887+
* @return boolean If column should have a default definition.
3888+
*/
3889+
private function column_has_default( $column, $mysql_type ) {
3890+
if ( null === $column->dflt_value ) {
3891+
return false;
3892+
}
3893+
3894+
if ( '' === $column->dflt_value ) {
3895+
return false;
3896+
}
3897+
3898+
if (
3899+
in_array( strtolower( $mysql_type ), array( 'datetime', 'date', 'time', 'timestamp', 'year' ), true ) &&
3900+
"''" === $column->dflt_value
3901+
) {
3902+
return false;
3903+
}
3904+
3905+
return true;
3906+
}
3907+
38613908
/**
38623909
* Consumes data types from the query.
38633910
*

0 commit comments

Comments
 (0)