Update database schema and fix things

This commit is contained in:
Tulir Asokan
2022-05-27 15:58:09 +03:00
parent 8c66a064e1
commit 4c7829c304
27 changed files with 806 additions and 903 deletions

View File

@@ -10,6 +10,69 @@ import (
"maunium.net/go/mautrix/util/dbutil"
)
type AttachmentQuery struct {
db *Database
log log.Logger
}
const (
attachmentSelect = "SELECT dcid, dc_msg_id, dc_chan_id, dc_chan_receiver FROM attachment"
)
func (aq *AttachmentQuery) New() *Attachment {
return &Attachment{
db: aq.db,
log: aq.log,
}
}
func (aq *AttachmentQuery) GetAllByDiscordMessageID(key PortalKey, discordMessageID string) []*Attachment {
query := attachmentSelect + " WHERE dc_chan_id=$1 AND dc_chan_receiver=$2 AND dc_msg_id=$3"
return aq.getAll(query, key.ChannelID, key.Receiver, discordMessageID)
}
func (aq *AttachmentQuery) getAll(query string, args ...interface{}) []*Attachment {
rows, err := aq.db.Query(query, args...)
if err != nil {
aq.log.Debugfln("getAll failed: %v", err)
return nil
}
if rows == nil {
return nil
}
var attachments []*Attachment
for rows.Next() {
attachments = append(attachments, aq.New().Scan(rows))
}
return attachments
}
func (aq *AttachmentQuery) GetByDiscordID(key PortalKey, discordMessageID, discordID string) *Attachment {
query := attachmentSelect + " WHERE dc_chan_id=$1 AND dc_chan_receiver=$2 AND dc_msg_id=$3 AND dcid=$4"
return aq.get(query, key.ChannelID, key.Receiver, discordMessageID, discordID)
}
func (aq *AttachmentQuery) GetByMatrixID(key PortalKey, matrixEventID id.EventID) *Attachment {
query := attachmentSelect + " WHERE dc_chan_id=$1 AND dc_chan_receiver=$2 AND mxid=$3"
return aq.get(query, key.ChannelID, key.Receiver, matrixEventID)
}
func (aq *AttachmentQuery) get(query string, args ...interface{}) *Attachment {
row := aq.db.QueryRow(query, args...)
if row == nil {
return nil
}
return aq.New().Scan(row)
}
type Attachment struct {
db *Database
log log.Logger
@@ -18,14 +81,14 @@ type Attachment struct {
DiscordMessageID string
DiscordAttachmentID string
MatrixEventID id.EventID
MXID id.EventID
}
func (a *Attachment) Scan(row dbutil.Scannable) *Attachment {
err := row.Scan(
&a.DiscordAttachmentID, &a.DiscordMessageID,
&a.Channel.ChannelID, &a.Channel.Receiver,
&a.DiscordMessageID, &a.DiscordAttachmentID,
&a.MatrixEventID)
&a.MXID)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
@@ -40,33 +103,32 @@ func (a *Attachment) Scan(row dbutil.Scannable) *Attachment {
func (a *Attachment) Insert() {
query := "INSERT INTO attachment" +
" (channel_id, receiver, discord_message_id, discord_attachment_id, " +
" matrix_event_id) VALUES ($1, $2, $3, $4, $5);"
" (dcid, dc_msg_id, dc_chan_id, dc_chan_receiver, " +
" mxid) VALUES ($1, $2, $3, $4, $5);"
_, err := a.db.Exec(
query,
a.Channel.ChannelID, a.Channel.Receiver,
a.DiscordMessageID, a.DiscordAttachmentID,
a.MatrixEventID,
a.MXID,
)
if err != nil {
a.log.Warnfln("Failed to insert attachment for %s@%s: %v", a.Channel, a.DiscordMessageID, err)
a.log.Warnfln("Failed to insert attachment for %s@%s: %v", a.DiscordAttachmentID, a.Channel, err)
}
}
func (a *Attachment) Delete() {
query := "DELETE FROM attachment WHERE" +
" channel_id=$1 AND receiver=$2 AND discord_attachment_id=$3 AND" +
" matrix_event_id=$4"
" dc_chan_id=$1 AND dc_chan_receiver=$2 AND dcid=$3"
_, err := a.db.Exec(
query,
a.Channel.ChannelID, a.Channel.Receiver,
a.DiscordAttachmentID, a.MatrixEventID,
a.DiscordAttachmentID,
)
if err != nil {
a.log.Warnfln("Failed to delete attachment for %s@%s: %v", a.Channel, a.DiscordAttachmentID, err)
a.log.Warnfln("Failed to delete attachment for %s@%s: %v", a.DiscordAttachmentID, a.Channel, err)
}
}