Skip to content

Commit

Permalink
Ensure that for empty default date fields there is no default
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekn committed Dec 11, 2024
1 parent 674bb6f commit 7835a5e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
9 changes: 5 additions & 4 deletions tests/WP_SQLite_Translator_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public function testShowCreateTable1() {
public function testShowCreateTableWithEmptyDatetimeDefault() {
$this->assertQuery(
"CREATE TABLE _tmp_table (

Check failure on line 295 in tests/WP_SQLite_Translator_Tests.php

View workflow job for this annotation

GitHub Actions / Check code style

String "CREATE TABLE _tmp_table (\n ID BIGINT PRIMARY KEY AUTO_INCREMENT,\n timestamp1 datetime NOT NULL,\n timestamp2 date NOT NULL,\n timestamp3 time NOT NULL,\n timestamp4 timestamp NOT NULL,\n timestamp5 year NOT NULL\n );" does not require double quotes; use single quotes instead
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
ID BIGINT PRIMARY KEY AUTO_INCREMENT,
timestamp1 datetime NOT NULL,
timestamp2 date NOT NULL,
timestamp3 time NOT NULL,
Expand All @@ -308,13 +308,14 @@ public function testShowCreateTableWithEmptyDatetimeDefault() {
$results = $this->engine->get_query_results();

$this->assertEquals(
"CREATE TABLE _tmp_table (
`ID` bigint PRIMARY KEY AUTO_INCREMENT NOT NULL,
"CREATE TABLE `_tmp_table` (

Check failure on line 311 in tests/WP_SQLite_Translator_Tests.php

View workflow job for this annotation

GitHub Actions / Check code style

String "CREATE TABLE `_tmp_table` (\n `ID` bigint AUTO_INCREMENT,\n `timestamp1` datetime NOT NULL,\n `timestamp2` date NOT NULL,\n `timestamp3` time NOT NULL,\n `timestamp4` timestamp NOT NULL,\n `timestamp5` year NOT NULL,\n PRIMARY KEY (`ID`)\n);" does not require double quotes; use single quotes instead
`ID` bigint AUTO_INCREMENT,
`timestamp1` datetime NOT NULL,
`timestamp2` date NOT NULL,
`timestamp3` time NOT NULL,
`timestamp4` timestamp NOT NULL,
`timestamp5` year NOT NULL
`timestamp5` year NOT NULL,
PRIMARY KEY (`ID`)
);",
$results[0]->{'Create Table'}
);
Expand Down
21 changes: 19 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 );

Check warning on line 3696 in wp-includes/sqlite/class-wp-sqlite-translator.php

View workflow job for this annotation

GitHub Actions / Check code style

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
$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,22 @@ 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 ) {

Check failure on line 3870 in wp-includes/sqlite/class-wp-sqlite-translator.php

View workflow job for this annotation

GitHub Actions / Check code style

Expected 1 space before opening brace; found 2
if ( null !== $column->dflt_value && '' !== $column->dflt_value && ! in_array( strtolower( $mysql_type ), array( 'datetime', 'date', 'time', 'timestamp', 'year' ), true ) ) {
return true;
}

return false;
}

/**
* Consumes data types from the query.
*
Expand Down

0 comments on commit 7835a5e

Please sign in to comment.