Skip to content

Commit b333f7d

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 4d5d83e commit b333f7d

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 ) {
@@ -318,6 +319,27 @@ private function save_current_driver_version(): void {
318319
);
319320
}
320321

322+
/**
323+
* Ensure that the information schema stores correct DATETIME_PRECISION values.
324+
*
325+
* Older driver versions stored 0 for all TIME, DATETIME, and TIMESTAMP
326+
* columns. The correct value is read from the column type, such as
327+
* "datetime(3)", and it is 0 for a column type without a precision.
328+
*/
329+
private function ensure_correct_datetime_precision(): void {
330+
// CAST() reads the leading integer, e.g., "3)" as 3, or returns 0 when there is none.
331+
$this->driver->execute_sqlite_query(
332+
sprintf(
333+
"UPDATE %s
334+
SET DATETIME_PRECISION = CAST(SUBSTR(COLUMN_TYPE, INSTR(COLUMN_TYPE, '(') + 1) AS INTEGER)
335+
WHERE DATA_TYPE IN ('time', 'datetime', 'timestamp')",
336+
$this->driver->get_connection()->quote_identifier(
337+
$this->schema_builder->get_table_name( false, 'columns' )
338+
)
339+
)
340+
);
341+
}
342+
321343
/**
322344
* Ensure that temporal columns store numeric zero defaults as zero values.
323345
*
@@ -332,19 +354,9 @@ private function ensure_temporal_zero_defaults(): void {
332354
$columns_table = $this->driver->get_connection()->quote_identifier(
333355
$this->schema_builder->get_table_name( false, 'columns' )
334356
);
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-
$columns = $this->driver->execute_sqlite_query(
357+
$columns = $this->driver->execute_sqlite_query(
341358
sprintf(
342-
"SELECT
343-
c.rowid,
344-
c.TABLE_NAME,
345-
c.DATA_TYPE,
346-
CAST(SUBSTR(c.COLUMN_TYPE, INSTR(c.COLUMN_TYPE, '(') + 1) AS INTEGER) AS DATETIME_PRECISION,
347-
p.dflt_value
359+
"SELECT c.rowid, c.TABLE_NAME, c.DATA_TYPE, c.DATETIME_PRECISION, p.dflt_value
348360
FROM %s AS c
349361
JOIN pragma_table_info(c.TABLE_NAME, 'main') AS p ON c.COLUMN_NAME = p.name
350362
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)