Use RETURNING because LastInsertID is not supported by postgres

This commit is contained in:
Neil Alexander 2020-06-30 14:10:24 +01:00
parent 693928396d
commit a6aaa5aa52
2 changed files with 8 additions and 10 deletions

View file

@ -39,7 +39,7 @@ CREATE TABLE IF NOT EXISTS federationsender_queue_json (
const insertJSONSQL = "" +
"INSERT INTO federationsender_queue_json (json_body)" +
" VALUES ($1)" +
" ON CONFLICT DO NOTHING"
" RETURNING json_nid"
const deleteJSONSQL = "" +
"DELETE FROM federationsender_queue_json WHERE json_nid = ANY($1)"
@ -75,12 +75,11 @@ func (s *queueJSONStatements) insertQueueJSON(
ctx context.Context, txn *sql.Tx, json string,
) (int64, error) {
stmt := sqlutil.TxStmt(txn, s.insertJSONStmt)
res, err := stmt.ExecContext(ctx, json)
if err != nil {
var lastid int64
if err := stmt.QueryRowContext(ctx, json).Scan(&lastid); err != nil {
return 0, err
}
lastid, err := res.LastInsertId()
return lastid, err
return lastid, nil
}
func (s *queueJSONStatements) deleteQueueJSON(

View file

@ -39,7 +39,7 @@ CREATE TABLE IF NOT EXISTS federationsender_queue_json (
const insertJSONSQL = "" +
"INSERT INTO federationsender_queue_json (json_body)" +
" VALUES ($1)" +
" ON CONFLICT DO NOTHING"
" RETURNING json_nid"
const deleteJSONSQL = "" +
"DELETE FROM federationsender_queue_json WHERE json_nid = ANY($1)"
@ -75,12 +75,11 @@ func (s *queueJSONStatements) insertQueueJSON(
ctx context.Context, txn *sql.Tx, json string,
) (int64, error) {
stmt := sqlutil.TxStmt(txn, s.insertJSONStmt)
res, err := stmt.ExecContext(ctx, json)
if err != nil {
var lastid int64
if err := stmt.QueryRowContext(ctx, json).Scan(&lastid); err != nil {
return 0, err
}
lastid, err := res.LastInsertId()
return lastid, err
return lastid, nil
}
func (s *queueJSONStatements) deleteQueueJSON(