Include the invites table when determining the maxInviteID

This commit is contained in:
Mark Haines 2017-09-20 14:33:25 +01:00
parent 6f0f922fad
commit 47bc9e16ab
2 changed files with 26 additions and 0 deletions

View file

@ -39,10 +39,14 @@ const selectInviteEventsInRangeSQL = "" +
" WHERE target_user_id = $1 AND id > $2 AND id <= $3" + " WHERE target_user_id = $1 AND id > $2 AND id <= $3" +
" ORDER BY id DESC" " ORDER BY id DESC"
const selectMaxInviteIDSQL = "" +
"SELECT MAX(id) FROM syncapi_invite_events"
type inviteEventsStatements struct { type inviteEventsStatements struct {
insertInviteEventStmt *sql.Stmt insertInviteEventStmt *sql.Stmt
selectInviteEventsInRangeStmt *sql.Stmt selectInviteEventsInRangeStmt *sql.Stmt
deleteInviteEventStmt *sql.Stmt deleteInviteEventStmt *sql.Stmt
selectMaxInviteIDStmt *sql.Stmt
} }
func (s *inviteEventsStatements) prepare(db *sql.DB) (err error) { 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 { if s.deleteInviteEventStmt, err = db.Prepare(deleteInviteEventSQL); err != nil {
return return
} }
if s.selectMaxInviteIDStmt, err = db.Prepare(selectMaxInviteIDSQL); err != nil {
return
}
return return
} }
@ -112,3 +119,15 @@ func (s *inviteEventsStatements) selectInviteEventsInRange(
} }
return result, nil 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
}

View file

@ -191,6 +191,13 @@ func (d *SyncServerDatabase) syncStreamPositionTx(
if maxAccountDataID > maxID { if maxAccountDataID > maxID {
maxID = maxAccountDataID 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 return types.StreamPosition(maxID), nil
} }