Skip to content

Commit 7835a5e

Browse files
committed
Ensure that for empty default date fields there is no default
1 parent 674bb6f commit 7835a5e

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

tests/WP_SQLite_Translator_Tests.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public function testShowCreateTable1() {
293293
public function testShowCreateTableWithEmptyDatetimeDefault() {
294294
$this->assertQuery(
295295
"CREATE TABLE _tmp_table (
296-
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
296+
ID BIGINT PRIMARY KEY AUTO_INCREMENT,
297297
timestamp1 datetime NOT NULL,
298298
timestamp2 date NOT NULL,
299299
timestamp3 time NOT NULL,
@@ -308,13 +308,14 @@ public function testShowCreateTableWithEmptyDatetimeDefault() {
308308
$results = $this->engine->get_query_results();
309309

310310
$this->assertEquals(
311-
"CREATE TABLE _tmp_table (
312-
`ID` bigint PRIMARY KEY AUTO_INCREMENT NOT NULL,
311+
"CREATE TABLE `_tmp_table` (
312+
`ID` bigint AUTO_INCREMENT,
313313
`timestamp1` datetime NOT NULL,
314314
`timestamp2` date NOT NULL,
315315
`timestamp3` time NOT NULL,
316316
`timestamp4` timestamp NOT NULL,
317-
`timestamp5` year NOT NULL
317+
`timestamp5` year NOT NULL,
318+
PRIMARY KEY (`ID`)
318319
);",
319320
$results[0]->{'Create Table'}
320321
);

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

+19-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,22 @@ 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 && '' !== $column->dflt_value && ! in_array( strtolower( $mysql_type ), array( 'datetime', 'date', 'time', 'timestamp', 'year' ), true ) ) {
3872+
return true;
3873+
}
3874+
3875+
return false;
3876+
}
3877+
38613878
/**
38623879
* Consumes data types from the query.
38633880
*

0 commit comments

Comments
 (0)