@@ -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
@@ -320,13 +323,29 @@ private function save_current_driver_version(): void {
320323 }
321324
322325 /**
323- * Ensure that the information schema stores correct DATETIME_PRECISION values.
326+ * Migrate data stored by older driver versions.
327+ *
328+ * This is used when a new driver version needs to change some existing data.
329+ * Migrations can be version-gated so that they don't rerun on every update.
330+ *
331+ * @param string $saved_version The driver version that last configured the database.
332+ */
333+ private function run_migrations ( string $ saved_version ): void {
334+ // Fix temporal precision and zero defaults stored by versions <= 3.1.0.
335+ if ( version_compare ( $ saved_version , '3.1.0 ' , '<= ' ) ) {
336+ $ this ->migrate_datetime_precision ();
337+ $ this ->migrate_temporal_zero_defaults ();
338+ }
339+ }
340+
341+ /**
342+ * Migrate DATETIME_PRECISION values stored by older driver versions.
324343 *
325344 * Older driver versions stored 0 for all TIME, DATETIME, and TIMESTAMP
326345 * columns. The correct value is read from the column type, such as
327346 * "datetime(3)", and it is 0 for a column type without a precision.
328347 */
329- private function ensure_correct_datetime_precision (): void {
348+ private function migrate_datetime_precision (): void {
330349 // CAST() reads the leading integer, e.g., "3)" as 3, or returns 0 when there is none.
331350 $ this ->driver ->execute_sqlite_query (
332351 sprintf (
@@ -341,7 +360,7 @@ private function ensure_correct_datetime_precision(): void {
341360 }
342361
343362 /**
344- * Ensure that temporal columns store numeric zero defaults as zero values .
363+ * Migrate numeric zero defaults of temporal columns stored by older driver versions .
345364 *
346365 * Older driver versions stored defaults such as "DEFAULT 0" of a TIMESTAMP
347366 * column verbatim, while MySQL stores them as a zero date/time value.
@@ -350,7 +369,7 @@ private function ensure_correct_datetime_precision(): void {
350369 * of a table reconstructed from the legacy SQLite driver already has a zero
351370 * value, while the SQLite table still stores "0".
352371 */
353- private function ensure_temporal_zero_defaults (): void {
372+ private function migrate_temporal_zero_defaults (): void {
354373 $ columns_table = $ this ->driver ->get_connection ()->quote_identifier (
355374 $ this ->schema_builder ->get_table_name ( false , 'columns ' )
356375 );
0 commit comments