Skip to content

Commit 86b43fd

Browse files
committed
Fxi support for valid date field default values
1 parent e81a812 commit 86b43fd

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

tests/WP_SQLite_Translator_Tests.php

+15-5
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,19 @@ public function testShowCreateTable1() {
292292

293293
public function testShowCreateTableWithEmptyDatetimeDefault() {
294294
$this->assertQuery(
295-
'CREATE TABLE _tmp_table (
295+
"CREATE TABLE _tmp_table (
296296
ID BIGINT PRIMARY KEY AUTO_INCREMENT,
297297
timestamp1 datetime NOT NULL,
298298
timestamp2 date NOT NULL,
299299
timestamp3 time NOT NULL,
300300
timestamp4 timestamp NOT NULL,
301-
timestamp5 year NOT NULL
302-
);'
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+
);"
303308
);
304309

305310
$this->assertQuery(
@@ -308,15 +313,20 @@ public function testShowCreateTableWithEmptyDatetimeDefault() {
308313
$results = $this->engine->get_query_results();
309314

310315
$this->assertEquals(
311-
'CREATE TABLE `_tmp_table` (
316+
"CREATE TABLE `_tmp_table` (
312317
`ID` bigint AUTO_INCREMENT,
313318
`timestamp1` datetime NOT NULL,
314319
`timestamp2` date NOT NULL,
315320
`timestamp3` time NOT NULL,
316321
`timestamp4` timestamp NOT NULL,
317322
`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',
318328
PRIMARY KEY (`ID`)
319-
);',
329+
);",
320330
$results[0]->{'Create Table'}
321331
);
322332
}

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

+14-3
Original file line numberDiff line numberDiff line change
@@ -3868,11 +3868,22 @@ function ( $row ) use ( $name_map ) {
38683868
* @return boolean If column should have a default definition.
38693869
*/
38703870
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;
3871+
if ( null === $column->dflt_value ) {
3872+
return false;
38733873
}
38743874

3875-
return false;
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;
38763887
}
38773888

38783889
/**

0 commit comments

Comments
 (0)