Skip to content

Commit

Permalink
change message history migration to rename column to sv1id
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackoverflow committed Oct 30, 2022
1 parent 04570ce commit 3b01877
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/store/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var (

sessionsSelect = "SELECT * FROM sessions ORDER BY timestamp DESC"

messagesSchema = "CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY, sv2id integer, source text, srcUUID string NOT NULL DEFAULT 0, message text, outgoing boolean, sentat integer, receivedat integer, ctype integer, attachment string, issent boolean, isread boolean, flags integer default 0, sendingError boolean, expireTimer integer default 0, receipt boolean default 0, statusMessage boolean default 0, quoteId integer NOT NULL default -1)"
messagesSchema = "CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY, sid integer, source text, srcUUID string NOT NULL DEFAULT 0, message text, outgoing boolean, sentat integer, receivedat integer, ctype integer, attachment string, issent boolean, isread boolean, flags integer default 0, sendingError boolean, expireTimer integer default 0, receipt boolean default 0, statusMessage boolean default 0, quoteId integer NOT NULL default -1)"
messagesInsert = "INSERT INTO messages (sid, source, srcUUID, message, outgoing, sentat, receivedat, ctype, attachment, issent, isread, flags, sendingError, expireTimer, statusMessage, quoteID) VALUES (:sid, :source, :srcUUID, :message, :outgoing, :sentat, :receivedat, :ctype, :attachment, :issent, :isread, :flags, :sendingError, :expireTimer, :statusMessage, :quoteId)"
messagesSelectWhereMore = "SELECT * FROM messages WHERE sid = ? AND sentat < ? ORDER BY sentat DESC LIMIT 20"

Expand Down
12 changes: 6 additions & 6 deletions app/store/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
log "github.com/sirupsen/logrus"
)

const getLastMessagesQuery = "SELECT *, max(sentat) FROM messages GROUP BY sv2id ORDER BY sentat DESC"
const getLastMessagesQuery = "SELECT *, max(sentat) FROM messages GROUP BY sid ORDER BY sentat DESC"

type Message struct {
ID int64 `db:"id"`
SID int64 `db:"sv2id"`
SV1ID *int64 `db:"sid"`
ID int64 `db:"id"`
SID int64
SV1ID *int64
ChatID string
Source string `db:"source"`
SourceUUID string `db:"srcUUID"`
Expand Down Expand Up @@ -163,7 +163,7 @@ func FindOutgoingMessage(timestamp uint64) (*Message, error) {
// GetUnreadMessageCounterForSession returns an int for the unread messages for a session
func GetUnreadMessageCounterForSession(id int64) (int64, error) {
var message = []Message{}
err := DS.Dbx.Select(&message, "SELECT id FROM messages WHERE isread = 0 AND sessionid = ?", id)
err := DS.Dbx.Select(&message, "SELECT id FROM messages WHERE isread = 0 AND sid = ?", id)
if err != nil {
return 0, err
}
Expand All @@ -182,7 +182,7 @@ func GetLastMessagesForAllSessions() ([]Message, error) {

func getMessagesForSession(id int64, limit, offset int) ([]*Message, error) {
var messages = []*Message{}
err := DS.Dbx.Select(&messages, "SELECT * FROM messages WHERE sv2id = ? ORDER BY sentat DESC LIMIT ? OFFSET ?", id, limit, offset)
err := DS.Dbx.Select(&messages, "SELECT * FROM messages WHERE sid = ? ORDER BY sentat DESC LIMIT ? OFFSET ?", id, limit, offset)
if err != nil {
return nil, err
}
Expand Down
25 changes: 15 additions & 10 deletions app/store/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func update_v_1_6_0() error {
return nil
}

// update_v_1_6_1 fixes the message histroy by introducing the new column sv2id in messages
// update_v_1_6_1 fixes the message histroy by introducing the renaming column sid to sv1id an reintrodrucing column sid in messages
func update_v_1_6_1() error {
err := sessionsV1toSessionsV2()
if err != nil {
Expand All @@ -255,28 +255,33 @@ func update_v_1_6_1() error {

func migrateMessageIds() error {
// check if new column exists and only migrate if it does not
_, err := DS.Dbx.Prepare("SELECT sv2id FROM messages limit 1")
_, err := DS.Dbx.Prepare("SELECT sv1id FROM messages limit 1")
if err == nil {
return nil
}
log.Infoln("[axolotl][update v_1_6_1] add column sv2id")
_, err = DS.Dbx.Exec("ALTER TABLE messages ADD sv2id integer;")
log.Infoln("[axolotl][update v_1_6_1] rename column sid to sv1id")
_, err = DS.Dbx.Exec("ALTER TABLE messages RENAME COLUMN sid TO sv1id;")
if err != nil {
return err
}
log.Infoln("[axolotl][update v_1_6_1] add column sid again")
_, err = DS.Dbx.Exec("ALTER TABLE messages ADD sid integer;")
if err != nil {
return err
}

log.Infoln("[axolotl][update v_1_6_1] set sv2id for group messages")
_, err = DS.Dbx.Exec("UPDATE messages SET sV2id = (SELECT v2.ID from sessions v1 JOIN sessionsv2 v2 ON v1.uuid = v2.groupV2Id where v1.ID = messages.sid) WHERE sv2id IS null;")
log.Infoln("[axolotl][update v_1_6_1] set sid for group messages")
_, err = DS.Dbx.Exec("UPDATE messages SET sid = (SELECT v2.ID from sessions v1 JOIN sessionsv2 v2 ON v1.uuid = v2.groupV2Id where v1.ID = messages.sv1id) WHERE sid IS null;")
if err != nil {
return err
}
log.Infoln("[axolotl][update v_1_6_1] set sv2id for direct messages")
_, err = DS.Dbx.Exec("UPDATE messages SET sv2id = (SELECT ID from sessionsv2 WHERE directMessageRecipientId = (SELECT r.id from recipients r JOIN sessions v1 ON r.uuid = v1.uuid WHERE v1.id = messages.sid)) WHERE sv2id IS null;")
log.Infoln("[axolotl][update v_1_6_1] set sid for direct messages")
_, err = DS.Dbx.Exec("UPDATE messages SET sid = (SELECT ID from sessionsv2 WHERE directMessageRecipientId = (SELECT r.id from recipients r JOIN sessions v1 ON r.uuid = v1.uuid WHERE v1.id = messages.sv1id)) WHERE sid IS null;")
if err != nil {
return err
}
log.Infoln("[axolotl][update v_1_6_1] set sv2id for messages of newly created sessions")
_, err = DS.Dbx.Exec("UPDATE messages SET sV2id = sid WHERE sv2id IS null;")
log.Infoln("[axolotl][update v_1_6_1] set sid for messages of new sessions")
_, err = DS.Dbx.Exec("UPDATE messages SET sid = sv1id WHERE sid IS null;")
return err
}

Expand Down

0 comments on commit 3b01877

Please sign in to comment.