Merge branch 'store-refactor-schemes' into 'master'

[Store] Make scheme more generic

See merge request famedly/famedlysdk!61
This commit is contained in:
Marcel 2019-08-05 10:04:14 +00:00
commit 1758c2c126

View file

@ -55,7 +55,7 @@ class Store {
_init() async { _init() async {
var databasePath = await getDatabasesPath(); var databasePath = await getDatabasesPath();
String path = p.join(databasePath, "FluffyMatrix.db"); String path = p.join(databasePath, "FluffyMatrix.db");
_db = await openDatabase(path, version: 8, _db = await openDatabase(path, version: 10,
onCreate: (Database db, int version) async { onCreate: (Database db, int version) async {
await createTables(db); await createTables(db);
}, onUpgrade: (Database db, int oldVersion, int newVersion) async { }, onUpgrade: (Database db, int oldVersion, int newVersion) async {
@ -63,11 +63,9 @@ class Store {
print( print(
"[Store] Migrate databse from version $oldVersion to $newVersion"); "[Store] Migrate databse from version $oldVersion to $newVersion");
if (oldVersion != newVersion) { if (oldVersion != newVersion) {
await db.execute("DROP TABLE IF EXISTS Rooms"); await schemes.forEach((String name, String scheme) async {
await db.execute("DROP TABLE IF EXISTS Participants"); await db.execute("DROP TABLE IF EXISTS $name");
await db.execute("DROP TABLE IF EXISTS Users"); });
await db.execute("DROP TABLE IF EXISTS Events");
await db.execute("DROP TABLE IF EXISTS NotificationsCache");
db.rawUpdate("UPDATE Clients SET prev_batch='' WHERE client=?", db.rawUpdate("UPDATE Clients SET prev_batch='' WHERE client=?",
[client.clientName]); [client.clientName]);
await createTables(db); await createTables(db);
@ -99,11 +97,9 @@ class Store {
} }
Future<void> createTables(Database db) async { Future<void> createTables(Database db) async {
await db.execute(ClientsScheme); await schemes.forEach((String name, String scheme) async {
await db.execute(RoomsScheme); await db.execute(scheme);
await db.execute(UserScheme); });
await db.execute(EventsScheme);
await db.execute(NotificationsCacheScheme);
} }
Future<String> queryPrevBatch() async { Future<String> queryPrevBatch() async {
@ -133,9 +129,9 @@ class Store {
Future<void> clear() async { Future<void> clear() async {
await _db await _db
.rawDelete("DELETE FROM Clients WHERE client=?", [client.clientName]); .rawDelete("DELETE FROM Clients WHERE client=?", [client.clientName]);
await _db.rawDelete("DELETE FROM Rooms"); await schemes.forEach((String name, String scheme) async {
await _db.rawDelete("DELETE FROM Users"); if (name != "Clients") await db.rawDelete("DELETE FROM $name");
await _db.rawDelete("DELETE FROM Events"); });
return; return;
} }
@ -674,87 +670,88 @@ class Store {
return res; return res;
} }
/// The database scheme for the Client class. static final Map<String, String> schemes = {
static final String ClientsScheme = 'CREATE TABLE IF NOT EXISTS Clients(' + /// The database scheme for the Client class.
'client TEXT PRIMARY KEY, ' + "Clients": 'CREATE TABLE IF NOT EXISTS Clients(' +
'token TEXT, ' + 'client TEXT PRIMARY KEY, ' +
'homeserver TEXT, ' + 'token TEXT, ' +
'matrix_id TEXT, ' + 'homeserver TEXT, ' +
'device_id TEXT, ' + 'matrix_id TEXT, ' +
'device_name TEXT, ' + 'device_id TEXT, ' +
'prev_batch TEXT, ' + 'device_name TEXT, ' +
'matrix_versions TEXT, ' + 'prev_batch TEXT, ' +
'lazy_load_members INTEGER, ' + 'matrix_versions TEXT, ' +
'UNIQUE(client))'; 'lazy_load_members INTEGER, ' +
'UNIQUE(client))',
/// The database scheme for the Room class. /// The database scheme for the Room class.
static final String RoomsScheme = 'CREATE TABLE IF NOT EXISTS Rooms(' + "Rooms": 'CREATE TABLE IF NOT EXISTS Rooms(' +
'id TEXT PRIMARY KEY, ' + 'id TEXT PRIMARY KEY, ' +
'membership TEXT, ' + 'membership TEXT, ' +
'topic TEXT, ' + 'topic TEXT, ' +
'highlight_count INTEGER, ' + 'highlight_count INTEGER, ' +
'notification_count INTEGER, ' + 'notification_count INTEGER, ' +
'prev_batch TEXT, ' + 'prev_batch TEXT, ' +
'avatar_url TEXT, ' + 'avatar_url TEXT, ' +
'draft TEXT, ' + 'draft TEXT, ' +
'unread INTEGER, ' + // Timestamp of when the user has last read the chat 'unread INTEGER, ' + // Timestamp of when the user has last read the chat
'fully_read TEXT, ' + // ID of the fully read marker event 'fully_read TEXT, ' + // ID of the fully read marker event
'description TEXT, ' + 'description TEXT, ' +
'canonical_alias TEXT, ' + // The address in the form: #roomname:homeserver.org 'canonical_alias TEXT, ' + // The address in the form: #roomname:homeserver.org
'direct_chat_matrix_id TEXT, ' + //If this room is a direct chat, this is the matrix ID of the user 'direct_chat_matrix_id TEXT, ' + //If this room is a direct chat, this is the matrix ID of the user
'notification_settings TEXT, ' + // Must be one of [all, mention] 'notification_settings TEXT, ' + // Must be one of [all, mention]
// Security rules // Security rules
'guest_access TEXT, ' + 'guest_access TEXT, ' +
'history_visibility TEXT, ' + 'history_visibility TEXT, ' +
'join_rules TEXT, ' + 'join_rules TEXT, ' +
// Power levels // Power levels
'power_events_default INTEGER, ' + 'power_events_default INTEGER, ' +
'power_state_default INTEGER, ' + 'power_state_default INTEGER, ' +
'power_redact INTEGER, ' + 'power_redact INTEGER, ' +
'power_invite INTEGER, ' + 'power_invite INTEGER, ' +
'power_ban INTEGER, ' + 'power_ban INTEGER, ' +
'power_kick INTEGER, ' + 'power_kick INTEGER, ' +
'power_user_default INTEGER, ' + 'power_user_default INTEGER, ' +
// Power levels for events // Power levels for events
'power_event_avatar INTEGER, ' + 'power_event_avatar INTEGER, ' +
'power_event_history_visibility INTEGER, ' + 'power_event_history_visibility INTEGER, ' +
'power_event_canonical_alias INTEGER, ' + 'power_event_canonical_alias INTEGER, ' +
'power_event_aliases INTEGER, ' + 'power_event_aliases INTEGER, ' +
'power_event_name INTEGER, ' + 'power_event_name INTEGER, ' +
'power_event_power_levels INTEGER, ' + 'power_event_power_levels INTEGER, ' +
'UNIQUE(id))'; 'UNIQUE(id))',
/// The database scheme for the Event class. /// The database scheme for the Event class.
static final String EventsScheme = 'CREATE TABLE IF NOT EXISTS Events(' + "Events": 'CREATE TABLE IF NOT EXISTS Events(' +
'id TEXT PRIMARY KEY, ' + 'id TEXT PRIMARY KEY, ' +
'chat_id TEXT, ' + 'chat_id TEXT, ' +
'origin_server_ts INTEGER, ' + 'origin_server_ts INTEGER, ' +
'sender TEXT, ' + 'sender TEXT, ' +
'state_key TEXT, ' + 'state_key TEXT, ' +
'content_body TEXT, ' + 'content_body TEXT, ' +
'type TEXT, ' + 'type TEXT, ' +
'content_json TEXT, ' + 'content_json TEXT, ' +
"status INTEGER, " + "status INTEGER, " +
'UNIQUE(id))'; 'UNIQUE(id))',
/// The database scheme for the User class. /// The database scheme for the User class.
static final String UserScheme = 'CREATE TABLE IF NOT EXISTS Users(' + "Users": 'CREATE TABLE IF NOT EXISTS Users(' +
'chat_id TEXT, ' + // The chat id of this membership 'chat_id TEXT, ' + // The chat id of this membership
'matrix_id TEXT, ' + // The matrix id of this user 'matrix_id TEXT, ' + // The matrix id of this user
'displayname TEXT, ' + 'displayname TEXT, ' +
'avatar_url TEXT, ' + 'avatar_url TEXT, ' +
'membership TEXT, ' + // The status of the membership. Must be one of [join, invite, ban, leave] 'membership TEXT, ' + // The status of the membership. Must be one of [join, invite, ban, leave]
'power_level INTEGER, ' + // The power level of this user. Must be in [0,..,100] 'power_level INTEGER, ' + // The power level of this user. Must be in [0,..,100]
'UNIQUE(chat_id, matrix_id))'; 'UNIQUE(chat_id, matrix_id))',
/// The database scheme for the NotificationsCache class. /// The database scheme for the NotificationsCache class.
static final String NotificationsCacheScheme = "NotificationsCache": 'CREATE TABLE IF NOT EXISTS NotificationsCache(' +
'CREATE TABLE IF NOT EXISTS NotificationsCache(' + 'id int PRIMARY KEY, ' +
'id int PRIMARY KEY, ' + 'chat_id TEXT, ' + // The chat id
'chat_id TEXT, ' + // The chat id 'event_id TEXT, ' + // The matrix id of the Event
'event_id TEXT, ' + // The matrix id of the Event 'UNIQUE(event_id))',
'UNIQUE(event_id))'; };
} }