Skip to content

Commit 813062f

Browse files
committed
Repair DATETIME_PRECISION stored by older driver versions
Older driver versions read the precision from the whole "(3)" node, which casts to 0, so INFORMATION_SCHEMA.COLUMNS reported a DATETIME_PRECISION of 0 for every TIME(n), DATETIME(n), and TIMESTAMP(n) column. Repair the stored values when configuring the database, using the precision in COLUMN_TYPE. The repair of temporal zero defaults now runs after it, and it reads the repaired DATETIME_PRECISION instead of parsing COLUMN_TYPE itself.
1 parent 0116e18 commit 813062f

2 files changed

Lines changed: 55 additions & 12 deletions

File tree

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function configure_database(): void {
9797
$this->schema_reconstructor->ensure_correct_information_schema();
9898
$this->save_current_driver_version();
9999
$this->ensure_database_data();
100+
$this->ensure_correct_datetime_precision();
100101
$this->ensure_temporal_zero_defaults();
101102
$this->driver->execute_sqlite_query( 'COMMIT' );
102103
} catch ( Throwable $e ) {
@@ -321,6 +322,26 @@ private function save_current_driver_version(): void {
321322
);
322323
}
323324

325+
/**
326+
* Ensure that the information schema stores correct DATETIME_PRECISION values.
327+
*
328+
* Older versions stored 0 regardless of fractional seconds precision.
329+
* The correct value is read from the column type, such as "datetime(3)".
330+
*/
331+
private function ensure_correct_datetime_precision(): void {
332+
// CAST() reads the leading integer, e.g., "3)" as 3, or returns 0 when there is none.
333+
$this->driver->execute_sqlite_query(
334+
sprintf(
335+
"UPDATE %s
336+
SET DATETIME_PRECISION = CAST(SUBSTR(COLUMN_TYPE, INSTR(COLUMN_TYPE, '(') + 1) AS INTEGER)
337+
WHERE DATA_TYPE IN ('time', 'datetime', 'timestamp')",
338+
$this->driver->get_connection()->quote_identifier(
339+
$this->schema_builder->get_table_name( false, 'columns' )
340+
)
341+
)
342+
);
343+
}
344+
324345
/**
325346
* Ensure that temporal columns store numeric zero defaults as zero values.
326347
*
@@ -332,21 +353,10 @@ private function ensure_temporal_zero_defaults(): void {
332353
$this->schema_builder->get_table_name( false, 'columns' )
333354
);
334355

335-
/*
336-
* Older driver versions stored a DATETIME_PRECISION of 0, so the precision
337-
* is read from the column type, such as "datetime(3)". CAST() reads the
338-
* leading integer, e.g., "3)" as 3, or returns 0 when there is none.
339-
*/
340-
341356
// Read SQLite defaults; reconstructed metadata may already be normalized.
342357
$columns = $this->driver->execute_sqlite_query(
343358
sprintf(
344-
"SELECT
345-
c.TABLE_NAME,
346-
c.COLUMN_NAME,
347-
c.DATA_TYPE,
348-
CAST(SUBSTR(c.COLUMN_TYPE, INSTR(c.COLUMN_TYPE, '(') + 1) AS INTEGER) AS DATETIME_PRECISION,
349-
p.dflt_value
359+
"SELECT c.TABLE_NAME, c.COLUMN_NAME, c.DATA_TYPE, c.DATETIME_PRECISION, p.dflt_value
350360
FROM %s AS c
351361
JOIN pragma_table_info(c.TABLE_NAME, 'main') AS p ON c.COLUMN_NAME = p.name
352362
WHERE c.TABLE_SCHEMA = ?

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6426,6 +6426,39 @@ public function testInformationSchemaColumnsDatetimePrecision(): void {
64266426
);
64276427
}
64286428

6429+
public function testConfigurationRepairsStoredDatetimePrecision(): void {
6430+
$this->assertQuery( 'CREATE TABLE _tmp_table (col_date DATE, col_time TIME(2), col_datetime DATETIME, col_timestamp TIMESTAMP(6) NULL)' );
6431+
6432+
// Older driver versions stored a zero precision for all temporal columns.
6433+
$this->sqlite->exec( "UPDATE _wp_sqlite_mysql_information_schema_columns SET datetime_precision = 0 WHERE data_type IN ('time', 'datetime', 'timestamp')" );
6434+
$this->reconnectAfterDriverUpgrade();
6435+
6436+
$this->assertQuery(
6437+
"SELECT column_name, datetime_precision FROM information_schema.columns WHERE table_name = '_tmp_table' ORDER BY ordinal_position"
6438+
);
6439+
$this->assertEquals(
6440+
array(
6441+
(object) array(
6442+
'COLUMN_NAME' => 'col_date',
6443+
'DATETIME_PRECISION' => null,
6444+
),
6445+
(object) array(
6446+
'COLUMN_NAME' => 'col_time',
6447+
'DATETIME_PRECISION' => '2',
6448+
),
6449+
(object) array(
6450+
'COLUMN_NAME' => 'col_datetime',
6451+
'DATETIME_PRECISION' => '0',
6452+
),
6453+
(object) array(
6454+
'COLUMN_NAME' => 'col_timestamp',
6455+
'DATETIME_PRECISION' => '6',
6456+
),
6457+
),
6458+
$this->last_result
6459+
);
6460+
}
6461+
64296462
public function testShowCreateTableWithTemporalZeroDefaults(): void {
64306463
$this->assertQuery(
64316464
'CREATE TABLE _tmp_table (

0 commit comments

Comments
 (0)