WIP: refactor: account for columns added to chats, contacts, and user_messages tables

This commit is contained in:
Michael Bradley, Jr 2021-09-12 16:58:25 -05:00
parent 379ddc3aa8
commit 8915bfc74f
No known key found for this signature in database
GPG Key ID: D0307DBCF21A9A58
5 changed files with 82 additions and 22 deletions

View File

@ -24,9 +24,16 @@ type
LastMessage = "lastMessage",
Members = "members",
MembershipUpdates = "membershipUpdates"
Profile = "profile",
Muted = "muted",
InvitationAdmin = "invitationAdmin",
Muted = "muted"
Profile = "profile",
CommunityId = "communityId",
Accepted = "accepted",
Joined = "joined",
SyncedTo = "syncedTo",
SyncedFrom = "syncedFrom",
UnviewedMentionsCount = "unviewedMentionsCount",
Description = "description"
ChatCol* {.pure.} = enum
Id = "id",
@ -42,9 +49,16 @@ type
LastMessage = "last_message",
Members = "members",
MembershipUpdates = "membership_updates"
Profile = "profile",
Muted = "muted",
InvitationAdmin = "invitation_admin",
Muted = "muted"
Profile = "profile"
CommunityId = "community_id",
Accepted = "accepted",
Joined = "joined",
SyncedTo = "synced_to",
SyncedFrom = "synced_from",
UnviewedMentionsCount = "unviewed_mentions_count",
Description = "description"
Chat* {.dbTableName("chats").} = object
id* {.serializedFieldName($ChatType.Id), dbColumnName($ChatCol.Id).}: string
@ -62,9 +76,16 @@ type
members* {.serializedFieldName($ChatType.Members), dbColumnName($ChatCol.Members).}: seq[byte]
# TODO: membershipUpdates should be a concrete type
membershipUpdates* {.serializedFieldName($ChatType.MembershipUpdates), dbColumnName($ChatCol.MembershipUpdates).}: seq[byte]
profile* {.serializedFieldName($ChatType.Profile), dbColumnName($ChatCol.Profile).}: string
invitationAdmin* {.serializedFieldName($ChatType.InvitationAdmin), dbColumnName($ChatCol.InvitationAdmin).}: string
muted* {.serializedFieldName($ChatType.Muted), dbColumnName($ChatCol.Muted).}: bool
invitationAdmin* {.serializedFieldName($ChatType.InvitationAdmin), dbColumnName($ChatCol.InvitationAdmin).}: string
profile* {.serializedFieldName($ChatType.Profile), dbColumnName($ChatCol.Profile).}: string
communityId* {.serializedFieldName($ChatType.CommunityId), dbColumnName($ChatCol.CommunityId).}: string
accepted* {.serializedFieldName($ChatType.Accepted), dbColumnName($ChatCol.Accepted).}: bool
joined* {.serializedFieldName($ChatType.Joined), dbColumnName($ChatCol.Joined).}: int
syncedTo* {.serializedFieldName($ChatType.SyncedTo), dbColumnName($ChatCol.SyncedTo).}: int
syncedFrom* {.serializedFieldName($ChatType.SyncedFrom), dbColumnName($ChatCol.SyncedFrom).}: int
unviewedMentionsCount* {.serializedFieldName($ChatType.UnviewedMentionsCount), dbColumnName($ChatCol.UnviewedMentionsCount).}: int
description* {.serializedFieldName($ChatType.Description), dbColumnName($ChatCol.Description).}: string
MessageType* {.pure.} = enum
Id = "id",

View File

