Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Serialize SQLite migration version checks

laurenspriem0e53df437906
diff --git a/mobile/apps/photos/lib/db/common/base.dart b/mobile/apps/photos/lib/db/common/base.dart
index 2b6e8efafa..327d2bb8a7 100644
--- a/mobile/apps/photos/lib/db/common/base.dart
+++ b/mobile/apps/photos/lib/db/common/base.dart
@@ -16,33 +16,40 @@ mixin SqlDbBase {
SqliteDatabase database,
List<String> migrationScripts,
) async {
- final result = await database.execute('PRAGMA user_version');
- final currentVersion = result.first['user_version'] as int;
final toVersion = migrationScripts.length;
+ final didMigrate = await database.writeTransaction((tx) async {
+ final result = await tx.get('PRAGMA user_version');
+ final currentVersion = result['user_version'] as int;
+
+ if (currentVersion > toVersion) {
+ throw AssertionError(
+ "currentVersion($currentVersion) cannot be greater than toVersion($toVersion)",
+ );
+ }
+ if (currentVersion == toVersion) {
+ return false;
+ }
- if (currentVersion < toVersion) {
debugPrint(
"$runtimeType migrating database from $currentVersion to $toVersion",
);
- await database.writeTransaction((tx) async {
- for (int i = currentVersion + 1; i <= toVersion; i++) {
- try {
- final script = migrationScripts[i - 1];
- await tx.computeWithDatabase(_scriptExecutor(script));
- } catch (e) {
- debugPrint(
- "$runtimeType Error running migration script index ${i - 1} $e",
- );
- rethrow;
- }
+ for (int i = currentVersion + 1; i <= toVersion; i++) {
+ try {
+ final script = migrationScripts[i - 1];
+ await tx.computeWithDatabase(_scriptExecutor(script));
+ } catch (e) {
+ debugPrint(
+ "$runtimeType Error running migration script index ${i - 1} $e",
+ );
+ rethrow;
}
- await tx.execute('PRAGMA user_version = $toVersion');
- });
+ }
+ await tx.execute('PRAGMA user_version = $toVersion');
+ return true;
+ });
+
+ if (didMigrate) {
await database.refreshSchema();
- } else if (currentVersion > toVersion) {
- throw AssertionError(
- "currentVersion($currentVersion) cannot be greater than toVersion($toVersion)",
- );
}
}
}