Skip to content

Commit 353d246

Browse files
committed
Run migrations only for databases from older driver versions
The repairs of DATETIME_PRECISION and temporal zero defaults ran on every driver upgrade, although they fix values that only older driver versions stored. Run both repairs only when the database was last configured by driver 3.1.1 or older, the last version that stored these values incorrectly. The saved version is read within the configuration transaction, before it is updated.
1 parent 813062f commit 353d246

2 files changed

Lines changed: 79 additions & 8 deletions

File tree

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,16 @@ public function configure_database(): void {
9292
$this->driver->execute_sqlite_query( 'BEGIN EXCLUSIVE TRANSACTION' );
9393
$transaction_started = true;
9494
$this->set_driver_in_transaction( true );
95+
96+
// Read the saved version within the transaction, before it's updated.
97+
$saved_version = $this->driver->get_saved_driver_version();
98+
9599
$this->ensure_global_variables_table();
96100
$this->schema_builder->ensure_information_schema_tables();
97101
$this->schema_reconstructor->ensure_correct_information_schema();
98-
$this->save_current_driver_version();
99102
$this->ensure_database_data();
100-
$this->ensure_correct_datetime_precision();
101-
$this->ensure_temporal_zero_defaults();
103+
$this->run_migrations( $saved_version );
104+
$this->save_current_driver_version();
102105
$this->driver->execute_sqlite_query( 'COMMIT' );
103106
} catch ( Throwable $e ) {
104107
// Roll back only the transaction started here, unless a failed
@@ -323,12 +326,28 @@ private function save_current_driver_version(): void {
323326
}
324327

325328
/**
326-
* Ensure that the information schema stores correct DATETIME_PRECISION values.
329+
* Migrate data stored by older driver versions.
330+
*
331+
* This is used when a new driver version needs to change some existing data.
332+
* Migrations can be version-gated so that they don't rerun on every update.
333+
*
334+
* @param string $saved_version The driver version that last configured the database.
335+
*/
336+
private function run_migrations( string $saved_version ): void {
337+
// Fix temporal precision and zero defaults stored by versions <= 3.1.1.
338+
if ( version_compare( $saved_version, '3.1.1', '<=' ) ) {
339+
$this->migrate_datetime_precision();
340+
$this->migrate_temporal_zero_defaults();
341+
}
342+
}
343+
344+
/**
345+
* Migrate DATETIME_PRECISION values stored by older driver versions.
327346
*
328347
* Older versions stored 0 regardless of fractional seconds precision.
329348
* The correct value is read from the column type, such as "datetime(3)".
330349
*/
331-
private function ensure_correct_datetime_precision(): void {
350+
private function migrate_datetime_precision(): void {
332351
// CAST() reads the leading integer, e.g., "3)" as 3, or returns 0 when there is none.
333352
$this->driver->execute_sqlite_query(
334353
sprintf(
@@ -343,12 +362,12 @@ private function ensure_correct_datetime_precision(): void {
343362
}
344363

345364
/**
346-
* Ensure that temporal columns store numeric zero defaults as zero values.
365+
* Migrate zero defaults of temporal columns stored by older driver versions.
347366
*
348367
* Older driver versions stored defaults such as "DEFAULT 0" of a TIMESTAMP
349368
* column verbatim, while MySQL stores them as a zero date/time value.
350369
*/
351-
private function ensure_temporal_zero_defaults(): void {
370+
private function migrate_temporal_zero_defaults(): void {
352371
$columns_table = $this->driver->get_connection()->quote_identifier(
353372
$this->schema_builder->get_table_name( false, 'columns' )
354373
);
@@ -368,7 +387,7 @@ private function ensure_temporal_zero_defaults(): void {
368387
array( WP_SQLite_Information_Schema_Builder::SAVED_DATABASE_NAME )
369388
)->fetchAll( PDO::FETCH_ASSOC );
370389

371-
// Normalize numeric zeros, such as 0, '0', '-0.0', or '0e0'.
390+
// Normalize zeros, such as 0, '0', -0.0, or 0e0.
372391
$table_columns = array();
373392
foreach ( $columns as $column ) {
374393
// SQLite returns the default as an SQL literal, e.g., 0, '0', or "0".

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6728,6 +6728,58 @@ public function testConfigurationNormalizesStoredTemporalZeroDefaults(): void {
67286728
$this->assertSame( '3', $this->last_result[0]->count );
67296729
}
67306730

6731+
/**
6732+
* @dataProvider migrationDriverVersions
6733+
*/
6734+
public function testConfigurationRunsMigrationsOnlyForOlderDriverVersions(
6735+
string $saved_version,
6736+
string $expected_default,
6737+
string $expected_precision
6738+
): void {
6739+
// Store the default verbatim and a zero precision, as older driver versions
6740+
// did, and apply them to the SQLite table.
6741+
$this->assertQuery( 'CREATE TABLE _tmp_table (created_at DATETIME(3) NOT NULL DEFAULT 0)' );
6742+
$this->sqlite->exec( "UPDATE _wp_sqlite_mysql_information_schema_columns SET column_default = '0', datetime_precision = 0 WHERE table_name = '_tmp_table'" );
6743+
$this->assertQuery( 'OPTIMIZE TABLE _tmp_table' );
6744+
6745+
// Configure the database as if it was last configured by the given driver version.
6746+
$this->sqlite->exec(
6747+
sprintf(
6748+
"UPDATE %s SET value = '%s' WHERE name = '%s'",
6749+
WP_MySQL_On_SQLite::GLOBAL_VARIABLES_TABLE_NAME,
6750+
$saved_version,
6751+
WP_MySQL_On_SQLite::DRIVER_VERSION_VARIABLE_NAME
6752+
)
6753+
);
6754+
$configurator = new WP_SQLite_Configurator(
6755+
$this->engine,
6756+
new WP_SQLite_Information_Schema_Builder( WP_MySQL_On_SQLite::RESERVED_PREFIX, $this->engine->get_connection() )
6757+
);
6758+
$configurator->configure_database();
6759+
6760+
$this->assertQuery( 'SHOW CREATE TABLE _tmp_table' );
6761+
$this->assertSame(
6762+
implode(
6763+
"\n",
6764+
array(
6765+
'CREATE TABLE `_tmp_table` (',
6766+
" `created_at` datetime(3) NOT NULL DEFAULT '$expected_default'",
6767+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
6768+
)
6769+
),
6770+
$this->last_result[0]->{'Create Table'}
6771+
);
6772+
$this->assertQuery( "SELECT datetime_precision FROM information_schema.columns WHERE table_name = '_tmp_table'" );
6773+
$this->assertSame( $expected_precision, $this->last_result[0]->DATETIME_PRECISION );
6774+
}
6775+
6776+
public static function migrationDriverVersions(): array {
6777+
return array(
6778+
'migrated from 3.1.1' => array( '3.1.1', '0000-00-00 00:00:00.000', '3' ),
6779+
'not migrated from 3.1.2' => array( '3.1.2', '0', '0' ),
6780+
);
6781+
}
6782+
67316783
public function testConfigurationNormalizesStoredTemporalZeroDefaultsOfReferencedTable(): void {
67326784
$this->assertQuery( 'CREATE TABLE _tmp_parent (id INT PRIMARY KEY, created_at TIMESTAMP NOT NULL DEFAULT 0)' );
67336785
$this->assertQuery( 'CREATE TABLE _tmp_cascade (id INT PRIMARY KEY, parent_id INT, FOREIGN KEY (parent_id) REFERENCES _tmp_parent (id) ON DELETE CASCADE)' );

0 commit comments

Comments
 (0)