Skip to content

Commit cd5d422

Browse files
committed
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 in the `CreditAccount` method.
1 parent e10ae03 commit cd5d422

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
DebitAccount(ctx context.Context, arg sqlc.DebitAccountParams) (int64, error)
3839
DeleteAccount(ctx context.Context, id int64) error
3940
DeleteAccountPayment(ctx context.Context, arg sqlc.DeleteAccountPaymentParams) error
@@ -397,17 +398,10 @@ func (s *SQLStore) CreditAccount(ctx context.Context, alias AccountID,
397398
return err
398399
}
399400

400-
acct, err := db.GetAccount(ctx, id)
401-
if err != nil {
402-
return err
403-
}
404-
405-
newBalance := acct.CurrentBalanceMsat + int64(amount)
406-
407-
_, err = db.UpdateAccountBalance(
408-
ctx, sqlc.UpdateAccountBalanceParams{
409-
ID: id,
410-
CurrentBalanceMsat: newBalance,
401+
_, err = db.CreditAccount(
402+
ctx, sqlc.CreditAccountParams{
403+
ID: id,
404+
Amount: int64(amount),
411405
},
412406
)
413407
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 = sqlc.arg(id)
16+
RETURNING id;
17+
1218
-- name: DebitAccount :one
1319
UPDATE accounts
1420
SET current_balance_msat = current_balance_msat - sqlc.arg(amount)

0 commit comments

Comments
 (0)