From 0f49bcbd27189cf1a5c95facd33a8f0fd2c533ed Mon Sep 17 00:00:00 2001 From: shenlong <139912620+shenlong-tanwen@users.noreply.github.com> Date: Tue, 16 Jun 2026 22:12:12 +0530 Subject: [PATCH] chore: do not optimize on cleanup (#29118) --- .../services/background_worker.service.dart | 16 +++++++++++++++- .../repositories/logger_db.repository.dart | 9 +++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/mobile/lib/domain/services/background_worker.service.dart b/mobile/lib/domain/services/background_worker.service.dart index 529ec770c4..d490b226c7 100644 --- a/mobile/lib/domain/services/background_worker.service.dart +++ b/mobile/lib/domain/services/background_worker.service.dart @@ -104,6 +104,7 @@ class BackgroundWorkerBgService extends BackgroundWorkerFlutterApi { Future onAndroidUpload(int? maxMinutes) async { final hashTimeout = Duration(minutes: _isBackupEnabled ? 3 : 6); final backupTimeout = maxMinutes != null ? Duration(minutes: maxMinutes - 1) : null; + await _optimizeDB(); return _backgroundLoop( hashTimeout: hashTimeout, backupTimeout: backupTimeout, @@ -123,6 +124,11 @@ class BackgroundWorkerBgService extends BackgroundWorkerFlutterApi { return; } + // Only for Background Processing tasks + if (maxSeconds == null) { + await _optimizeDB(); + } + // Run sync local, sync remote, hash and backup concurrently so the bg // refresh task (20s budget) can make progress on all four instead of // racing them sequentially. Phases are independent at the data layer: @@ -193,6 +199,14 @@ class BackgroundWorkerBgService extends BackgroundWorkerFlutterApi { } } + Future _optimizeDB() async { + try { + await (_drift.optimize(allTables: true), _driftLogger.optimize()).wait; + } catch (error, stack) { + dPrint(() => "Error during background worker optimize: $error, $stack"); + } + } + Future _cleanup() async { await runZonedGuarded(_handleCleanup, (error, stack) { dPrint(() => "Error during background worker cleanup: $error, $stack"); @@ -221,7 +235,7 @@ class BackgroundWorkerBgService extends BackgroundWorkerFlutterApi { if (nativeSyncApi != null) nativeSyncApi.cancelHashing(), ]); await workerManagerPatch.dispose().catchError((_) async {}); - await Future.wait([LogService.I.dispose(), Store.dispose(), _drift.optimize(allTables: true)]); + await Future.wait([LogService.I.dispose(), Store.dispose()]); await _drift.close(); await _driftLogger.close(); diff --git a/mobile/lib/infrastructure/repositories/logger_db.repository.dart b/mobile/lib/infrastructure/repositories/logger_db.repository.dart index 32af4af748..8d9794ac8b 100644 --- a/mobile/lib/infrastructure/repositories/logger_db.repository.dart +++ b/mobile/lib/infrastructure/repositories/logger_db.repository.dart @@ -2,6 +2,7 @@ import 'package:drift/drift.dart'; import 'package:drift_sqlite_async/drift_sqlite_async.dart'; import 'package:immich_mobile/infrastructure/entities/log.entity.dart'; import 'package:immich_mobile/infrastructure/repositories/logger_db.repository.drift.dart'; +import 'package:immich_mobile/utils/debug_print.dart'; import 'package:sqlite_async/sqlite_async.dart'; @DriftDatabase(tables: [LogMessageEntity]) @@ -13,6 +14,14 @@ class DriftLogger extends $DriftLogger { @override int get schemaVersion => 1; + Future optimize() async { + try { + await customStatement('PRAGMA optimize=0x10002'); + } catch (error) { + dPrint(() => 'Failed to optimize logger database: $error'); + } + } + @override MigrationStrategy get migration => MigrationStrategy( beforeOpen: (details) async {