From a18ec4eff2479f3a5966d558e005dfb25d3b9923 Mon Sep 17 00:00:00 2001 From: Wojtek Naruniec Date: Wed, 11 Dec 2024 17:35:29 +0100 Subject: [PATCH] Ensure that for empty default date fields there is no default --- .../sqlite/class-wp-sqlite-translator.php | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/wp-includes/sqlite/class-wp-sqlite-translator.php b/wp-includes/sqlite/class-wp-sqlite-translator.php index ac6a170c..c07967b1 100644 --- a/wp-includes/sqlite/class-wp-sqlite-translator.php +++ b/wp-includes/sqlite/class-wp-sqlite-translator.php @@ -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 ) { @@ -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. *