@@ -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 ();
98102 $ this ->save_current_driver_version ();
99103 $ this ->ensure_database_data ();
100- $ this ->ensure_correct_datetime_precision ();
101- $ this ->ensure_temporal_zero_defaults ();
104+ $ this ->run_migrations ( $ saved_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".
0 commit comments