Skip to content

Commit

Permalink
tapdb: change InsertNewAsset into UpsertAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
ffranr committed Nov 17, 2023
1 parent 671e617 commit 66a73c2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 50 deletions.
12 changes: 6 additions & 6 deletions tapdb/assets_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ type UpsertAssetStore interface {
UpsertAssetGroupKey(ctx context.Context, arg AssetGroupKey) (int64,
error)

// InsertNewAsset inserts a new asset on disk.
InsertNewAsset(ctx context.Context,
arg sqlc.InsertNewAssetParams) (int64, error)
// UpsertAsset upserts an asset on disk.
UpsertAsset(ctx context.Context,
arg sqlc.UpsertAssetParams) (int64, error)

// UpsertAssetMeta inserts a new asset meta into the DB.
UpsertAssetMeta(ctx context.Context, arg NewAssetMeta) (int64, error)
Expand Down Expand Up @@ -200,10 +200,10 @@ func upsertAssetsWithGenesis(ctx context.Context, q UpsertAssetStore,
anchorUtxoID = anchorUtxoIDs[idx]
}

// With all the dependent data inserted, we can now insert the
// With all the dependent data inserted, we can now upsert the
// base asset information itself.
assetIDs[idx], err = q.InsertNewAsset(
ctx, sqlc.InsertNewAssetParams{
assetIDs[idx], err = q.UpsertAsset(
ctx, sqlc.UpsertAssetParams{
GenesisID: genAssetID,
Version: int32(a.Version),
ScriptKeyID: scriptKeyID,
Expand Down
91 changes: 51 additions & 40 deletions tapdb/sqlc/assets.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tapdb/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions tapdb/sqlc/queries/assets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,24 @@ INSERT INTO genesis_assets (
DO UPDATE SET asset_id = EXCLUDED.asset_id
RETURNING gen_asset_id;

-- name: InsertNewAsset :one
-- name: UpsertAsset :one
INSERT INTO assets (
genesis_id, version, script_key_id, asset_group_witness_id, script_version,
amount, lock_time, relative_lock_time, anchor_utxo_id, spent
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10
) RETURNING asset_id;
@genesis_id, @version, @script_key_id, @asset_group_witness_id,
@script_version, @amount, @lock_time, @relative_lock_time, @anchor_utxo_id,
@spent
) ON CONFLICT (anchor_utxo_id, genesis_id, script_key_id)
DO UPDATE SET
version = @version,
asset_group_witness_id = @asset_group_witness_id,
script_version = @script_version,
amount = @amount,
lock_time = @lock_time,
relative_lock_time = @relative_lock_time,
spent = @spent
RETURNING asset_id;

-- name: FetchAssetsForBatch :many
WITH genesis_info AS (
Expand Down

0 comments on commit 66a73c2

Please sign in to comment.