mirror of
https://github.com/lightninglabs/loop
synced 2024-11-08 01:10:29 +00:00
loopdb+sweepbatcher: add the DropBatch call
This commit is contained in:
parent
38f0e3a1f5
commit
939c9b4ccf
@ -25,6 +25,15 @@ func (q *Queries) ConfirmBatch(ctx context.Context, id int32) error {
|
||||
return err
|
||||
}
|
||||
|
||||
const dropBatch = `-- name: DropBatch :exec
|
||||
DELETE FROM sweep_batches WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DropBatch(ctx context.Context, id int32) error {
|
||||
_, err := q.db.ExecContext(ctx, dropBatch, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const getBatchSweeps = `-- name: GetBatchSweeps :many
|
||||
SELECT
|
||||
sweeps.id, sweeps.swap_hash, sweeps.batch_id, sweeps.outpoint_txid, sweeps.outpoint_index, sweeps.amt, sweeps.completed,
|
||||
|
@ -11,6 +11,7 @@ import (
|
||||
type Querier interface {
|
||||
ConfirmBatch(ctx context.Context, id int32) error
|
||||
CreateReservation(ctx context.Context, arg CreateReservationParams) error
|
||||
DropBatch(ctx context.Context, id int32) error
|
||||
FetchLiquidityParams(ctx context.Context) ([]byte, error)
|
||||
GetBatchSweeps(ctx context.Context, batchID int32) ([]GetBatchSweepsRow, error)
|
||||
GetBatchSweptAmount(ctx context.Context, batchID int32) (int64, error)
|
||||
|
@ -23,6 +23,9 @@ INSERT INTO sweep_batches (
|
||||
$6
|
||||
) RETURNING id;
|
||||
|
||||
-- name: DropBatch :exec
|
||||
DELETE FROM sweep_batches WHERE id = $1;
|
||||
|
||||
-- name: UpdateBatch :exec
|
||||
UPDATE sweep_batches SET
|
||||
confirmed = $2,
|
||||
|
@ -3,6 +3,7 @@ package sweepbatcher
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/btcsuite/btcd/btcutil"
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
@ -46,6 +47,9 @@ type BaseDB interface {
|
||||
InsertBatch(ctx context.Context, arg sqlc.InsertBatchParams) (
|
||||
int32, error)
|
||||
|
||||
// DropBatch drops a batch from the database.
|
||||
DropBatch(ctx context.Context, id int32) error
|
||||
|
||||
// UpdateBatch updates a batch in the database.
|
||||
UpdateBatch(ctx context.Context, arg sqlc.UpdateBatchParams) error
|
||||
|
||||
@ -108,6 +112,24 @@ func (s *SQLStore) InsertSweepBatch(ctx context.Context, batch *dbBatch) (int32,
|
||||
return s.baseDb.InsertBatch(ctx, batchToInsertArgs(*batch))
|
||||
}
|
||||
|
||||
// DropBatch drops a batch from the database. Note that we only use this call
|
||||
// for batches that have no sweeps and so we'd not be able to resume.
|
||||
func (s *SQLStore) DropBatch(ctx context.Context, id int32) error {
|
||||
readOpts := loopdb.NewSqlReadOpts()
|
||||
return s.baseDb.ExecTx(ctx, readOpts, func(tx *sqlc.Queries) error {
|
||||
dbSweeps, err := tx.GetBatchSweeps(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(dbSweeps) != 0 {
|
||||
return fmt.Errorf("cannot drop a non-empty batch")
|
||||
}
|
||||
|
||||
return tx.DropBatch(ctx, id)
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateSweepBatch updates a batch in the database.
|
||||
func (s *SQLStore) UpdateSweepBatch(ctx context.Context, batch *dbBatch) error {
|
||||
return s.baseDb.UpdateBatch(ctx, batchToUpdateArgs(*batch))
|
||||
|
@ -56,6 +56,12 @@ func (s *StoreMock) InsertSweepBatch(ctx context.Context,
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// DropBatch drops a batch from the database.
|
||||
func (s *StoreMock) DropBatch(ctx context.Context, id int32) error {
|
||||
delete(s.batches, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateSweepBatch updates a batch in the database.
|
||||
func (s *StoreMock) UpdateSweepBatch(ctx context.Context,
|
||||
batch *dbBatch) error {
|
||||
|
Loading…
Reference in New Issue
Block a user