Split the select+update query for txn_id counter (#1855)

The update part wasn't executed actually for SQLite, so it is moved to
a separate statement.

Fixes #1852.

Signed-off-by: Bohdan Horbeshko <bodqhrohro@gmail.com>
This commit is contained in:
bodqhrohro 2021-06-07 10:51:19 +03:00 committed by GitHub
parent b0aa101dcd
commit f18001ecbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,14 +32,18 @@ INSERT OR IGNORE INTO appservice_counters (name, last_id) VALUES('txn_id', 1);
` `
const selectTxnIDSQL = ` const selectTxnIDSQL = `
SELECT last_id FROM appservice_counters WHERE name='txn_id'; SELECT last_id FROM appservice_counters WHERE name='txn_id'
UPDATE appservice_counters SET last_id=last_id+1 WHERE name='txn_id'; `
const updateTxnIDSQL = `
UPDATE appservice_counters SET last_id=last_id+1 WHERE name='txn_id'
` `
type txnStatements struct { type txnStatements struct {
db *sql.DB db *sql.DB
writer sqlutil.Writer writer sqlutil.Writer
selectTxnIDStmt *sql.Stmt selectTxnIDStmt *sql.Stmt
updateTxnIDStmt *sql.Stmt
} }
func (s *txnStatements) prepare(db *sql.DB, writer sqlutil.Writer) (err error) { func (s *txnStatements) prepare(db *sql.DB, writer sqlutil.Writer) (err error) {
@ -54,6 +58,10 @@ func (s *txnStatements) prepare(db *sql.DB, writer sqlutil.Writer) (err error) {
return return
} }
if s.updateTxnIDStmt, err = db.Prepare(updateTxnIDSQL); err != nil {
return
}
return return
} }
@ -63,6 +71,11 @@ func (s *txnStatements) selectTxnID(
) (txnID int, err error) { ) (txnID int, err error) {
err = s.writer.Do(s.db, nil, func(txn *sql.Tx) error { err = s.writer.Do(s.db, nil, func(txn *sql.Tx) error {
err := s.selectTxnIDStmt.QueryRowContext(ctx).Scan(&txnID) err := s.selectTxnIDStmt.QueryRowContext(ctx).Scan(&txnID)
if err != nil {
return err
}
_, err = s.updateTxnIDStmt.ExecContext(ctx)
return err return err
}) })
return return