diff --git a/tapdb/assets_common.go b/tapdb/assets_common.go index d131b3586..caba8e29e 100644 --- a/tapdb/assets_common.go +++ b/tapdb/assets_common.go @@ -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) @@ -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, diff --git a/tapdb/sqlc/assets.sql.go b/tapdb/sqlc/assets.sql.go index 9289c1081..8dfdf66ff 100644 --- a/tapdb/sqlc/assets.sql.go +++ b/tapdb/sqlc/assets.sql.go @@ -1713,46 +1713,6 @@ func (q *Queries) InsertAssetWitness(ctx context.Context, arg InsertAssetWitness return err } -const insertNewAsset = `-- name: InsertNewAsset :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 -` - -type InsertNewAssetParams struct { - GenesisID int64 - Version int32 - ScriptKeyID int64 - AssetGroupWitnessID sql.NullInt64 - ScriptVersion int32 - Amount int64 - LockTime sql.NullInt32 - RelativeLockTime sql.NullInt32 - AnchorUtxoID sql.NullInt64 - Spent bool -} - -func (q *Queries) InsertNewAsset(ctx context.Context, arg InsertNewAssetParams) (int64, error) { - row := q.db.QueryRowContext(ctx, insertNewAsset, - arg.GenesisID, - arg.Version, - arg.ScriptKeyID, - arg.AssetGroupWitnessID, - arg.ScriptVersion, - arg.Amount, - arg.LockTime, - arg.RelativeLockTime, - arg.AnchorUtxoID, - arg.Spent, - ) - var asset_id int64 - err := row.Scan(&asset_id) - return asset_id, err -} - const newMintingBatch = `-- name: NewMintingBatch :exec INSERT INTO asset_minting_batches ( batch_state, batch_id, height_hint, creation_time_unix @@ -2181,6 +2141,57 @@ func (q *Queries) UpdateUTXOLease(ctx context.Context, arg UpdateUTXOLeaseParams return err } +const upsertAsset = `-- 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 +) ON CONFLICT (anchor_utxo_id, genesis_id, script_key_id) +DO UPDATE SET + version = $2, + asset_group_witness_id = $4, + script_version = $5, + amount = $6, + lock_time = $7, + relative_lock_time = $8, + spent = $10 +RETURNING asset_id +` + +type UpsertAssetParams struct { + GenesisID int64 + Version int32 + ScriptKeyID int64 + AssetGroupWitnessID sql.NullInt64 + ScriptVersion int32 + Amount int64 + LockTime sql.NullInt32 + RelativeLockTime sql.NullInt32 + AnchorUtxoID sql.NullInt64 + Spent bool +} + +func (q *Queries) UpsertAsset(ctx context.Context, arg UpsertAssetParams) (int64, error) { + row := q.db.QueryRowContext(ctx, upsertAsset, + arg.GenesisID, + arg.Version, + arg.ScriptKeyID, + arg.AssetGroupWitnessID, + arg.ScriptVersion, + arg.Amount, + arg.LockTime, + arg.RelativeLockTime, + arg.AnchorUtxoID, + arg.Spent, + ) + var asset_id int64 + err := row.Scan(&asset_id) + return asset_id, err +} + const upsertAssetGroupKey = `-- name: UpsertAssetGroupKey :one INSERT INTO asset_groups ( tweaked_group_key, tapscript_root, internal_key_id, genesis_point_id diff --git a/tapdb/sqlc/querier.go b/tapdb/sqlc/querier.go index ee001f911..55e185305 100644 --- a/tapdb/sqlc/querier.go +++ b/tapdb/sqlc/querier.go @@ -88,7 +88,6 @@ type Querier interface { InsertBranch(ctx context.Context, arg InsertBranchParams) error InsertCompactedLeaf(ctx context.Context, arg InsertCompactedLeafParams) error InsertLeaf(ctx context.Context, arg InsertLeafParams) error - InsertNewAsset(ctx context.Context, arg InsertNewAssetParams) (int64, error) InsertNewProofEvent(ctx context.Context, arg InsertNewProofEventParams) error InsertNewSyncEvent(ctx context.Context, arg InsertNewSyncEventParams) error InsertPassiveAsset(ctx context.Context, arg InsertPassiveAssetParams) error @@ -139,6 +138,7 @@ type Querier interface { UpdateMintingBatchState(ctx context.Context, arg UpdateMintingBatchStateParams) error UpdateUTXOLease(ctx context.Context, arg UpdateUTXOLeaseParams) error UpsertAddrEvent(ctx context.Context, arg UpsertAddrEventParams) (int64, error) + UpsertAsset(ctx context.Context, arg UpsertAssetParams) (int64, error) UpsertAssetGroupKey(ctx context.Context, arg UpsertAssetGroupKeyParams) (int64, error) UpsertAssetGroupWitness(ctx context.Context, arg UpsertAssetGroupWitnessParams) (int64, error) UpsertAssetMeta(ctx context.Context, arg UpsertAssetMetaParams) (int64, error) diff --git a/tapdb/sqlc/queries/assets.sql b/tapdb/sqlc/queries/assets.sql index 749557eb2..cec023d24 100644 --- a/tapdb/sqlc/queries/assets.sql +++ b/tapdb/sqlc/queries/assets.sql @@ -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 (