From c6f699aaff89eaa051128037aac6ae5554b57624 Mon Sep 17 00:00:00 2001 From: Marcel Date: Sat, 17 Aug 2019 15:33:53 +0200 Subject: [PATCH] [Store][NotificationsCache] Fixing issues that caused some cache errors - Remove primary key - make sure that the id is per room not per event to have no issues with the order that select returned - Add asserts to detect any null values when testing - up the db version as the schema changed Took 16 minutes --- lib/src/Store.dart | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/src/Store.dart b/lib/src/Store.dart index 80bb54e..036efb9 100644 --- a/lib/src/Store.dart +++ b/lib/src/Store.dart @@ -55,7 +55,7 @@ class Store { _init() async { var databasePath = await getDatabasesPath(); String path = p.join(databasePath, "FluffyMatrix.db"); - _db = await openDatabase(path, version: 11, + _db = await openDatabase(path, version: 12, onCreate: (Database db, int version) async { await createTables(db); }, onUpgrade: (Database db, int oldVersion, int newVersion) async { @@ -665,19 +665,27 @@ class Store { } Future forgetNotification(String roomID) async { + assert(roomID != ""); await db .rawDelete("DELETE FROM NotificationsCache WHERE chat_id=?", [roomID]); return; } Future addNotification(String roomID, String event_id, int uniqueID) async { - await db.rawInsert("INSERT INTO NotificationsCache VALUES (?, ?,?)", + assert(roomID != ""); + assert(event_id != ""); + assert(uniqueID != ""); + await db.rawInsert( + "INSERT INTO NotificationsCache(id, chat_id, event_id) VALUES (?, ?, ?)", [uniqueID, roomID, event_id]); + // Make sure we got the same unique ID everywhere + await db.rawUpdate("UPDATE NotificationsCache SET id=? WHERE chat_id=?", [uniqueID, roomID]); return; } Future>> getNotificationByRoom( String room_id) async { + assert(room_id != ""); List> res = await db.rawQuery( "SELECT * FROM NotificationsCache WHERE chat_id=?", [room_id]); if (res.length == 0) return null; @@ -766,7 +774,7 @@ class Store { /// The database scheme for the NotificationsCache class. "NotificationsCache": 'CREATE TABLE IF NOT EXISTS NotificationsCache(' + - 'id int PRIMARY KEY, ' + + 'id int, ' + 'chat_id TEXT, ' + // The chat id 'event_id TEXT, ' + // The matrix id of the Event 'UNIQUE(event_id))',