From 47bc9e16ab5d9056ebf50658ff65193b2f209d56 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Wed, 20 Sep 2017 14:33:25 +0100 Subject: [PATCH] Include the invites table when determining the maxInviteID --- .../dendrite/syncapi/storage/invites_table.go | 19 +++++++++++++++++++ .../dendrite/syncapi/storage/syncserver.go | 7 +++++++ 2 files changed, 26 insertions(+) diff --git a/src/github.com/matrix-org/dendrite/syncapi/storage/invites_table.go b/src/github.com/matrix-org/dendrite/syncapi/storage/invites_table.go index 68087244d..88c98f7e3 100644 --- a/src/github.com/matrix-org/dendrite/syncapi/storage/invites_table.go +++ b/src/github.com/matrix-org/dendrite/syncapi/storage/invites_table.go @@ -39,10 +39,14 @@ const selectInviteEventsInRangeSQL = "" + " WHERE target_user_id = $1 AND id > $2 AND id <= $3" + " ORDER BY id DESC" +const selectMaxInviteIDSQL = "" + + "SELECT MAX(id) FROM syncapi_invite_events" + type inviteEventsStatements struct { insertInviteEventStmt *sql.Stmt selectInviteEventsInRangeStmt *sql.Stmt deleteInviteEventStmt *sql.Stmt + selectMaxInviteIDStmt *sql.Stmt } func (s *inviteEventsStatements) prepare(db *sql.DB) (err error) { @@ -59,6 +63,9 @@ func (s *inviteEventsStatements) prepare(db *sql.DB) (err error) { if s.deleteInviteEventStmt, err = db.Prepare(deleteInviteEventSQL); err != nil { return } + if s.selectMaxInviteIDStmt, err = db.Prepare(selectMaxInviteIDSQL); err != nil { + return + } return } @@ -112,3 +119,15 @@ func (s *inviteEventsStatements) selectInviteEventsInRange( } return result, nil } + +func (s *inviteEventsStatements) selectMaxInviteID( + ctx context.Context, txn *sql.Tx, +) (id int64, err error) { + var nullableID sql.NullInt64 + stmt := common.TxStmt(txn, s.selectMaxInviteIDStmt) + err = stmt.QueryRowContext(ctx).Scan(&nullableID) + if nullableID.Valid { + id = nullableID.Int64 + } + return +} diff --git a/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go b/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go index 4b7d7e4c8..bb6b028bb 100644 --- a/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go +++ b/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go @@ -191,6 +191,13 @@ func (d *SyncServerDatabase) syncStreamPositionTx( if maxAccountDataID > maxID { maxID = maxAccountDataID } + maxInviteID, err := d.invites.selectMaxInviteID(ctx, txn) + if err != nil { + return 0, err + } + if maxInviteID > maxID { + maxID = maxInviteID + } return types.StreamPosition(maxID), nil }