Skip to content

Commit 6b7f989

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 fe98e62 commit 6b7f989

2 files changed

Lines changed: 57 additions & 12 deletions

File tree

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public function configure_database(): void {
9595
$this->schema_reconstructor->ensure_correct_information_schema();
9696
$this->save_current_driver_version();
9797
$this->ensure_database_data();
98+
$this->ensure_correct_datetime_precision();
9899
$this->ensure_temporal_zero_defaults();
99100
$this->driver->execute_sqlite_query( 'COMMIT' );
100101
} catch ( Throwable $e ) {
@@ -314,6 +315,27 @@ private function save_current_driver_version(): void {
314315
);
315316
}
316317

318+
/**
319+
* Ensure that the information schema stores correct DATETIME_PRECISION values.
320+
*
321+
* Older driver versions stored 0 for all TIME, DATETIME, and TIMESTAMP
322+
* columns. The correct value is read from the column type, such as
323+
* "datetime(3)", and it is 0 for a column type without a precision.
324+
*/
325+
private function ensure_correct_datetime_precision(): void {
326+
// CAST() reads the leading integer, e.g., "3)" as 3, or returns 0 when there is none.
327+
$this->driver->execute_sqlite_query(
328+
sprintf(
329+
"UPDATE %s
330+
SET DATETIME_PRECISION = CAST(SUBSTR(COLUMN_TYPE, INSTR(COLUMN_TYPE, '(') + 1) AS INTEGER)
331+
WHERE DATA_TYPE IN ('time', 'datetime', 'timestamp')",
332+
$this->driver->get_connection()->quote_identifier(
333+
$this->schema_builder->get_table_name( false, 'columns' )
334+
)
335+
)
336+
);
337+
}
338+
317339
/**
318340
* Ensure that temporal columns store numeric zero defaults as zero values.
319341
*
@@ -324,19 +346,9 @@ private function ensure_temporal_zero_defaults(): void {
324346
$columns_table = $this->driver->get_connection()->quote_identifier(
325347
$this->schema_builder->get_table_name( false, 'columns' )
326348
);
327-
/*
328-
* Older driver versions stored a DATETIME_PRECISION of 0, so the precision
329-
* is read from the column type, such as "datetime(3)". CAST() reads the
330-
* leading integer, e.g., "3)" as 3, or returns 0 when there is none.
331-
*/
332-
$columns = $this->driver->execute_sqlite_query(
349+
$columns = $this->driver->execute_sqlite_query(
333350
sprintf(
334-
"SELECT
335-
rowid,
336-
TABLE_NAME,
337-
DATA_TYPE,
338-
CAST(SUBSTR(COLUMN_TYPE, INSTR(COLUMN_TYPE, '(') + 1) AS INTEGER) AS DATETIME_PRECISION,
339-
COLUMN_DEFAULT
351+
"SELECT rowid, TABLE_NAME, DATA_TYPE, DATETIME_PRECISION, COLUMN_DEFAULT
340352
FROM %s
341353
WHERE TABLE_SCHEMA = ?
342354
AND DATA_TYPE IN ('date', 'time', 'datetime', 'timestamp')

‎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
@@ -5743,6 +5743,39 @@ public function testInformationSchemaColumnsDatetimePrecision(): void {
57435743
);
57445744
}
57455745

5746+
public function testConfigurationRepairsStoredDatetimePrecision(): void {
5747+
$this->assertQuery( 'CREATE TABLE _tmp_table (col_date DATE, col_time TIME(2), col_datetime DATETIME, col_timestamp TIMESTAMP(6) NULL)' );
5748+
5749+
// Older driver versions stored a zero precision for all temporal columns.
5750+
$this->sqlite->exec( "UPDATE _wp_sqlite_mysql_information_schema_columns SET datetime_precision = 0 WHERE data_type IN ('time', 'datetime', 'timestamp')" );
5751+
$this->reconnectAfterDriverUpgrade();
5752+
5753+
$this->assertQuery(
5754+
"SELECT column_name, datetime_precision FROM information_schema.columns WHERE table_name = '_tmp_table' ORDER BY ordinal_position"
5755+
);
5756+
$this->assertEquals(
5757+
array(
5758+
(object) array(
5759+
'COLUMN_NAME' => 'col_date',
5760+
'DATETIME_PRECISION' => null,
5761+
),
5762+
(object) array(
5763+
'COLUMN_NAME' => 'col_time',
5764+
'DATETIME_PRECISION' => '2',
5765+
),
5766+
(object) array(
5767+
'COLUMN_NAME' => 'col_datetime',
5768+
'DATETIME_PRECISION' => '0',
5769+
),
5770+
(object) array(
5771+
'COLUMN_NAME' => 'col_timestamp',
5772+
'DATETIME_PRECISION' => '6',
5773+
),
5774+
),
5775+
$this->last_result
5776+
);
5777+
}
5778+
57465779
public function testShowCreateTableWithTemporalZeroDefaults(): void {
57475780
$this->assertQuery(
57485781
'CREATE TABLE _tmp_table (

0 commit comments

Comments
 (0)