Skip to content

Commit 44ae7e0

Browse files
accounts+db: Add sql db CreditAccount query
This commit adds a separate `CreditAccount` query to the sql db, to incentivize not setting the balance directly, but rather instead increase and decrease the balance by a specific amount. We also update the sql store to use the new query function, instead of setting the balance directly.
1 parent 21c0677 commit 44ae7e0

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

accounts/store_sql.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const (
3434
//nolint:lll
3535
type SQLQueries interface {
3636
AddAccountInvoice(ctx context.Context, arg sqlc.AddAccountInvoiceParams) error
37+
CreditAccount(ctx context.Context, arg sqlc.CreditAccountParams) (int64, error)
3738
DeleteAccount(ctx context.Context, id int64) error
3839
DeleteAccountPayment(ctx context.Context, arg sqlc.DeleteAccountPaymentParams) error
3940
GetAccount(ctx context.Context, id int64) (sqlc.Account, error)
@@ -396,17 +397,10 @@ func (s *SQLStore) IncreaseAccountBalance(ctx context.Context, alias AccountID,
396397
return err
397398
}
398399

399-
acct, err := db.GetAccount(ctx, id)
400-
if err != nil {
401-
return err
402-
}
403-
404-
newBalance := acct.CurrentBalanceMsat + int64(amount)
405-
406-
_, err = db.UpdateAccountBalance(
407-
ctx, sqlc.UpdateAccountBalanceParams{
408-
ID: id,
409-
CurrentBalanceMsat: newBalance,
400+
_, err = db.CreditAccount(
401+
ctx, sqlc.CreditAccountParams{
402+
ID: id,
403+
Amount: int64(amount),
410404
},
411405
)
412406
if err != nil {

db/sqlc/accounts.sql.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

db/sqlc/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

db/sqlc/queries/accounts.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ SET current_balance_msat = $1
99
WHERE id = $2
1010
RETURNING id;
1111

12+
-- name: CreditAccount :one
13+
UPDATE accounts
14+
SET current_balance_msat = current_balance_msat + sqlc.arg(amount)
15+
WHERE id = $1
16+
RETURNING id;
17+
1218
-- name: UpdateAccountExpiry :one
1319
UPDATE accounts
1420
SET expiration = $1

0 commit comments

Comments
 (0)