Skip to content

Commit 939c9b4

Browse files
committed
loopdb+sweepbatcher: add the DropBatch call
1 parent 38f0e3a commit 939c9b4

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

loopdb/sqlc/batch.sql.go

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

loopdb/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.

loopdb/sqlc/queries/batch.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ INSERT INTO sweep_batches (
2323
$6
2424
) RETURNING id;
2525

26+
-- name: DropBatch :exec
27+
DELETE FROM sweep_batches WHERE id = $1;
28+
2629
-- name: UpdateBatch :exec
2730
UPDATE sweep_batches SET
2831
confirmed = $2,

sweepbatcher/store.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sweepbatcher
33
import (
44
"context"
55
"database/sql"
6+
"fmt"
67

78
"github.com/btcsuite/btcd/btcutil"
89
"github.com/btcsuite/btcd/chaincfg"
@@ -46,6 +47,9 @@ type BaseDB interface {
4647
InsertBatch(ctx context.Context, arg sqlc.InsertBatchParams) (
4748
int32, error)
4849

50+
// DropBatch drops a batch from the database.
51+
DropBatch(ctx context.Context, id int32) error
52+
4953
// UpdateBatch updates a batch in the database.
5054
UpdateBatch(ctx context.Context, arg sqlc.UpdateBatchParams) error
5155

@@ -108,6 +112,24 @@ func (s *SQLStore) InsertSweepBatch(ctx context.Context, batch *dbBatch) (int32,
108112
return s.baseDb.InsertBatch(ctx, batchToInsertArgs(*batch))
109113
}
110114

115+
// DropBatch drops a batch from the database. Note that we only use this call
116+
// for batches that have no sweeps and so we'd not be able to resume.
117+
func (s *SQLStore) DropBatch(ctx context.Context, id int32) error {
118+
readOpts := loopdb.NewSqlReadOpts()
119+
return s.baseDb.ExecTx(ctx, readOpts, func(tx *sqlc.Queries) error {
120+
dbSweeps, err := tx.GetBatchSweeps(ctx, id)
121+
if err != nil {
122+
return err
123+
}
124+
125+
if len(dbSweeps) != 0 {
126+
return fmt.Errorf("cannot drop a non-empty batch")
127+
}
128+
129+
return tx.DropBatch(ctx, id)
130+
})
131+
}
132+
111133
// UpdateSweepBatch updates a batch in the database.
112134
func (s *SQLStore) UpdateSweepBatch(ctx context.Context, batch *dbBatch) error {
113135
return s.baseDb.UpdateBatch(ctx, batchToUpdateArgs(*batch))

sweepbatcher/store_mock.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ func (s *StoreMock) InsertSweepBatch(ctx context.Context,
5656
return id, nil
5757
}
5858

59+
// DropBatch drops a batch from the database.
60+
func (s *StoreMock) DropBatch(ctx context.Context, id int32) error {
61+
delete(s.batches, id)
62+
return nil
63+
}
64+
5965
// UpdateSweepBatch updates a batch in the database.
6066
func (s *StoreMock) UpdateSweepBatch(ctx context.Context,
6167
batch *dbBatch) error {

0 commit comments

Comments
 (0)