From afef2a19ae1de9fef5fbc2b2632be6d72d090acd Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Tue, 13 Oct 2020 07:30:09 +0200 Subject: [PATCH] - Add postgres tables - Rename index on sqlite3 --- roomserver/storage/postgres/receipt_table.go | 77 ++++++++++++++++++++ roomserver/storage/postgres/storage.go | 5 ++ roomserver/storage/sqlite3/receipt_table.go | 2 +- 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 roomserver/storage/postgres/receipt_table.go diff --git a/roomserver/storage/postgres/receipt_table.go b/roomserver/storage/postgres/receipt_table.go new file mode 100644 index 000000000..c06fe49c1 --- /dev/null +++ b/roomserver/storage/postgres/receipt_table.go @@ -0,0 +1,77 @@ +// Copyright 2020 The Matrix.org Foundation C.I.C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package postgres + +import ( + "context" + "database/sql" + "time" + + "github.com/pkg/errors" + + "github.com/matrix-org/dendrite/internal/sqlutil" + "github.com/matrix-org/dendrite/roomserver/storage/tables" +) + +const receiptsSchema = ` +-- Stores data about receipts +CREATE TABLE IF NOT EXISTS roomserver_receipts ( + -- The ID + id INTEGER PRIMARY KEY, + room_id TEXT NOT NULL, + receipt_type TEXT NOT NULL, + user_id TEXT NOT NULL, + event_id TEXT NOT NULL, + receipt_ts BIGINT NOT NULL, + + CONSTRAINT roomserver_receipts_unique UNIQUE (room_id, receipt_type, user_id) +); + +CREATE INDEX IF NOT EXISTS roomserver_receipts_room_id ON roomserver_receipts(room_id); +` + +const upsertReceipt = "" + + "INSERT INTO roomserver_receipts" + + " (room_id, receipt_type, user_id, event_id, receipt_ts)" + + " VALUES ($1, $2, $3, $4, $5)" + + " ON CONFLICT (room_id, receipt_type, user_id)" + + " DO UPDATE SET event_id = $4, receipt_ts = $5" + +type receiptStatements struct { + db *sql.DB + upsertReceipt *sql.Stmt +} + +func NewPostgresReceiptsTable(db *sql.DB) (tables.Receipts, error) { + _, err := db.Exec(receiptsSchema) + if err != nil { + return nil, err + } + r := &receiptStatements{ + db: db, + } + if r.upsertReceipt, err = db.Prepare(upsertReceipt); err != nil { + return nil, errors.Wrap(err, "unable to prepare upsertReceipt statement") + } + + return r, nil +} + +func (r *receiptStatements) UpsertReceipt(ctx context.Context, txn *sql.Tx, roomId, receiptType, userId, eventId string) error { + receiptTs := time.Now().UnixNano() / int64(time.Millisecond) + stmt := sqlutil.TxStmt(txn, r.upsertReceipt) + _, err := stmt.ExecContext(ctx, roomId, receiptType, userId, eventId, receiptTs) + return err +} diff --git a/roomserver/storage/postgres/storage.go b/roomserver/storage/postgres/storage.go index 02ff072d7..4e0e63984 100644 --- a/roomserver/storage/postgres/storage.go +++ b/roomserver/storage/postgres/storage.go @@ -97,6 +97,10 @@ func Open(dbProperties *config.DatabaseOptions, cache caching.RoomServerCaches) if err != nil { return nil, err } + receipts, err := NewPostgresReceiptsTable(db) + if err != nil { + return nil, err + } d.Database = shared.Database{ DB: db, Cache: cache, @@ -115,6 +119,7 @@ func Open(dbProperties *config.DatabaseOptions, cache caching.RoomServerCaches) MembershipTable: membership, PublishedTable: published, RedactionsTable: redactions, + ReceiptsTable: receipts, } return &d, nil } diff --git a/roomserver/storage/sqlite3/receipt_table.go b/roomserver/storage/sqlite3/receipt_table.go index 73108273f..bae699678 100644 --- a/roomserver/storage/sqlite3/receipt_table.go +++ b/roomserver/storage/sqlite3/receipt_table.go @@ -39,7 +39,7 @@ CREATE TABLE IF NOT EXISTS roomserver_receipts ( CONSTRAINT roomserver_receipts_unique UNIQUE (room_id, receipt_type, user_id) ); -CREATE INDEX IF NOT EXISTS roomserver_receipts_user_id ON roomserver_receipts(room_id); +CREATE INDEX IF NOT EXISTS roomserver_receipts_room_id ON roomserver_receipts(room_id); ` const upsertReceipt = "" +