aboutsummaryrefslogtreecommitdiff
path: root/syncapi/storage/postgres/receipt_table.go
diff options
context:
space:
mode:
Diffstat (limited to 'syncapi/storage/postgres/receipt_table.go')
-rw-r--r--syncapi/storage/postgres/receipt_table.go27
1 files changed, 17 insertions, 10 deletions
diff --git a/syncapi/storage/postgres/receipt_table.go b/syncapi/storage/postgres/receipt_table.go
index 327a7a37..0fcbebfc 100644
--- a/syncapi/storage/postgres/receipt_table.go
+++ b/syncapi/storage/postgres/receipt_table.go
@@ -62,11 +62,15 @@ const selectRoomReceipts = "" +
const selectMaxReceiptIDSQL = "" +
"SELECT MAX(id) FROM syncapi_receipts"
+const purgeReceiptsSQL = "" +
+ "DELETE FROM syncapi_receipts WHERE room_id = $1"
+
type receiptStatements struct {
db *sql.DB
upsertReceipt *sql.Stmt
selectRoomReceipts *sql.Stmt
selectMaxReceiptID *sql.Stmt
+ purgeReceiptsStmt *sql.Stmt
}
func NewPostgresReceiptsTable(db *sql.DB) (tables.Receipts, error) {
@@ -86,16 +90,12 @@ func NewPostgresReceiptsTable(db *sql.DB) (tables.Receipts, error) {
r := &receiptStatements{
db: db,
}
- if r.upsertReceipt, err = db.Prepare(upsertReceipt); err != nil {
- return nil, fmt.Errorf("unable to prepare upsertReceipt statement: %w", err)
- }
- if r.selectRoomReceipts, err = db.Prepare(selectRoomReceipts); err != nil {
- return nil, fmt.Errorf("unable to prepare selectRoomReceipts statement: %w", err)
- }
- if r.selectMaxReceiptID, err = db.Prepare(selectMaxReceiptIDSQL); err != nil {
- return nil, fmt.Errorf("unable to prepare selectRoomReceipts statement: %w", err)
- }
- return r, nil
+ return r, sqlutil.StatementList{
+ {&r.upsertReceipt, upsertReceipt},
+ {&r.selectRoomReceipts, selectRoomReceipts},
+ {&r.selectMaxReceiptID, selectMaxReceiptIDSQL},
+ {&r.purgeReceiptsStmt, purgeReceiptsSQL},
+ }.Prepare(db)
}
func (r *receiptStatements) UpsertReceipt(ctx context.Context, txn *sql.Tx, roomId, receiptType, userId, eventId string, timestamp gomatrixserverlib.Timestamp) (pos types.StreamPosition, err error) {
@@ -138,3 +138,10 @@ func (s *receiptStatements) SelectMaxReceiptID(
}
return
}
+
+func (s *receiptStatements) PurgeReceipts(
+ ctx context.Context, txn *sql.Tx, roomID string,
+) error {
+ _, err := sqlutil.TxStmt(txn, s.purgeReceiptsStmt).ExecContext(ctx, roomID)
+ return err
+}