Fix insertion query for db paths

This commit is contained in:
Neeraj Gupta 2022-07-25 12:45:17 +05:30
parent 99f02574b3
commit 70faf7f5f5
No known key found for this signature in database
GPG key ID: 3C5A1684DC1729E1
2 changed files with 23 additions and 20 deletions

View file

@ -66,12 +66,12 @@ extension DeviceFiles on FilesDB {
Future<Set<String>> getDevicePathIDs() async {
final rows = await (await database).rawQuery(
'''
SELECT path_id FROM device_path_collections
SELECT id FROM device_path_collections
''',
);
final Set<String> result = <String>{};
for (final row in rows) {
result.add(row['path_id']);
result.add(row['id']);
}
return result;
}
@ -79,24 +79,28 @@ extension DeviceFiles on FilesDB {
Future<void> insertOrUpdatePathName(
List<AssetPathEntity> pathEntities,
) async {
final Set<String> existingPathIds = await getDevicePathIDs();
final Database db = await database;
for (AssetPathEntity pathEntity in pathEntities) {
if (existingPathIds.contains(pathEntity.id)) {
await db.rawUpdate(
"UPDATE device_path_collections SET name = ? where id = "
"?",
[pathEntity.name, pathEntity.id],
);
} else {
await db.insert(
"device_path_collections",
{
"id": pathEntity.id,
"name": pathEntity.name,
},
);
try {
final Set<String> existingPathIds = await getDevicePathIDs();
final Database db = await database;
for (AssetPathEntity pathEntity in pathEntities) {
if (existingPathIds.contains(pathEntity.id)) {
await db.rawUpdate(
"UPDATE device_path_collections SET name = ? where id = "
"?",
[pathEntity.name, pathEntity.id],
);
} else {
await db.insert(
"device_path_collections",
{
"id": pathEntity.id,
"name": pathEntity.name,
},
);
}
}
} catch (e) {
_logger.severe("failed to save path names", e);
}
}
}

View file

@ -309,7 +309,6 @@ class FilesDB {
CREATE TABLE device_path_collections (
id TEXT PRIMARY KEY NOT NULL,
name TEXT,
path TEXT,
modified_at INTEGER NOT NULL DEFAULT 0,
sync INTEGER NOT NULL DEFAULT 0,
count INTEGER NOT NULL DEFAULT 0,