Skip to content

Commit

Permalink
Fix datetime field with empty default (#166)
Browse files Browse the repository at this point in the history
Fixes
#165

I propose to fix an issue where dumping the datetime field that has
empty value results with producing incorrect MySQL.
  • Loading branch information
JanJakes authored Dec 19, 2024
2 parents ac75e90 + 86b43fd commit e1931d2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
41 changes: 41 additions & 0 deletions tests/WP_SQLite_Translator_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
32 changes: 30 additions & 2 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit e1931d2

Please sign in to comment.