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 929d908 commit a18ec4e
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3352,16 +3352,14 @@ private function execute_show() {

foreach ( $columns as $column ) {
$column = (array) $column;
$mysql_type = $this->get_cached_mysql_data_type( $table_name, $column['Field'] );
$definition = '';
$definition .= '`' . $column['Field'] . '` ';
$definition .= $this->get_cached_mysql_data_type(
$table_name,
$column['Field']
) ?? $column['Type'];
$definition .= $mysql_type ?? $column['Type'];
$definition .= 'PRI' === $column['Key'] ? ' PRIMARY KEY' : '';
$definition .= 'PRI' === $column['Key'] && 'INTEGER' === $column['Type'] ? ' AUTO_INCREMENT' : '';
$definition .= 'NO' === $column['Null'] ? ' NOT NULL' : '';
$definition .= $column['Default'] ? ' DEFAULT ' . $column['Default'] : '';
$definition .= $this->get_column_default( $column, $mysql_type );
$entries[] = $definition;
}
foreach ( $keys as $key ) {
Expand Down Expand Up @@ -3533,6 +3531,21 @@ function ( $row ) use ( $name_map ) {
return $columns;
}

/**
* Gets the column default.
*
* @param array $column The table column
*
* @return string Prepared default value for the column.
*/
private function get_column_default( $column, $mysql_type ) {
if ( $column['Default'] && ! in_array( strtolower( $mysql_type ), array( 'datetime', 'date', 'time', 'timestamp', 'year' ), true ) ) {
return ' DEFAULT ' . $column['Default'];
} else {
return '';
}
}

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

0 comments on commit a18ec4e

Please sign in to comment.