Simplify using optional db transactions

This commit is contained in:
Tulir Asokan 2022-11-10 23:08:29 +02:00
parent f51fb5c5c2
commit 86e739b6a1
2 changed files with 19 additions and 34 deletions

View file

@ -180,26 +180,20 @@ func (msg *Message) Scan(row dbutil.Scannable) *Message {
return msg return msg
} }
func (msg *Message) Insert(txn dbutil.Transaction) { func (msg *Message) Insert(txn dbutil.Execable) {
if txn == nil {
txn = msg.db
}
var sender interface{} = msg.Sender var sender interface{} = msg.Sender
// Slightly hacky hack to allow inserting empty senders (used for post-backfill dummy events) // Slightly hacky hack to allow inserting empty senders (used for post-backfill dummy events)
if msg.Sender.IsEmpty() { if msg.Sender.IsEmpty() {
sender = "" sender = ""
} }
query := ` _, err := txn.Exec(`
INSERT INTO message INSERT INTO message
(chat_jid, chat_receiver, jid, mxid, sender, timestamp, sent, type, error, broadcast_list_jid) (chat_jid, chat_receiver, jid, mxid, sender, timestamp, sent, type, error, broadcast_list_jid)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
` `, msg.Chat.JID, msg.Chat.Receiver, msg.JID, msg.MXID, sender, msg.Timestamp.Unix(), msg.Sent, msg.Type, msg.Error, msg.BroadcastListJID)
args := []interface{}{
msg.Chat.JID, msg.Chat.Receiver, msg.JID, msg.MXID, sender, msg.Timestamp.Unix(), msg.Sent, msg.Type, msg.Error, msg.BroadcastListJID,
}
var err error
if txn != nil {
_, err = txn.Exec(query, args...)
} else {
_, err = msg.db.Exec(query, args...)
}
if err != nil { if err != nil {
msg.log.Warnfln("Failed to insert %s@%s: %v", msg.Chat, msg.JID, err) msg.log.Warnfln("Failed to insert %s@%s: %v", msg.Chat, msg.JID, err)
} }
@ -214,18 +208,15 @@ func (msg *Message) MarkSent(ts time.Time) {
} }
} }
func (msg *Message) UpdateMXID(txn dbutil.Transaction, mxid id.EventID, newType MessageType, newError MessageErrorType) { func (msg *Message) UpdateMXID(txn dbutil.Execable, mxid id.EventID, newType MessageType, newError MessageErrorType) {
if txn == nil {
txn = msg.db
}
msg.MXID = mxid msg.MXID = mxid
msg.Type = newType msg.Type = newType
msg.Error = newError msg.Error = newError
query := "UPDATE message SET mxid=$1, type=$2, error=$3 WHERE chat_jid=$4 AND chat_receiver=$5 AND jid=$6" _, err := txn.Exec("UPDATE message SET mxid=$1, type=$2, error=$3 WHERE chat_jid=$4 AND chat_receiver=$5 AND jid=$6",
args := []interface{}{mxid, newType, newError, msg.Chat.JID, msg.Chat.Receiver, msg.JID} mxid, newType, newError, msg.Chat.JID, msg.Chat.Receiver, msg.JID)
var err error
if txn != nil {
_, err = txn.Exec(query, args...)
} else {
_, err = msg.db.Exec(query, args...)
}
if err != nil { if err != nil {
msg.log.Warnfln("Failed to update %s@%s: %v", msg.Chat, msg.JID, err) msg.log.Warnfln("Failed to update %s@%s: %v", msg.Chat, msg.JID, err)
} }

View file

@ -209,24 +209,18 @@ func (portal *Portal) Insert() {
} }
} }
func (portal *Portal) Update(txn dbutil.Transaction) { func (portal *Portal) Update(txn dbutil.Execable) {
query := ` if txn == nil {
txn = portal.db
}
_, err := txn.Exec(`
UPDATE portal UPDATE portal
SET mxid=$1, name=$2, name_set=$3, topic=$4, topic_set=$5, avatar=$6, avatar_url=$7, avatar_set=$8, SET mxid=$1, name=$2, name_set=$3, topic=$4, topic_set=$5, avatar=$6, avatar_url=$7, avatar_set=$8,
encrypted=$9, last_sync=$10, first_event_id=$11, next_batch_id=$12, relay_user_id=$13, expiration_time=$14 encrypted=$9, last_sync=$10, first_event_id=$11, next_batch_id=$12, relay_user_id=$13, expiration_time=$14
WHERE jid=$15 AND receiver=$16 WHERE jid=$15 AND receiver=$16
` `, portal.mxidPtr(), portal.Name, portal.NameSet, portal.Topic, portal.TopicSet, portal.Avatar, portal.AvatarURL.String(),
args := []interface{}{
portal.mxidPtr(), portal.Name, portal.NameSet, portal.Topic, portal.TopicSet, portal.Avatar, portal.AvatarURL.String(),
portal.AvatarSet, portal.Encrypted, portal.lastSyncTs(), portal.FirstEventID.String(), portal.NextBatchID.String(), portal.AvatarSet, portal.Encrypted, portal.lastSyncTs(), portal.FirstEventID.String(), portal.NextBatchID.String(),
portal.relayUserPtr(), portal.ExpirationTime, portal.Key.JID, portal.Key.Receiver, portal.relayUserPtr(), portal.ExpirationTime, portal.Key.JID, portal.Key.Receiver)
}
var err error
if txn != nil {
_, err = txn.Exec(query, args...)
} else {
_, err = portal.db.Exec(query, args...)
}
if err != nil { if err != nil {
portal.log.Warnfln("Failed to update %s: %v", portal.Key, err) portal.log.Warnfln("Failed to update %s: %v", portal.Key, err)
} }