Skip to content

Commit eb4b5e1

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 a2848be commit eb4b5e1

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
@@ -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,27 @@ 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 driver versions stored 0 for all TIME, DATETIME, and TIMESTAMP
329+
* columns. The correct value is read from the column type, such as
330+
* "datetime(3)", and it is 0 for a column type without a precision.
331+
*/
332+
private function ensure_correct_datetime_precision(): void {
333+
// CAST() reads the leading integer, e.g., "3)" as 3, or returns 0 when there is none.
334+
$this->driver->execute_sqlite_query(
335+
sprintf(
336+
"UPDATE %s
337+
SET DATETIME_PRECISION = CAST(SUBSTR(COLUMN_TYPE, INSTR(COLUMN_TYPE, '(') + 1) AS INTEGER)
338+
WHERE DATA_TYPE IN ('time', 'datetime', 'timestamp')",
339+
$this->driver->get_connection()->quote_identifier(
340+
$this->schema_builder->get_table_name( false, 'columns' )
341+
)
342+
)
343+
);
344+
}
345+
324346
/**
325347
* Ensure that temporal columns store numeric zero defaults as zero values.
326348
*
@@ -335,19 +357,9 @@ private function ensure_temporal_zero_defaults(): void {
335357
$columns_table = $this->driver->get_connection()->quote_identifier(
336358
$this->schema_builder->get_table_name( false, 'columns' )
337359
);
338-
/*
339-
* Older driver versions stored a DATETIME_PRECISION of 0, so the precision
340-
* is read from the column type, such as "datetime(3)". CAST() reads the
341-
* leading integer, e.g., "3)" as 3, or returns 0 when there is none.
342-
*/
343-
$columns = $this->driver->execute_sqlite_query(
360+
$columns = $this->driver->execute_sqlite_query(
344361
sprintf(
345-
"SELECT
346-
c.rowid,
347-
c.TABLE_NAME,
348-
c.DATA_TYPE,
349-
CAST(SUBSTR(c.COLUMN_TYPE, INSTR(c.COLUMN_TYPE, '(') + 1) AS INTEGER) AS DATETIME_PRECISION,
350-
p.dflt_value
362+
"SELECT c.rowid, c.TABLE_NAME, c.DATA_TYPE, c.DATETIME_PRECISION, p.dflt_value
351363
FROM %s AS c
352364
JOIN pragma_table_info(c.TABLE_NAME, 'main') AS p ON c.COLUMN_NAME = p.name
353365
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
@@ -5753,6 +5753,39 @@ public function testInformationSchemaColumnsDatetimePrecision(): void {
57535753
);
57545754
}
57555755

5756+
public function testConfigurationRepairsStoredDatetimePrecision(): void {
5757+
$this->assertQuery( 'CREATE TABLE _tmp_table (col_date DATE, col_time TIME(2), col_datetime DATETIME, col_timestamp TIMESTAMP(6) NULL)' );
5758+
5759+
// Older driver versions stored a zero precision for all temporal columns.
5760+
$this->sqlite->exec( "UPDATE _wp_sqlite_mysql_information_schema_columns SET datetime_precision = 0 WHERE data_type IN ('time', 'datetime', 'timestamp')" );
5761+
$this->reconnectAfterDriverUpgrade();
5762+
5763+
$this->assertQuery(
5764+
"SELECT column_name, datetime_precision FROM information_schema.columns WHERE table_name = '_tmp_table' ORDER BY ordinal_position"
5765+
);
5766+
$this->assertEquals(
5767+
array(
5768+
(object) array(
5769+
'COLUMN_NAME' => 'col_date',
5770+
'DATETIME_PRECISION' => null,
5771+
),
5772+
(object) array(
5773+
'COLUMN_NAME' => 'col_time',
5774+
'DATETIME_PRECISION' => '2',
5775+
),
5776+
(object) array(
5777+
'COLUMN_NAME' => 'col_datetime',
5778+
'DATETIME_PRECISION' => '0',
5779+
),
5780+
(object) array(
5781+
'COLUMN_NAME' => 'col_timestamp',
5782+
'DATETIME_PRECISION' => '6',
5783+
),
5784+
),
5785+
$this->last_result
5786+
);
5787+
}
5788+
57565789
public function testShowCreateTableWithTemporalZeroDefaults(): void {
57575790
$this->assertQuery(
57585791
'CREATE TABLE _tmp_table (

0 commit comments

Comments
 (0)