@ -12,35 +12,37 @@ import # status modules
export chatmessages, common
proc getChats*(db: DbConn): DbResult[seq[Chat]] {.raises: [AssertionError,
Defect].} =
proc getChats*(db: DbConn): DbResult[seq[Chat]]
{.raises: [AssertionError, Defect].} =
try:
var chat: Chat
let query = fmt"""SELECT *
FROM {chat.tableName}"""
ok db.all(Chat, query)
except ConversionError: err MarshalFailure
except SqliteError: err OperationError
except ValueError: err QueryBuildError
proc getChatById*(db: DbConn, id: string): DbResult[Option[Chat]] {.raises:
[AssertionError, Defect].} =
proc getChatById*(db: DbConn, id: string): DbResult[Option[Chat]]
{.raises: [AssertionError, Defect].} =
try:
var chat: Chat
let query = fmt"""SELECT *
FROM {chat.tableName}
WHERE {ChatCol.Id} = ?"""
ok db.one(Chat, query, id)
except ConversionError: err MarshalFailure
except SqliteError: err OperationError
except ValueError: err QueryBuildError
proc saveChat*(db: DbConn, chat: Chat): DbResult[void] =
try:
let query = fmt"""INSERT INTO chats(
{ChatCol.Id},
{ChatCol.Name},
@ -55,11 +57,20 @@ proc saveChat*(db: DbConn, chat: Chat): DbResult[void] =
{ChatCol.LastMessage},
{ChatCol.Members},
{ChatCol.MembershipUpdates},
{ChatCol.Profile},
{ChatCol.Muted},
{ChatCol.InvitationAdmin},
{ChatCol.Muted})
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
{ChatCol.Profile},
{ChatCol.CommunityId},
{ChatCol.Accepted},
{ChatCol.Joined},
{ChatCol.SyncedTo},
{ChatCol.SyncedFrom},
{ChatCol.UnviewedMentionsCount},
{ChatCol.Description}
)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
"""
db.exec(query,
chat.id,
chat.name,
@ -74,9 +85,17 @@ proc saveChat*(db: DbConn, chat: Chat): DbResult[void] =
chat.lastMessage,
chat.members,
chat.membershipUpdates,
chat.profile,
chat.muted,
chat.invitationAdmin,
chat.muted)
chat.profile,
chat.communityId,
chat.accepted,
chat.joined,
chat.syncedTo,
chat.syncedFrom,
chat.unviewedMentionsCount,
chat.description)
ok()
except SqliteError: err OperationError
@ -88,8 +107,10 @@ proc muteChat*(db: DbConn, chatId: string): DbResult[void] {.raises: [].} =
let query = fmt"""UPDATE {chat.tableName}
SET {ChatCol.Muted} = 1
WHERE {ChatCol.Id} = ?"""
db.exec(query, chatId)
ok()
except SqliteError: err OperationError
except ValueError: err QueryBuildError
@ -99,8 +120,10 @@ proc unmuteChat*(db: DbConn, chatId: string): DbResult[void] {.raises: [].} =
let query = fmt"""UPDATE {chat.tableName}
SET {ChatCol.Muted} = 0
WHERE {ChatCol.Id} = ?"""
db.exec(query, chatId)
ok()
except SqliteError: err OperationError
except ValueError: err QueryBuildError
@ -109,8 +132,10 @@ proc deleteChat*(db: DbConn, chat: Chat): DbResult[void] {.raises: [].} =
var tblChat: Chat
let query = fmt"""DELETE FROM {tblChat.tableName}
WHERE {ChatCol.Id} = ?"""
db.exec(query, chat.id)
ok()
except SqliteError: err OperationError
except ValueError: err QueryBuildError
@ -123,8 +148,10 @@ proc deleteOneToOneChat*(db: DbConn, contactId: string): DbResult[void]
let query = fmt"""DELETE
FROM {chat.tableName}
WHERE id = ?"""
db.exec(query, contactId)
ok()
except SqliteError: err OperationError
except ValueError: err QueryBuildError
@ -138,8 +165,10 @@ proc updateLastMessage*(db: DbConn, chatId: string,
let query = fmt"""UPDATE {chat.tableName}
SET {ChatCol.LastMessage} = ?
WHERE {ChatCol.Id} = ?"""
db.exec(query, lastMessage, chatId)
ok()
except SqliteError: err OperationError
except ValueError: err QueryBuildError
@ -163,6 +192,7 @@ proc blockContact*(db: DbConn, contact: Contact): DbResult[seq[Chat]] =
var chats = ?getChats(db)
# var lastResultErr: DbError
var chatsModified: seq[Chat] = @[]
for chat in chats:
var chatModified = chat
let lastMessage = ?db.getLastMessage(chat.id)

View File

@ -2,7 +2,7 @@ import # std libs
std/[json, options, os, unittest]
import # vendor libs
chronos, json_serialization, secp256k1, sqlcipher, stew/byteutils
chronicles, chronos, json_serialization, secp256k1, sqlcipher, stew/byteutils
import # status lib
status/private/[chats, contacts, conversions, database, messages]
@ -42,9 +42,16 @@ procSuite "chats":
lastMessage: message.some,
members: "members".toBytes(),
membershipUpdates: "membershipUpdates".toBytes(),
profile: "profile",
invitationAdmin: "invitationAdmin",
muted: false,
invitationAdmin: "invitationAdmin",
profile: "profile",
communityId: "community-id",
accepted: false,
joined: 0,
syncedTo: 0,
syncedFrom: 0,
unviewedMentionsCount: 0,
description: "chat-description"
)
# saveChat
@ -113,6 +120,8 @@ procSuite "chats":
localNickname: some("ABC1")
)
var saved = db.saveContact(contact)
error "THIS WAS THE ERROR", error=saved.error
check db.saveContact(contact).isOk
var msg = Message(

View File

@ -7,8 +7,8 @@ import # test modules
./api/networks,
./bip32,
./callrpc,
# ./chats,
# ./contacts,
./chats,
./contacts,
./db_smoke,
./mailservers,
./messages,