Skip to content

Commit c1f9a42

Browse files
committed
Normalize zero defaults of temporal columns
MySQL stores a numeric zero default of a temporal column, such as DEFAULT 0 on a TIMESTAMP column, as its zero value, e.g. '0000-00-00 00:00:00'. The driver stored it verbatim, so SHOW CREATE TABLE emitted DEFAULT '0', which MySQL rejects in strict SQL mode. Normalize these defaults when recording column definitions, and repair defaults stored by older driver versions when the database is configured.
1 parent e88a817 commit c1f9a42

3 files changed

Lines changed: 238 additions & 7 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-configurator.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function configure_database(): void {
8282
$this->ensure_global_variables_table();
8383
$this->schema_builder->ensure_information_schema_tables();
8484
$this->schema_reconstructor->ensure_correct_information_schema();
85+
$this->schema_builder->normalize_temporal_zero_defaults();
8586
$this->save_current_driver_version();
8687
$this->ensure_database_data();
8788
} catch ( Throwable $e ) {

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php‎

Lines changed: 97 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5660,6 +5660,146 @@ public function testUnionAll(): void {
56605660
);
56615661
}
56625662

5663+
public function testShowCreateTableWithTemporalZeroDefaults(): void {
5664+
$this->assertQuery(
5665+
'CREATE TABLE _tmp_table (
5666+
col_timestamp TIMESTAMP NOT NULL DEFAULT 0,
5667+
col_datetime DATETIME NOT NULL DEFAULT 0,
5668+
col_date DATE NOT NULL DEFAULT 0,
5669+
col_time TIME NOT NULL DEFAULT 0,
5670+
col_year YEAR NOT NULL DEFAULT 0,
5671+
col_datetime_fsp DATETIME(3) NOT NULL DEFAULT 0,
5672+
col_timestamp_fsp TIMESTAMP(6) NOT NULL DEFAULT 0,
5673+
col_time_fsp TIME(2) NOT NULL DEFAULT 0,
5674+
col_decimal_zero DATETIME NOT NULL DEFAULT 0.0,
5675+
col_negative_zero DATETIME NOT NULL DEFAULT -0,
5676+
col_positive_zero DATETIME NOT NULL DEFAULT +0,
5677+
col_padded_zero DATETIME NOT NULL DEFAULT 00,
5678+
col_int INT NOT NULL DEFAULT 0
5679+
)'
5680+
);
5681+
5682+
$this->assertQuery( 'SHOW CREATE TABLE _tmp_table' );
5683+
$this->assertSame(
5684+
implode(
5685+
"\n",
5686+
array(
5687+
'CREATE TABLE `_tmp_table` (',
5688+
" `col_timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',",
5689+
" `col_datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
5690+
" `col_date` date NOT NULL DEFAULT '0000-00-00',",
5691+
" `col_time` time NOT NULL DEFAULT '00:00:00',",
5692+
" `col_year` year NOT NULL DEFAULT '0000',",
5693+
" `col_datetime_fsp` datetime(3) NOT NULL DEFAULT '0000-00-00 00:00:00.000',",
5694+
" `col_timestamp_fsp` timestamp(6) NOT NULL DEFAULT '0000-00-00 00:00:00.000000',",
5695+
" `col_time_fsp` time(2) NOT NULL DEFAULT '00:00:00.00',",
5696+
" `col_decimal_zero` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
5697+
" `col_negative_zero` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
5698+
" `col_positive_zero` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
5699+
" `col_padded_zero` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
5700+
" `col_int` int NOT NULL DEFAULT '0'",
5701+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
5702+
)
5703+
),
5704+
$this->last_result[0]->{'Create Table'}
5705+
);
5706+
5707+
$this->assertQuery(
5708+
"SELECT column_name, column_default FROM information_schema.columns WHERE table_name = '_tmp_table' AND column_name IN ('col_timestamp', 'col_date', 'col_time_fsp') ORDER BY ordinal_position"
5709+
);
5710+
$this->assertEquals(
5711+
array(
5712+
(object) array(
5713+
'COLUMN_NAME' => 'col_timestamp',
5714+
'COLUMN_DEFAULT' => '0000-00-00 00:00:00',
5715+
),
5716+
(object) array(
5717+
'COLUMN_NAME' => 'col_date',
5718+
'COLUMN_DEFAULT' => '0000-00-00',
5719+
),
5720+
(object) array(
5721+
'COLUMN_NAME' => 'col_time_fsp',
5722+
'COLUMN_DEFAULT' => '00:00:00.00',
5723+
),
5724+
),
5725+
$this->last_result
5726+
);
5727+
}
5728+
5729+
public function testInsertUsesTemporalZeroDefaults(): void {
5730+
$this->assertQuery(
5731+
'CREATE TABLE _tmp_table (
5732+
id INT NOT NULL,
5733+
created_at TIMESTAMP NOT NULL DEFAULT 0,
5734+
updated_on DATE NOT NULL DEFAULT 0
5735+
)'
5736+
);
5737+
$this->assertQuery( 'INSERT INTO _tmp_table (id) VALUES (1)' );
5738+
$this->assertQuery( 'SELECT created_at, updated_on FROM _tmp_table' );
5739+
$this->assertEquals(
5740+
array(
5741+
(object) array(
5742+
'created_at' => '0000-00-00 00:00:00',
5743+
'updated_on' => '0000-00-00',
5744+
),
5745+
),
5746+
$this->last_result
5747+
);
5748+
}
5749+
5750+
public function testAlterTableWithTemporalZeroDefaults(): void {
5751+
$this->assertQuery( 'CREATE TABLE _tmp_table (id INT NOT NULL, modified_at DATETIME NOT NULL)' );
5752+
$this->assertQuery( 'ALTER TABLE _tmp_table ADD COLUMN created_at TIMESTAMP NOT NULL DEFAULT 0' );
5753+
$this->assertQuery( 'ALTER TABLE _tmp_table MODIFY COLUMN modified_at DATETIME NOT NULL DEFAULT 0' );
5754+
5755+
$this->assertQuery( 'SHOW CREATE TABLE _tmp_table' );
5756+
$this->assertSame(
5757+
implode(
5758+
"\n",
5759+
array(
5760+
'CREATE TABLE `_tmp_table` (',
5761+
' `id` int NOT NULL,',
5762+
" `modified_at` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
5763+
" `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'",
5764+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
5765+
)
5766+
),
5767+
$this->last_result[0]->{'Create Table'}
5768+
);
5769+
}
5770+
5771+
public function testConfigurationNormalizesStoredTemporalZeroDefaults(): void {
5772+
$this->assertQuery( 'CREATE TABLE _tmp_table (id INT NOT NULL DEFAULT 0, created_at TIMESTAMP NOT NULL DEFAULT 0, label VARCHAR(20) NOT NULL DEFAULT 0)' );
5773+
5774+
// Simulate a database configured by an older driver version that
5775+
// stored numeric zero temporal defaults verbatim.
5776+
$this->sqlite->exec( "UPDATE _wp_sqlite_mysql_information_schema_columns SET column_default = '0' WHERE table_name = '_tmp_table'" );
5777+
$this->sqlite->exec( "UPDATE _wp_sqlite_global_variables SET value = '0.0.0' WHERE name = '_wp_sqlite_driver_version'" );
5778+
5779+
$this->engine = new WP_MySQL_On_SQLite(
5780+
'mysql-on-sqlite:dbname=wp',
5781+
null,
5782+
null,
5783+
array( 'sqlite_pdo' => $this->sqlite )
5784+
);
5785+
$this->engine->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
5786+
5787+
$this->assertQuery( 'SHOW CREATE TABLE _tmp_table' );
5788+
$this->assertSame(
5789+
implode(
5790+
"\n",
5791+
array(
5792+
'CREATE TABLE `_tmp_table` (',
5793+
" `id` int NOT NULL DEFAULT '0',",
5794+
" `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',",
5795+
" `label` varchar(20) NOT NULL DEFAULT '0'",
5796+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
5797+
)
5798+
),
5799+
$this->last_result[0]->{'Create Table'}
5800+
);
5801+
}
5802+
56635803
public function testShowCreateTableWithEmptyDatetimeDefault() {
56645804
$this->assertQuery(
56655805
"CREATE TABLE _tmp_table (

0 commit comments

Comments
 (0)