Skip to content

Commit eb3ee6f

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. The zero default repair can't tell a numeric zero from a string zero in stored defaults, so running it again would change string zero defaults that the current driver stores verbatim on purpose. Run both repairs only when the database was last configured by driver 3.1.0 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 6b7f989 commit eb3ee6f

2 files changed

Lines changed: 61 additions & 6 deletions

File tree

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,16 @@ public function configure_database(): void {
9090
// from attempting to configure the database at the same time.
9191
$this->driver->execute_sqlite_query( 'BEGIN EXCLUSIVE TRANSACTION' );
9292
$this->set_driver_in_transaction( true );
93+
94+
// Read the saved version within the transaction, before it's updated.
95+
$saved_version = $this->driver->get_saved_driver_version();
96+
9397
$this->ensure_global_variables_table();
9498
$this->schema_builder->ensure_information_schema_tables();
9599
$this->schema_reconstructor->ensure_correct_information_schema();
96100
$this->save_current_driver_version();
97101
$this->ensure_database_data();
98-
$this->ensure_correct_datetime_precision();
99-
$this->ensure_temporal_zero_defaults();
102+
$this->run_migrations( $saved_version );
100103
$this->driver->execute_sqlite_query( 'COMMIT' );
101104
} catch ( Throwable $e ) {
102105
// The transaction is not active when BEGIN failed, or when a failed
@@ -316,13 +319,29 @@ private function save_current_driver_version(): void {
316319
}
317320

318321
/**
319-
* Ensure that the information schema stores correct DATETIME_PRECISION values.
322+
* Migrate data stored by older driver versions.
323+
*
324+
* This is used when a new driver version needs to change some existing data.
325+
* Migrations can be version-gated so that they don't rerun on every update.
326+
*
327+
* @param string $saved_version The driver version that last configured the database.
328+
*/
329+
private function run_migrations( string $saved_version ): void {
330+
// Fix temporal precision and zero defaults stored by versions <= 3.1.0.
331+
if ( version_compare( $saved_version, '3.1.0', '<=' ) ) {
332+
$this->migrate_datetime_precision();
333+
$this->migrate_temporal_zero_defaults();
334+
}
335+
}
336+
337+
/**
338+
* Migrate DATETIME_PRECISION values stored by older driver versions.
320339
*
321340
* Older driver versions stored 0 for all TIME, DATETIME, and TIMESTAMP
322341
* columns. The correct value is read from the column type, such as
323342
* "datetime(3)", and it is 0 for a column type without a precision.
324343
*/
325-
private function ensure_correct_datetime_precision(): void {
344+
private function migrate_datetime_precision(): void {
326345
// CAST() reads the leading integer, e.g., "3)" as 3, or returns 0 when there is none.
327346
$this->driver->execute_sqlite_query(
328347
sprintf(
@@ -337,12 +356,12 @@ private function ensure_correct_datetime_precision(): void {
337356
}
338357

339358
/**
340-
* Ensure that temporal columns store numeric zero defaults as zero values.
359+
* Migrate numeric zero defaults of temporal columns stored by older driver versions.
341360
*
342361
* Older driver versions stored defaults such as "DEFAULT 0" of a TIMESTAMP
343362
* column verbatim, while MySQL stores them as a zero date/time value.
344363
*/
345-
private function ensure_temporal_zero_defaults(): void {
364+
private function migrate_temporal_zero_defaults(): void {
346365
$columns_table = $this->driver->get_connection()->quote_identifier(
347366
$this->schema_builder->get_table_name( false, 'columns' )
348367
);

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6010,6 +6010,42 @@ public function testConfigurationNormalizesStoredTemporalZeroDefaults(): void {
60106010
$this->assertSame( '3', $this->last_result[0]->count );
60116011
}
60126012

6013+
public function testConfigurationSkipsMigrationsForNewerDriverVersions(): void {
6014+
// String zero defaults are stored verbatim, unlike numeric zero defaults.
6015+
$this->assertQuery( "CREATE TABLE _tmp_table (created_at DATETIME(3) NOT NULL DEFAULT '0')" );
6016+
$this->sqlite->exec( "UPDATE _wp_sqlite_mysql_information_schema_columns SET datetime_precision = 0 WHERE table_name = '_tmp_table'" );
6017+
6018+
// Configure the database as if it was last configured by a newer driver version.
6019+
$this->sqlite->exec(
6020+
sprintf(
6021+
"UPDATE %s SET value = '3.1.1' WHERE name = '%s'",
6022+
WP_MySQL_On_SQLite::GLOBAL_VARIABLES_TABLE_NAME,
6023+
WP_MySQL_On_SQLite::DRIVER_VERSION_VARIABLE_NAME
6024+
)
6025+
);
6026+
$configurator = new WP_SQLite_Configurator(
6027+
$this->engine,
6028+
new WP_SQLite_Information_Schema_Builder( WP_MySQL_On_SQLite::RESERVED_PREFIX, $this->engine->get_connection() )
6029+
);
6030+
$configurator->configure_database();
6031+
6032+
// The migrations didn't run.
6033+
$this->assertQuery( 'SHOW CREATE TABLE _tmp_table' );
6034+
$this->assertSame(
6035+
implode(
6036+
"\n",
6037+
array(
6038+
'CREATE TABLE `_tmp_table` (',
6039+
" `created_at` datetime(3) NOT NULL DEFAULT '0'",
6040+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
6041+
)
6042+
),
6043+
$this->last_result[0]->{'Create Table'}
6044+
);
6045+
$this->assertQuery( "SELECT datetime_precision FROM information_schema.columns WHERE table_name = '_tmp_table'" );
6046+
$this->assertSame( '0', $this->last_result[0]->DATETIME_PRECISION );
6047+
}
6048+
60136049
public function testConfigurationNormalizesStoredTemporalZeroDefaultsOfReferencedTable(): void {
60146050
$this->assertQuery( 'CREATE TABLE _tmp_parent (id INT PRIMARY KEY, created_at TIMESTAMP NOT NULL DEFAULT 0)' );
60156051
$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)