@@ -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,13 +326,29 @@ 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.0.
338+ if ( version_compare ( $ saved_version , '3.1.0 ' , '<= ' ) ) {
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 driver versions stored 0 for all TIME, DATETIME, and TIMESTAMP
329348 * columns. The correct value is read from the column type, such as
330349 * "datetime(3)", and it is 0 for a column type without a precision.
331350 */
332- private function ensure_correct_datetime_precision (): void {
351+ private function migrate_datetime_precision (): void {
333352 // CAST() reads the leading integer, e.g., "3)" as 3, or returns 0 when there is none.
334353 $ this ->driver ->execute_sqlite_query (
335354 sprintf (
@@ -344,7 +363,7 @@ private function ensure_correct_datetime_precision(): void {
344363 }
345364
346365 /**
347- * Ensure that temporal columns store numeric zero defaults as zero values .
366+ * Migrate numeric zero defaults of temporal columns stored by older driver versions .
348367 *
349368 * Older driver versions stored defaults such as "DEFAULT 0" of a TIMESTAMP
350369 * column verbatim, while MySQL stores them as a zero date/time value.
@@ -353,7 +372,7 @@ private function ensure_correct_datetime_precision(): void {
353372 * of a table reconstructed from the legacy SQLite driver already has a zero
354373 * value, while the SQLite table still stores "0".
355374 */
356- private function ensure_temporal_zero_defaults (): void {
375+ private function migrate_temporal_zero_defaults (): void {
357376 $ columns_table = $ this ->driver ->get_connection ()->quote_identifier (
358377 $ this ->schema_builder ->get_table_name ( false , 'columns ' )
359378 );
0 commit comments