diff --git a/tests/WP_SQLite_Translator_Tests.php b/tests/WP_SQLite_Translator_Tests.php index 20bc152e..b93afe30 100644 --- a/tests/WP_SQLite_Translator_Tests.php +++ b/tests/WP_SQLite_Translator_Tests.php @@ -290,6 +290,47 @@ public function testShowCreateTable1() { ); } + public function testShowCreateTableWithEmptyDatetimeDefault() { + $this->assertQuery( + "CREATE TABLE _tmp_table ( + ID BIGINT PRIMARY KEY AUTO_INCREMENT, + timestamp1 datetime NOT NULL, + timestamp2 date NOT NULL, + timestamp3 time NOT NULL, + timestamp4 timestamp NOT NULL, + timestamp5 year NOT NULL, + notempty1 datetime DEFAULT '1999-12-12 12:12:12', + notempty2 date DEFAULT '1999-12-12', + notempty3 time DEFAULT '12:12:12', + notempty4 year DEFAULT '2024', + notempty5 timestamp DEFAULT '1734539165', + );" + ); + + $this->assertQuery( + 'SHOW CREATE TABLE _tmp_table;' + ); + $results = $this->engine->get_query_results(); + + $this->assertEquals( + "CREATE TABLE `_tmp_table` ( + `ID` bigint AUTO_INCREMENT, + `timestamp1` datetime NOT NULL, + `timestamp2` date NOT NULL, + `timestamp3` time NOT NULL, + `timestamp4` timestamp NOT NULL, + `timestamp5` year NOT NULL, + `notempty1` datetime DEFAULT '1999-12-12 12:12:12', + `notempty2` date DEFAULT '1999-12-12', + `notempty3` time DEFAULT '12:12:12', + `notempty4` year DEFAULT '2024', + `notempty5` timestamp DEFAULT '1734539165', + PRIMARY KEY (`ID`) +);", + $results[0]->{'Create Table'} + ); + } + public function testShowCreateTableQuoted() { $this->assertQuery( "CREATE TABLE _tmp_table ( diff --git a/wp-includes/sqlite/class-wp-sqlite-translator.php b/wp-includes/sqlite/class-wp-sqlite-translator.php index 705b3970..0f61a7c7 100644 --- a/wp-includes/sqlite/class-wp-sqlite-translator.php +++ b/wp-includes/sqlite/class-wp-sqlite-translator.php @@ -3693,16 +3693,17 @@ protected function get_column_definitions( $table_name, $columns ) { $auto_increment_column = $this->get_autoincrement_column( $table_name ); $column_definitions = array(); foreach ( $columns as $column ) { + $mysql_type = $this->get_cached_mysql_data_type( $table_name, $column->name ); $is_auto_incr = $auto_increment_column && strtolower( $auto_increment_column ) === strtolower( $column->name ); $definition = array(); $definition[] = '`' . $column->name . '`'; - $definition[] = $this->get_cached_mysql_data_type( $table_name, $column->name ) ?? $column->name; + $definition[] = $mysql_type ?? $column->name; if ( '1' === $column->notnull ) { $definition[] = 'NOT NULL'; } - if ( null !== $column->dflt_value && '' !== $column->dflt_value && ! $is_auto_incr ) { + if ( $this->column_has_default( $column, $mysql_type ) && ! $is_auto_incr ) { $definition[] = 'DEFAULT ' . $column->dflt_value; } @@ -3858,6 +3859,33 @@ function ( $row ) use ( $name_map ) { ); } + /** + * Checks if column should define the default. + * + * @param stdClass $column The table column + * @param string $mysql_type The MySQL data type + * + * @return boolean If column should have a default definition. + */ + private function column_has_default( $column, $mysql_type ) { + if ( null === $column->dflt_value ) { + return false; + } + + if ( '' === $column->dflt_value ) { + return false; + } + + if ( + in_array( strtolower( $mysql_type ), array( 'datetime', 'date', 'time', 'timestamp', 'year' ), true ) && + "''" === $column->dflt_value + ) { + return false; + } + + return true; + } + /** * Consumes data types from the query. *