Skip to content

Commit e1931d2

Browse files
authored
Fix datetime field with empty default (#166)
Fixes #165 I propose to fix an issue where dumping the datetime field that has empty value results with producing incorrect MySQL.
2 parents ac75e90 + 86b43fd commit e1931d2

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

tests/WP_SQLite_Translator_Tests.php

+41
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,47 @@ public function testShowCreateTable1() {
290290
);
291291
}
292292

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`)
329+
);",
330+
$results[0]->{'Create Table'}
331+
);
332+
}
333+
293334
public function testShowCreateTableQuoted() {
294335
$this->assertQuery(
295336
"CREATE TABLE _tmp_table (

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

+30-2
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

@@ -3858,6 +3859,33 @@ function ( $row ) use ( $name_map ) {
38583859
);
38593860
}
38603861

3862+
/**
3863+
* Checks if column should define the default.
3864+
*
3865+
* @param stdClass $column The table column
3866+
* @param string $mysql_type The MySQL data type
3867+
*
3868+
* @return boolean If column should have a default definition.
3869+
*/
3870+
private function column_has_default( $column, $mysql_type ) {
3871+
if ( null === $column->dflt_value ) {
3872+
return false;
3873+
}
3874+
3875+
if ( '' === $column->dflt_value ) {
3876+
return false;
3877+
}
3878+
3879+
if (
3880+
in_array( strtolower( $mysql_type ), array( 'datetime', 'date', 'time', 'timestamp', 'year' ), true ) &&
3881+
"''" === $column->dflt_value
3882+
) {
3883+
return false;
3884+
}
3885+
3886+
return true;
3887+
}
3888+
38613889
/**
38623890
* Consumes data types from the query.
38633891
*

0 commit comments

Comments
 (0)