feat: migrate store to sqlite (#21078)

* add store entity and migration

* make store service take both isar and drift repos

* migrate and switch store on beta timeline state change

* chore: make drift variables final

* dispose old store before switching repos

* use store to update values for beta timeline

* change log service to use the proper store

* migrate store when beta already enabled

* use isar repository to check beta timeline in store service

* remove unused update method from store repo

* dispose after create

* change watchAll signature in store repo

* fix test

* rename init isar to initDB

* request user to close and reopen on beta migration

* fix tests

* handle empty version in migration

* wait for cache to be populated after migration

---------

Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com>
Co-authored-by: Alex <alex.tran1502@gmail.com>
This commit is contained in:
shenlong 2025-08-22 01:28:50 +05:30 committed by GitHub
parent ed3997d844
commit 6f4f79d8cc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 7907 additions and 169 deletions

View file

@ -20,6 +20,7 @@ import 'package:immich_mobile/infrastructure/entities/exif.entity.dart';
import 'package:immich_mobile/infrastructure/entities/local_album.entity.drift.dart';
import 'package:immich_mobile/infrastructure/entities/local_asset.entity.drift.dart';
import 'package:immich_mobile/infrastructure/entities/store.entity.dart';
import 'package:immich_mobile/infrastructure/entities/store.entity.drift.dart';
import 'package:immich_mobile/infrastructure/entities/user.entity.dart';
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
import 'package:immich_mobile/providers/background_sync.provider.dart';
@ -30,9 +31,10 @@ import 'package:logging/logging.dart';
// ignore: import_rule_photo_manager
import 'package:photo_manager/photo_manager.dart';
const int targetVersion = 13;
const int targetVersion = 14;
Future<void> migrateDatabaseIfNeeded(Isar db) async {
Future<void> migrateDatabaseIfNeeded(Isar db, Drift drift) async {
final hasVersion = Store.tryGet(StoreKey.version) != null;
final int version = Store.get(StoreKey.version, targetVersion);
if (version < 9) {
@ -58,6 +60,12 @@ Future<void> migrateDatabaseIfNeeded(Isar db) async {
await Store.put(StoreKey.photoManagerCustomFilter, true);
}
// This means that the SQLite DB is just created and has no version
if (version < 14 || !hasVersion) {
await migrateStoreToSqlite(db, drift);
await Store.populateCache();
}
if (targetVersion >= 12) {
await Store.put(StoreKey.version, targetVersion);
return;
@ -215,6 +223,39 @@ Future<void> migrateBackupAlbumsToSqlite(Isar db, Drift drift) async {
}
}
Future<void> migrateStoreToSqlite(Isar db, Drift drift) async {
try {
final isarStoreValues = await db.storeValues.where().findAll();
await drift.batch((batch) {
for (final storeValue in isarStoreValues) {
final companion = StoreEntityCompanion(
id: Value(storeValue.id),
stringValue: Value(storeValue.strValue),
intValue: Value(storeValue.intValue),
);
batch.insert(drift.storeEntity, companion, onConflict: DoUpdate((_) => companion));
}
});
} catch (error) {
debugPrint("[MIGRATION] Error while migrating store values to SQLite: $error");
}
}
Future<void> migrateStoreToIsar(Isar db, Drift drift) async {
try {
final driftStoreValues = await drift.storeEntity
.select()
.map((entity) => StoreValue(entity.id, intValue: entity.intValue, strValue: entity.stringValue))
.get();
await db.writeTxn(() async {
await db.storeValues.putAll(driftStoreValues);
});
} catch (error) {
debugPrint("[MIGRATION] Error while migrating store values to Isar: $error");
}
}
class _DeviceAsset {
final String assetId;
final List<int>? hash;