pessistic locking upsert on limit_check_locks (#287)

This commit is contained in:
Michael Quigley 2024-05-15 15:18:53 -04:00
parent f0b0a959f0
commit 2a770cc3b8
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 5 additions and 5 deletions

View File

@ -6,12 +6,12 @@ import (
)
func (str *Store) LimitCheckLock(acctId int, trx *sqlx.Tx) error {
rows, err := trx.Queryx("select * from limit_check_locks where account_id = $1 for update", acctId)
stmt, err := trx.Prepare("insert into limit_check_locks (account_id) values ($1) on conflict (account_id) do update set updated_at = current_timestamp")
if err != nil {
return errors.Wrap(err, "error preparing limit_check_locks select statement")
return errors.Wrap(err, "error preparing upsert on limit_check_locks")
}
if !rows.Next() {
return errors.Errorf("no limit_check_locks entry for account_id '%d'", acctId)
if _, err := stmt.Exec(acctId); err != nil {
return errors.Wrap(err, "error executing upsert on limit_check_locks")
}
return nil
}

View File

@ -2,6 +2,6 @@
create table limit_check_locks (
id serial primary key,
account_id integer not null references accounts (id),
account_id integer not null references accounts (id) unique,
updated_at timestamptz not null default(current_timestamp)
);