Skip to content

armgit / ente

Public Imported from GitHub
0

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

Atomically upsert device collection covers

Neeraj Guptad08d62e1d13b
diff --git a/mobile/apps/photos/lib/db/device_files_db.dart b/mobile/apps/photos/lib/db/device_files_db.dart
index a8b07b9cc4..b5fcc8c6a7 100644
--- a/mobile/apps/photos/lib/db/device_files_db.dart
+++ b/mobile/apps/photos/lib/db/device_files_db.dart
@@ -205,49 +205,35 @@ extension DeviceFiles on FilesDB {
final String localID = tup.item2;
final int modifiedAt =
pathEntity.lastModified?.microsecondsSinceEpoch ?? 0;
- final bool shouldUpdate = existingPathIds.contains(pathEntity.id);
- if (shouldUpdate) {
- final rowUpdated = await db.writeTransaction((tx) async {
- await tx.execute(
- "UPDATE device_collections SET name = ?, cover_id = ?, count"
- " = ?, modified_at = ? where id = ? AND (name != ? OR "
- "cover_id != ? OR count != ? OR modified_at != ?)",
- [
- pathEntity.name,
- localID,
- assetCount,
- modifiedAt,
- pathEntity.id,
- pathEntity.name,
- localID,
- assetCount,
- modifiedAt,
- ],
- );
- final result = await tx.get("SELECT changes();");
- return result["changes()"] as int;
- });
-
- if (rowUpdated > 0) {
- _logger.info("Updated $rowUpdated rows for ${pathEntity.name}");
- hasUpdated = true;
- }
- } else {
- hasUpdated = true;
- await db.execute(
- '''
+ final updatedRows = await db.execute(
+ '''
INSERT INTO device_collections (id, name, count, cover_id, modified_at, should_backup)
- VALUES (?, ?, ?, ?, ?, ?);
+ VALUES (?, ?, ?, ?, ?, ?)
+ ON CONFLICT(id) DO UPDATE SET
+ name = excluded.name,
+ count = excluded.count,
+ cover_id = excluded.cover_id,
+ modified_at = excluded.modified_at
+ WHERE device_collections.name IS NOT excluded.name
+ OR device_collections.count IS NOT excluded.count
+ OR device_collections.cover_id IS NOT excluded.cover_id
+ OR device_collections.modified_at IS NOT excluded.modified_at
+ RETURNING id;
''',
- [
- pathEntity.id,
- pathEntity.name,
- assetCount,
- localID,
- modifiedAt,
- shouldBackup ? _sqlBoolTrue : _sqlBoolFalse,
- ],
+ [
+ pathEntity.id,
+ pathEntity.name,
+ assetCount,
+ localID,
+ modifiedAt,
+ shouldBackup ? _sqlBoolTrue : _sqlBoolFalse,
+ ],
+ );
+ if (updatedRows.isNotEmpty) {
+ _logger.info(
+ "Updated ${updatedRows.length} rows for ${pathEntity.name}",
);
+ hasUpdated = true;
}
}
// delete existing pathIDs which are missing on device