From 4ccb56a7712afb943d08bff09d8e85d5b57504f4 Mon Sep 17 00:00:00 2001 From: Viktor Torstensson Date: Tue, 9 Sep 2025 15:04:07 +0200 Subject: [PATCH] multi: add `UpdateAccountAliasForTests` query In the upcoming kvdb to SQL migration of the actions store, we need to simulate in tests that two or more accounts have colliding account aliases for the first 4 bytes of the alias. In order to allow creation of such accounts, we need to be able to update the alias of an account in tests, and this commit adds the a SQL query enabling this functionality. Note that the `UpdateAccountAliasForTests` query is only intended for use in tests and should not be used in production code. --- accounts/store_sql.go | 3 +++ db/sqlc/accounts.sql.go | 20 ++++++++++++++++++++ db/sqlc/querier.go | 2 ++ db/sqlc/queries/accounts.sql | 7 +++++++ 4 files changed, 32 insertions(+) diff --git a/accounts/store_sql.go b/accounts/store_sql.go index 830f1658..2fc6d229 100644 --- a/accounts/store_sql.go +++ b/accounts/store_sql.go @@ -49,6 +49,9 @@ type SQLQueries interface { UpdateAccountBalance(ctx context.Context, arg sqlc.UpdateAccountBalanceParams) (int64, error) UpdateAccountExpiry(ctx context.Context, arg sqlc.UpdateAccountExpiryParams) (int64, error) UpdateAccountLastUpdate(ctx context.Context, arg sqlc.UpdateAccountLastUpdateParams) (int64, error) + // UpdateAccountAliasForTests is a query intended only for testing + // purposes, to change the account alias. + UpdateAccountAliasForTests(ctx context.Context, arg sqlc.UpdateAccountAliasForTestsParams) (int64, error) UpsertAccountPayment(ctx context.Context, arg sqlc.UpsertAccountPaymentParams) error GetAccountInvoice(ctx context.Context, arg sqlc.GetAccountInvoiceParams) (sqlc.AccountInvoice, error) } diff --git a/db/sqlc/accounts.sql.go b/db/sqlc/accounts.sql.go index f6b3fc81..33334dbc 100644 --- a/db/sqlc/accounts.sql.go +++ b/db/sqlc/accounts.sql.go @@ -313,6 +313,26 @@ func (q *Queries) SetAccountIndex(ctx context.Context, arg SetAccountIndexParams return err } +const updateAccountAliasForTests = `-- name: UpdateAccountAliasForTests :one +UPDATE accounts +SET alias = $1 +WHERE id = $2 + RETURNING id +` + +type UpdateAccountAliasForTestsParams struct { + Alias int64 + ID int64 +} + +// NOTE: This query is only intended for testing purposes. +func (q *Queries) UpdateAccountAliasForTests(ctx context.Context, arg UpdateAccountAliasForTestsParams) (int64, error) { + row := q.db.QueryRowContext(ctx, updateAccountAliasForTests, arg.Alias, arg.ID) + var id int64 + err := row.Scan(&id) + return id, err +} + const updateAccountBalance = `-- name: UpdateAccountBalance :one UPDATE accounts SET current_balance_msat = $1 diff --git a/db/sqlc/querier.go b/db/sqlc/querier.go index 4ff08707..3e261503 100644 --- a/db/sqlc/querier.go +++ b/db/sqlc/querier.go @@ -67,6 +67,8 @@ type Querier interface { SetSessionGroupID(ctx context.Context, arg SetSessionGroupIDParams) error SetSessionRemotePublicKey(ctx context.Context, arg SetSessionRemotePublicKeyParams) error SetSessionRevokedAt(ctx context.Context, arg SetSessionRevokedAtParams) error + // NOTE: This query is only intended for testing purposes. + UpdateAccountAliasForTests(ctx context.Context, arg UpdateAccountAliasForTestsParams) (int64, error) UpdateAccountBalance(ctx context.Context, arg UpdateAccountBalanceParams) (int64, error) UpdateAccountExpiry(ctx context.Context, arg UpdateAccountExpiryParams) (int64, error) UpdateAccountLastUpdate(ctx context.Context, arg UpdateAccountLastUpdateParams) (int64, error) diff --git a/db/sqlc/queries/accounts.sql b/db/sqlc/queries/accounts.sql index 9c23c8c4..3be5ccd5 100644 --- a/db/sqlc/queries/accounts.sql +++ b/db/sqlc/queries/accounts.sql @@ -25,6 +25,13 @@ RETURNING id; INSERT INTO account_invoices (account_id, hash) VALUES ($1, $2); +-- name: UpdateAccountAliasForTests :one +-- NOTE: This query is only intended for testing purposes. +UPDATE accounts +SET alias = $1 +WHERE id = $2 + RETURNING id; + -- name: DeleteAccountPayment :exec DELETE FROM account_payments WHERE hash = $1