π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Harden sqlite_async database lifecycle
laurenspriem725d3c495c5b
diff --git a/mobile/apps/photos/lib/core/configuration.dart b/mobile/apps/photos/lib/core/configuration.dartindex 1a1d2b7262..85e4b0fc98 100644--- a/mobile/apps/photos/lib/core/configuration.dart+++ b/mobile/apps/photos/lib/core/configuration.dart@@ -48,6 +48,7 @@ import "package:photos/services/machine_learning/ml_service.dart";import "package:photos/services/machine_learning/similar_images_service.dart";import "package:photos/services/memory_share_service.dart";import "package:photos/services/notification_service.dart";+import 'package:photos/services/photos_contacts_service.dart';import 'package:photos/services/search_service.dart';import 'package:photos/services/sync/sync_service.dart';import 'package:photos/services/video_preview_service.dart';@@ -231,6 +232,7 @@ class Configuration implements LockScreenHost, AccountDeletionHost {await MLDataDB.instance.clearTable();await UploadLocksDB.instance.clearTable();await TrashDB.instance.clearTable();+ await PhotosContactsService.instance.close();await ContactsDatabase().clearTable();await SocialDB.instance.clearAllData();diff --git a/mobile/apps/photos/lib/db/collections_db.dart b/mobile/apps/photos/lib/db/collections_db.dartindex 9033a88d57..f87e4aa1da 100644--- a/mobile/apps/photos/lib/db/collections_db.dart+++ b/mobile/apps/photos/lib/db/collections_db.dart@@ -63,8 +63,15 @@ class CollectionsDB with SqlDbBase {static Future<SqliteDatabase>? _dbFuture;Future<SqliteDatabase> get database async {- _dbFuture ??= _initDatabase();- return _dbFuture!;+ final databaseFuture = _dbFuture ??= _initDatabase();+ try {+ return await databaseFuture;+ } catch (_) {+ if (identical(_dbFuture, databaseFuture)) {+ _dbFuture = null;+ }+ rethrow;+ }}Future<SqliteDatabase> _initDatabase() async {@@ -72,8 +79,13 @@ class CollectionsDB with SqlDbBase {await getApplicationDocumentsDirectory();final String path = join(documentsDirectory.path, _databaseName);final database = SqliteDatabase(path: path);- await migrate(database, [...intitialScript, ...migrationScripts]);- return database;+ try {+ await migrate(database, [...intitialScript, ...migrationScripts]);+ return database;+ } catch (_) {+ await database.close();+ rethrow;+ }}Future<void> clearTable() async {diff --git a/mobile/apps/photos/lib/db/file_updation_db.dart b/mobile/apps/photos/lib/db/file_updation_db.dartindex cce21d244e..d6e94ad30c 100644--- a/mobile/apps/photos/lib/db/file_updation_db.dart+++ b/mobile/apps/photos/lib/db/file_updation_db.dart@@ -49,8 +49,15 @@ class FileUpdationDB with SqlDbBase {static Future<SqliteDatabase>? _dbFuture;Future<SqliteDatabase> get database async {- _dbFuture ??= _initDatabase();- return _dbFuture!;+ final databaseFuture = _dbFuture ??= _initDatabase();+ try {+ return await databaseFuture;+ } catch (_) {+ if (identical(_dbFuture, databaseFuture)) {+ _dbFuture = null;+ }+ rethrow;+ }}Future<SqliteDatabase> _initDatabase() async {@@ -59,8 +66,13 @@ class FileUpdationDB with SqlDbBase {final String path = join(documentsDirectory.path, _databaseName);debugPrint("DB path " + path);final database = SqliteDatabase(path: path);- await migrate(database, [...initializationScript, ...migrationScripts]);- return database;+ try {+ await migrate(database, [...initializationScript, ...migrationScripts]);+ return database;+ } catch (_) {+ await database.close();+ rethrow;+ }}Future<void> clearTable() async {diff --git a/mobile/apps/photos/lib/db/ignored_files_db.dart b/mobile/apps/photos/lib/db/ignored_files_db.dartindex be27767a4b..01dcf7fde9 100644--- a/mobile/apps/photos/lib/db/ignored_files_db.dart+++ b/mobile/apps/photos/lib/db/ignored_files_db.dart@@ -39,8 +39,15 @@ class IgnoredFilesDB with SqlDbBase {static Future<SqliteDatabase>? _dbFuture;Future<SqliteDatabase> get database async {- _dbFuture ??= _initDatabase();- return _dbFuture!;+ final databaseFuture = _dbFuture ??= _initDatabase();+ try {+ return await databaseFuture;+ } catch (_) {+ if (identical(_dbFuture, databaseFuture)) {+ _dbFuture = null;+ }+ rethrow;+ }}Future<SqliteDatabase> _initDatabase() async {@@ -48,8 +55,13 @@ class IgnoredFilesDB with SqlDbBase {await getApplicationDocumentsDirectory();final String path = join(documentsDirectory.path, _databaseName);final database = SqliteDatabase(path: path);- await migrate(database, _migrationScripts);- return database;+ try {+ await migrate(database, _migrationScripts);+ return database;+ } catch (_) {+ await database.close();+ rethrow;+ }}Future<void> clearTable() async {diff --git a/mobile/apps/photos/lib/db/memories_db.dart b/mobile/apps/photos/lib/db/memories_db.dartindex 66d53e4498..84bfeaf6b2 100644--- a/mobile/apps/photos/lib/db/memories_db.dart+++ b/mobile/apps/photos/lib/db/memories_db.dart@@ -26,8 +26,15 @@ class MemoriesDB with SqlDbBase {Future<SqliteDatabase>? _dbFuture;Future<SqliteDatabase> get database async {- _dbFuture ??= _initDatabase();- return _dbFuture!;+ final databaseFuture = _dbFuture ??= _initDatabase();+ try {+ return await databaseFuture;+ } catch (_) {+ if (identical(_dbFuture, databaseFuture)) {+ _dbFuture = null;+ }+ rethrow;+ }}Future<SqliteDatabase> _initDatabase() async {@@ -35,15 +42,20 @@ class MemoriesDB with SqlDbBase {await getApplicationDocumentsDirectory();final String path = join(documentsDirectory.path, _dbName);final database = SqliteDatabase(path: path);- await migrate(database, [- '''+ try {+ await migrate(database, [+ '''CREATE TABLE $table ($columnFileID INTEGER PRIMARY KEY NOT NULL,$columnSeenTime TEXT NOT NULL)''',- ]);- return database;+ ]);+ return database;+ } catch (_) {+ await database.close();+ rethrow;+ }}Future<void> clearTable() async {diff --git a/mobile/apps/photos/lib/db/memory_shares_db.dart b/mobile/apps/photos/lib/db/memory_shares_db.dartindex aff31fcbeb..f6a0785969 100644--- a/mobile/apps/photos/lib/db/memory_shares_db.dart+++ b/mobile/apps/photos/lib/db/memory_shares_db.dart@@ -32,8 +32,15 @@ class MemorySharesDB with SqlDbBase {static Future<SqliteDatabase>? _dbFuture;Future<SqliteDatabase> get database async {- _dbFuture ??= _initDatabase();- return _dbFuture!;+ final databaseFuture = _dbFuture ??= _initDatabase();+ try {+ return await databaseFuture;+ } catch (_) {+ if (identical(_dbFuture, databaseFuture)) {+ _dbFuture = null;+ }+ rethrow;+ }}Future<SqliteDatabase> _initDatabase() async {@@ -41,8 +48,9 @@ class MemorySharesDB with SqlDbBase {await getApplicationDocumentsDirectory();final String path = join(documentsDirectory.path, _databaseName);final database = SqliteDatabase(path: path);- await migrate(database, [- '''+ try {+ await migrate(database, [+ '''CREATE TABLE $_table ($_columnID INTEGER PRIMARY KEY NOT NULL,$_columnType TEXT NOT NULL,@@ -60,8 +68,12 @@ class MemorySharesDB with SqlDbBase {$_columnFileCount INTEGER)''',- ]);- return database;+ ]);+ return database;+ } catch (_) {+ await database.close();+ rethrow;+ }}Future<void> upsert(MemoryShare share) async {diff --git a/mobile/apps/photos/lib/db/social_db.dart b/mobile/apps/photos/lib/db/social_db.dartindex 5513497648..ccb7522fed 100644--- a/mobile/apps/photos/lib/db/social_db.dart+++ b/mobile/apps/photos/lib/db/social_db.dart@@ -98,8 +98,15 @@ class SocialDB with SqlDbBase {static Future<SqliteDatabase>? _dbFuture;Future<SqliteDatabase> get database async {- _dbFuture ??= _initDatabase();- return _dbFuture!;+ final databaseFuture = _dbFuture ??= _initDatabase();+ try {+ return await databaseFuture;+ } catch (_) {+ if (identical(_dbFuture, databaseFuture)) {+ _dbFuture = null;+ }+ rethrow;+ }}Future<SqliteDatabase> _initDatabase() async {@@ -108,8 +115,13 @@ class SocialDB with SqlDbBase {final String path = join(documentsDirectory.path, _databaseName);_logger.info("DB path: $path");final database = SqliteDatabase(path: path);- await migrate(database, _migrationScripts);- return database;+ try {+ await migrate(database, _migrationScripts);+ return database;+ } catch (_) {+ await database.close();+ rethrow;+ }}Future<void> addComment(Comment comment) async {diff --git a/mobile/apps/photos/lib/db/trash_db.dart b/mobile/apps/photos/lib/db/trash_db.dartindex f29edc7203..3376b36e17 100644--- a/mobile/apps/photos/lib/db/trash_db.dart+++ b/mobile/apps/photos/lib/db/trash_db.dart@@ -73,8 +73,15 @@ class TrashDB with SqlDbBase {static Future<SqliteDatabase>? _dbFuture;Future<SqliteDatabase> get database async {- _dbFuture ??= _initDatabase();- return _dbFuture!;+ final databaseFuture = _dbFuture ??= _initDatabase();+ try {+ return await databaseFuture;+ } catch (_) {+ if (identical(_dbFuture, databaseFuture)) {+ _dbFuture = null;+ }+ rethrow;+ }}Future<SqliteDatabase> _initDatabase() async {@@ -83,8 +90,13 @@ class TrashDB with SqlDbBase {final String path = join(documentsDirectory.path, _databaseName);_logger.info("DB path " + path);final database = SqliteDatabase(path: path);- await migrate(database, _migrationScripts);- return database;+ try {+ await migrate(database, _migrationScripts);+ return database;+ } catch (_) {+ await database.close();+ rethrow;+ }}Future<void> clearTable() async {diff --git a/mobile/apps/photos/lib/db/upload_locks_db.dart b/mobile/apps/photos/lib/db/upload_locks_db.dartindex 9b02c5dca9..fb7b76a0ba 100644--- a/mobile/apps/photos/lib/db/upload_locks_db.dart+++ b/mobile/apps/photos/lib/db/upload_locks_db.dart@@ -89,8 +89,15 @@ class UploadLocksDB with SqlDbBase {static Future<SqliteDatabase>? _dbFuture;Future<SqliteDatabase> get database async {- _dbFuture ??= _initDatabase();- return _dbFuture!;+ final databaseFuture = _dbFuture ??= _initDatabase();+ try {+ return await databaseFuture;+ } catch (_) {+ if (identical(_dbFuture, databaseFuture)) {+ _dbFuture = null;+ }+ rethrow;+ }}Future<SqliteDatabase> _initDatabase() async {@@ -99,8 +106,13 @@ class UploadLocksDB with SqlDbBase {final String path = join(documentsDirectory.path, _databaseName);final database = SqliteDatabase(path: path);- await migrate(database, [...initializationScript, ...migrationScripts]);- return database;+ try {+ await migrate(database, [...initializationScript, ...migrationScripts]);+ return database;+ } catch (_) {+ await database.close();+ rethrow;+ }}static List<String> _createUploadLocksTable() {diff --git a/mobile/apps/photos/lib/services/photos_contacts_service.dart b/mobile/apps/photos/lib/services/photos_contacts_service.dartindex fe5bf59225..f8f9c84935 100644--- a/mobile/apps/photos/lib/services/photos_contacts_service.dart+++ b/mobile/apps/photos/lib/services/photos_contacts_service.dart@@ -151,6 +151,8 @@ class PhotosContactsService {@visibleForTestingvoid debugReset({bool notify = false}) => _store.clearSession(notify: notify);+ Future<void> close() => _store.close();+contacts.ContactsSession? _buildSession() {final config = Configuration.instance;final userId = config.getUserID();diff --git a/mobile/packages/contacts/lib/src/db/contacts_database.dart b/mobile/packages/contacts/lib/src/db/contacts_database.dartindex 1b1494c52f..9aceb8053d 100644--- a/mobile/packages/contacts/lib/src/db/contacts_database.dart+++ b/mobile/packages/contacts/lib/src/db/contacts_database.dart@@ -24,7 +24,6 @@ class ContactsDatabase {final ContactsDatabaseDirectoryResolver? _directoryResolver;- SqliteDatabase? _database;Future<SqliteDatabase>? _dbFuture;int? _configuredUserId;@@ -32,12 +31,25 @@ class ContactsDatabase {if (_configuredUserId == userId && _dbFuture != null) {return;}- if (_database != null) {- await _database!.close();- _database = null;- }+ final previousDatabaseFuture = _dbFuture;_dbFuture = null;_configuredUserId = userId;+ if (previousDatabaseFuture != null) {+ try {+ await (await previousDatabaseFuture).close();+ } catch (_) {}+ }+ }++ Future<void> close() async {+ final databaseFuture = _dbFuture;+ _dbFuture = null;+ _configuredUserId = null;+ if (databaseFuture != null) {+ try {+ await (await databaseFuture).close();+ } catch (_) {}+ }}Future<SqliteDatabase> get database async {@@ -47,9 +59,15 @@ class ContactsDatabase {'ContactsDatabase.configure(userId: ...) must be called first',);}- _dbFuture ??= _initDatabase(userId);- _database ??= await _dbFuture!;- return _database!;+ final databaseFuture = _dbFuture ??= _initDatabase(userId);+ try {+ return await databaseFuture;+ } catch (_) {+ if (identical(_dbFuture, databaseFuture)) {+ _dbFuture = null;+ }+ rethrow;+ }}Future<void> upsertContacts(List<ContactRecord> contacts) async {@@ -188,11 +206,7 @@ class ContactsDatabase {}Future<void> clearTable() async {- if (_database != null) {- await _database!.close();- _database = null;- _dbFuture = null;- }+ await close();final directory = await _resolvedDirectory();if (!directory.existsSync()) {return;@@ -224,19 +238,19 @@ class ContactsDatabase {}Future<void> _migrate(SqliteDatabase database) async {- final result = await database.execute('PRAGMA user_version');- final currentVersion = result.first['user_version'] as int;- if (currentVersion == _databaseVersion) {- return;- }- if (currentVersion > _databaseVersion) {- throw StateError(- 'Contacts database version $currentVersion is newer than supported '- 'version $_databaseVersion',- );- }+ final didMigrate = await database.writeTransaction((tx) async {+ final result = await tx.get('PRAGMA user_version');+ final currentVersion = result['user_version'] as int;+ if (currentVersion == _databaseVersion) {+ return false;+ }+ if (currentVersion > _databaseVersion) {+ throw StateError(+ 'Contacts database version $currentVersion is newer than supported '+ 'version $_databaseVersion',+ );+ }- await database.writeTransaction((tx) async {await tx.execute('''CREATE TABLE $_contactsTable (id TEXT PRIMARY KEY,@@ -274,8 +288,11 @@ class ContactsDatabase {'INSERT INTO $_stateTable (id, last_synced_updated_at) VALUES (1, 0)',);await tx.execute('PRAGMA user_version = $_databaseVersion');+ return true;});- await database.refreshSchema();+ if (didMigrate) {+ await database.refreshSchema();+ }}Future<Directory> _resolvedDirectory() async {diff --git a/mobile/packages/contacts/lib/src/service/contact_directory.dart b/mobile/packages/contacts/lib/src/service/contact_directory.dartindex 8d41092609..33adf49cad 100644--- a/mobile/packages/contacts/lib/src/service/contact_directory.dart+++ b/mobile/packages/contacts/lib/src/service/contact_directory.dart@@ -87,6 +87,18 @@ class ContactDirectory {void clearSession({bool notify = true}) => _clear(notify: notify);+ Future<void> close({bool notify = true}) async {+ final active = _active;+ _clear(notify: notify);+ final ready = active?.ready;+ if (ready != null) {+ try {+ await ready;+ } catch (_) {}+ }+ await active?.service.close();+ }+// Positive account IDs are authoritative and never fall back to email.ContactRecord? getCachedContact({int? contactUserId, String? email}) {if (contactUserId != null && contactUserId > 0) {diff --git a/mobile/packages/contacts/lib/src/service/contacts_service.dart b/mobile/packages/contacts/lib/src/service/contacts_service.dartindex 0f0f3d473a..98ebbf6145 100644--- a/mobile/packages/contacts/lib/src/service/contacts_service.dart+++ b/mobile/packages/contacts/lib/src/service/contacts_service.dart@@ -262,6 +262,12 @@ class ContactsService {await _database.resetState();}+ Future<void> close() async {+ _ctx = null;+ _session = null;+ await _database.close();+ }+int _nextSyncCursor(List<ContactRecord> diff, int maxUpdatedAt, int limit) {if (diff.length < limit || maxUpdatedAt <= 0) {return maxUpdatedAt;diff --git a/mobile/packages/log_viewer/lib/src/core/log_database.dart b/mobile/packages/log_viewer/lib/src/core/log_database.dartindex 8df1e0cfcc..93a9e84c75 100644--- a/mobile/packages/log_viewer/lib/src/core/log_database.dart+++ b/mobile/packages/log_viewer/lib/src/core/log_database.dart@@ -36,19 +36,19 @@ class LogDatabase {}Future<void> _migrate(SqliteDatabase database) async {- final result = await database.execute('PRAGMA user_version');- final currentVersion = result.first['user_version'] as int;- if (currentVersion == _databaseVersion) {- return;- }- if (currentVersion > _databaseVersion) {- throw StateError(- 'Log database version $currentVersion is newer than supported version '- '$_databaseVersion',- );- }+ final didMigrate = await database.writeTransaction((tx) async {+ final result = await tx.get('PRAGMA user_version');+ final currentVersion = result['user_version'] as int;+ if (currentVersion == _databaseVersion) {+ return false;+ }+ if (currentVersion > _databaseVersion) {+ throw StateError(+ 'Log database version $currentVersion is newer than supported '+ 'version $_databaseVersion',+ );+ }- await database.writeTransaction((tx) async {await tx.execute('''CREATE TABLE $_tableName(id INTEGER PRIMARY KEY AUTOINCREMENT,@@ -65,8 +65,11 @@ class LogDatabase {'CREATE INDEX idx_timestamp ON $_tableName(timestamp DESC)',);await tx.execute('PRAGMA user_version = $_databaseVersion');+ return true;});- await database.refreshSchema();+ if (didMigrate) {+ await database.refreshSchema();+ }}Future<int> insertLog(LogEntry entry) async {@@ -184,7 +187,8 @@ class LogDatabase {Future<List<String>> getUniqueProcesses() async {final db = await database;final results = await db.getAll(- 'SELECT DISTINCT process_prefix FROM $_tableName WHERE process_prefix != "" ORDER BY process_prefix',+ 'SELECT DISTINCT process_prefix FROM $_tableName WHERE process_prefix != ? ORDER BY process_prefix',+ [''],);final prefixes = results