@@ -413,6 +413,46 @@ public function ensure_information_schema_tables(): void {
413413 }
414414 }
415415
416+ /**
417+ * Normalize numeric zero defaults of temporal columns in the information schema.
418+ *
419+ * Older driver versions stored defaults such as "DEFAULT 0" of a TIMESTAMP
420+ * column verbatim. MySQL stores them as a canonical zero value, such as
421+ * "0000-00-00 00:00:00", and "SHOW CREATE TABLE" must emit that value for
422+ * the output to be accepted by MySQL in strict SQL mode.
423+ */
424+ public function normalize_temporal_zero_defaults (): void {
425+ $ columns_table = $ this ->connection ->quote_identifier ( $ this ->get_table_name ( false , 'columns ' ) );
426+ $ columns = $ this ->connection ->query (
427+ sprintf (
428+ "SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, COLUMN_TYPE, COLUMN_DEFAULT
429+ FROM %s
430+ WHERE DATA_TYPE IN ('date', 'time', 'datetime', 'timestamp', 'year')
431+ AND COLUMN_DEFAULT IS NOT NULL " ,
432+ $ columns_table
433+ )
434+ )->fetchAll ( PDO ::FETCH_ASSOC ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
435+
436+ foreach ( $ columns as $ column ) {
437+ $ default = $ this ->get_temporal_zero_default (
438+ (string ) $ column ['COLUMN_DEFAULT ' ],
439+ $ column ['DATA_TYPE ' ],
440+ $ column ['COLUMN_TYPE ' ]
441+ );
442+ if ( null === $ default || $ default === $ column ['COLUMN_DEFAULT ' ] ) {
443+ continue ;
444+ }
445+
446+ $ this ->connection ->query (
447+ sprintf (
448+ 'UPDATE %s SET COLUMN_DEFAULT = ? WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND COLUMN_NAME = ? ' ,
449+ $ columns_table
450+ ),
451+ array ( $ default , $ column ['TABLE_SCHEMA ' ], $ column ['TABLE_NAME ' ], $ column ['COLUMN_NAME ' ] )
452+ );
453+ }
454+ }
455+
416456 /**
417457 * Get the definition and data of a computed information schema table.
418458 *
@@ -1521,7 +1561,7 @@ private function record_drop_check_constraint(
15211561 private function extract_column_data ( string $ table_name , string $ column_name , WP_Parser_Node $ node , int $ position ): array {
15221562 list ( $ data_type , $ column_type ) = $ this ->get_column_data_types ( $ node );
15231563
1524- $ default = $ this ->get_column_default ( $ node , $ data_type , $ column_name );
1564+ $ default = $ this ->get_column_default ( $ node , $ data_type , $ column_name, $ column_type );
15251565 $ nullable = $ this ->get_column_nullable ( $ node );
15261566 $ key = $ this ->get_column_key ( $ node );
15271567 $ extra = $ this ->get_column_extra ( $ node );
@@ -2066,9 +2106,10 @@ private function get_table_comment( WP_Parser_Node $node ): string {
20662106 * @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
20672107 * @param string $data_type The column data type as stored in information schema.
20682108 * @param string $column_name The column name.
2109+ * @param string $column_type The column type as stored in information schema.
20692110 * @return string|null The column default as stored in information schema.
20702111 */
2071- private function get_column_default ( WP_Parser_Node $ node , string $ data_type , string $ column_name ): ?string {
2112+ private function get_column_default ( WP_Parser_Node $ node , string $ data_type , string $ column_name, string $ column_type ): ?string {
20722113 $ default_attr = null ;
20732114 foreach ( $ node ->get_descendant_nodes ( 'columnAttribute ' ) as $ attr ) {
20742115 if ( $ attr ->has_child_token ( WP_MySQL_Lexer::DEFAULT_SYMBOL ) ) {
@@ -2100,9 +2141,11 @@ private function get_column_default( WP_Parser_Node $node, string $data_type, st
21002141 $ literal = $ signed_literal ->get_first_child_node ( 'literal ' );
21012142 if ( null === $ literal ) {
21022143 // A signed number, such as "-5", has no "literal" child node.
2103- return $ this ->get_value ( $ signed_literal );
2144+ $ default = $ this ->get_value ( $ signed_literal );
2145+ $ zero_default = $ this ->get_temporal_zero_default ( $ default , $ data_type , $ column_type );
2146+ return $ zero_default ?? $ default ;
21042147 }
2105- return $ this ->get_literal_default ( $ literal , $ data_type , $ column_name );
2148+ return $ this ->get_literal_default ( $ literal , $ data_type , $ column_name, $ column_type );
21062149 }
21072150
21082151 // DEFAULT (expression) - MySQL 8.0.13+ supports exprWithParentheses
@@ -2120,9 +2163,10 @@ private function get_column_default( WP_Parser_Node $node, string $data_type, st
21202163 * @param WP_Parser_Node $literal The "literal" AST node.
21212164 * @param string $data_type The column data type as stored in information schema.
21222165 * @param string $column_name The column name.
2166+ * @param string $column_type The column type as stored in information schema.
21232167 * @return string|null The default value as stored in information schema.
21242168 */
2125- private function get_literal_default ( WP_Parser_Node $ literal , string $ data_type , string $ column_name ): ?string {
2169+ private function get_literal_default ( WP_Parser_Node $ literal , string $ data_type , string $ column_name, string $ column_type ): ?string {
21262170 // DEFAULT NULL
21272171 if ( $ literal ->has_child_node ( 'nullLiteral ' ) ) {
21282172 return null ;
@@ -2137,6 +2181,12 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
21372181
21382182 $ default = $ this ->get_value ( $ literal );
21392183
2184+ // Numeric zero temporal defaults, such as "DEFAULT 0" of a DATETIME column.
2185+ $ zero_default = $ this ->get_temporal_zero_default ( $ default , $ data_type , $ column_type );
2186+ if ( null !== $ zero_default ) {
2187+ return $ zero_default ;
2188+ }
2189+
21402190 if ( 'bit ' === $ data_type ) {
21412191 /*
21422192 * @TODO: Validate and normalize defaults from their AST and the full
@@ -2150,8 +2200,9 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
21502200 }
21512201
21522202 /*
2153- * @TODO: Non-BIT literal defaults are currently stored verbatim. To
2154- * match MySQL, they should be normalized. For example:
2203+ * @TODO: Other non-BIT literal defaults (apart from numeric zero temporal
2204+ * defaults handled above) are currently stored verbatim. To match MySQL,
2205+ * they should be normalized. For example:
21552206 * - BINARY/VARBINARY defaults stored as 0x hex literals.
21562207 * - Hex and bit literals coerced to raw bytes for text columns and to
21572208 * their decimal value for numeric columns.
@@ -2161,6 +2212,45 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
21612212 return $ default ;
21622213 }
21632214
2215+ /**
2216+ * Normalize a numeric zero default of a temporal column to its MySQL zero value.
2217+ *
2218+ * MySQL stores "DEFAULT 0" of a DATETIME column as "0000-00-00 00:00:00",
2219+ * and it rejects the quoted form "DEFAULT '0'" in strict SQL mode. Other
2220+ * temporal defaults are not handled here.
2221+ *
2222+ * @param string $default_value The default value.
2223+ * @param string $data_type The column data type as stored in information schema.
2224+ * @param string $column_type The column type as stored in information schema.
2225+ * @return string|null The zero value, or null when the default is not a numeric zero
2226+ * or the column is not a temporal column.
2227+ */
2228+ private function get_temporal_zero_default ( string $ default_value , string $ data_type , string $ column_type ): ?string {
2229+ if ( ! preg_match ( '/\A[+-]?(?:0+(?:\.0*)?|\.0+)\z/ ' , $ default_value ) ) {
2230+ return null ;
2231+ }
2232+
2233+ // Fractional seconds precision, e.g., "datetime(3)".
2234+ $ fraction = '' ;
2235+ if ( preg_match ( '/\((\d+)\)/ ' , $ column_type , $ matches ) && (int ) $ matches [1 ] > 0 ) {
2236+ $ fraction = '. ' . str_repeat ( '0 ' , (int ) $ matches [1 ] );
2237+ }
2238+
2239+ switch ( $ data_type ) {
2240+ case 'date ' :
2241+ return '0000-00-00 ' ;
2242+ case 'year ' :
2243+ return '0000 ' ;
2244+ case 'time ' :
2245+ return '00:00:00 ' . $ fraction ;
2246+ case 'datetime ' :
2247+ case 'timestamp ' :
2248+ return '0000-00-00 00:00:00 ' . $ fraction ;
2249+ default :
2250+ return null ;
2251+ }
2252+ }
2253+
21642254 /**
21652255 * Normalize a BIT default value to a MySQL bit literal.
21662256 *
0 commit comments