multi: use sqldb.NoOpReset helper

Define a re-usable "reset" function, sqldb.NoOpReset, that can be used
for the reset parameter in sql ExecTx calls.
This commit is contained in:
Elle Mouton
2025-05-27 18:59:44 +02:00
parent c4e6f23c5b
commit 9cbc1f804e
5 changed files with 19 additions and 15 deletions

View File

@ -177,7 +177,7 @@ func (s *SQLStore) FetchLightningNode(pubKey route.Vertex) (
_, node, err = getNodeByPubKey(ctx, db, pubKey) _, node, err = getNodeByPubKey(ctx, db, pubKey)
return err return err
}, func() {}) }, sqldb.NoOpReset)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to fetch node: %w", err) return nil, fmt.Errorf("unable to fetch node: %w", err)
} }
@ -221,7 +221,7 @@ func (s *SQLStore) HasLightningNode(pubKey [33]byte) (time.Time, bool,
} }
return nil return nil
}, func() {}) }, sqldb.NoOpReset)
if err != nil { if err != nil {
return time.Time{}, false, return time.Time{}, false,
fmt.Errorf("unable to fetch node: %w", err) fmt.Errorf("unable to fetch node: %w", err)
@ -255,7 +255,7 @@ func (s *SQLStore) AddrsForNode(nodePub *btcec.PublicKey) (bool, []net.Addr,
} }
return nil return nil
}, func() {}) }, sqldb.NoOpReset)
if err != nil { if err != nil {
return false, nil, fmt.Errorf("unable to get addresses for "+ return false, nil, fmt.Errorf("unable to get addresses for "+
"node(%x): %w", nodePub.SerializeCompressed(), err) "node(%x): %w", nodePub.SerializeCompressed(), err)
@ -294,7 +294,7 @@ func (s *SQLStore) DeleteLightningNode(pubKey route.Vertex) error {
} }
return err return err
}, func() {}) }, sqldb.NoOpReset)
if err != nil { if err != nil {
return fmt.Errorf("unable to delete node: %w", err) return fmt.Errorf("unable to delete node: %w", err)
} }
@ -342,7 +342,7 @@ func (s *SQLStore) LookupAlias(pub *btcec.PublicKey) (string, error) {
alias = dbNode.Alias.String alias = dbNode.Alias.String
return nil return nil
}, func() {}) }, sqldb.NoOpReset)
if err != nil { if err != nil {
return "", fmt.Errorf("unable to look up alias: %w", err) return "", fmt.Errorf("unable to look up alias: %w", err)
} }
@ -370,7 +370,7 @@ func (s *SQLStore) SourceNode() (*models.LightningNode, error) {
_, node, err = getNodeByPubKey(ctx, db, nodePub) _, node, err = getNodeByPubKey(ctx, db, nodePub)
return err return err
}, func() {}) }, sqldb.NoOpReset)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to fetch source node: %w", err) return nil, fmt.Errorf("unable to fetch source node: %w", err)
} }
@ -410,7 +410,7 @@ func (s *SQLStore) SetSourceNode(node *models.LightningNode) error {
} }
return db.AddSourceNode(ctx, id) return db.AddSourceNode(ctx, id)
}, func() {}) }, sqldb.NoOpReset)
} }
// NodeUpdatesInHorizon returns all the known lightning node which have an // NodeUpdatesInHorizon returns all the known lightning node which have an
@ -447,7 +447,7 @@ func (s *SQLStore) NodeUpdatesInHorizon(startTime,
} }
return nil return nil
}, func() {}) }, sqldb.NoOpReset)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to fetch nodes: %w", err) return nil, fmt.Errorf("unable to fetch nodes: %w", err)
} }

View File

@ -76,7 +76,7 @@ func TestMigrationWithChannelDB(t *testing.T) {
ctxb, kvStore.Backend, kvStore, tx, ctxb, kvStore.Backend, kvStore, tx,
batchSize, batchSize,
) )
}, func() {}, }, sqldb.NoOpReset,
) )
require.NoError(t, err) require.NoError(t, err)

View File

@ -325,7 +325,7 @@ func testMigrateSingleInvoiceRapid(t *rapid.T, store *SQLStore, mpp bool,
} }
return nil return nil
}, func() {}) }, sqldb.NoOpReset)
require.NoError(t, err) require.NoError(t, err)
// Fetch and compare each migrated invoice from the store with the // Fetch and compare each migrated invoice from the store with the

View File

@ -322,7 +322,7 @@ func (i *SQLStore) AddInvoice(ctx context.Context,
AddedAt: newInvoice.CreationDate.UTC(), AddedAt: newInvoice.CreationDate.UTC(),
InvoiceID: invoiceID, InvoiceID: invoiceID,
}) })
}, func() {}) }, sqldb.NoOpReset)
if err != nil { if err != nil {
mappedSQLErr := sqldb.MapSQLError(err) mappedSQLErr := sqldb.MapSQLError(err)
var uniqueConstraintErr *sqldb.ErrSQLUniqueConstraintViolation var uniqueConstraintErr *sqldb.ErrSQLUniqueConstraintViolation
@ -702,7 +702,7 @@ func (i *SQLStore) LookupInvoice(ctx context.Context,
invoice, err = fetchInvoice(ctx, db, ref) invoice, err = fetchInvoice(ctx, db, ref)
return err return err
}, func() {}) }, sqldb.NoOpReset)
if txErr != nil { if txErr != nil {
return Invoice{}, txErr return Invoice{}, txErr
} }
@ -1488,7 +1488,7 @@ func (i *SQLStore) UpdateInvoice(ctx context.Context, ref InvoiceRef,
) )
return err return err
}, func() {}) }, sqldb.NoOpReset)
if txErr != nil { if txErr != nil {
// If the invoice is already settled, we'll return the // If the invoice is already settled, we'll return the
// (unchanged) invoice and the ErrInvoiceAlreadySettled error. // (unchanged) invoice and the ErrInvoiceAlreadySettled error.
@ -1552,7 +1552,7 @@ func (i *SQLStore) DeleteInvoice(ctx context.Context,
} }
return nil return nil
}, func() {}) }, sqldb.NoOpReset)
if err != nil { if err != nil {
return fmt.Errorf("unable to delete invoices: %w", err) return fmt.Errorf("unable to delete invoices: %w", err)
@ -1572,7 +1572,7 @@ func (i *SQLStore) DeleteCanceledInvoices(ctx context.Context) error {
} }
return nil return nil
}, func() {}) }, sqldb.NoOpReset)
if err != nil { if err != nil {
return fmt.Errorf("unable to delete invoices: %w", err) return fmt.Errorf("unable to delete invoices: %w", err)
} }

View File

@ -7,6 +7,10 @@ import (
"golang.org/x/exp/constraints" "golang.org/x/exp/constraints"
) )
// NoOpReset is a no-op function that can be used as a default
// reset function ExecTx calls.
var NoOpReset = func() {}
// SQLInt32 turns a numerical integer type into the NullInt32 that sql/sqlc // SQLInt32 turns a numerical integer type into the NullInt32 that sql/sqlc
// uses when an integer field can be permitted to be NULL. // uses when an integer field can be permitted to be NULL.
// //