Skip to content

Commit a6415f8

Browse files
authored
Merge pull request #784 from guggero/fix-asset-witness
tapdb: fix migration 15
2 parents 99b8f86 + 6774f0e commit a6415f8

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

tapdb/migrations_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"testing"
66

7+
"github.com/btcsuite/btcd/wire"
78
"github.com/stretchr/testify/require"
89
)
910

@@ -51,3 +52,34 @@ func TestMigrationSteps(t *testing.T) {
5152
// the steps that can be taken to test migrations, so we are done for
5253
// this test.
5354
}
55+
56+
// TestMigration15 tests that the migration to version 15 works as expected.
57+
func TestMigration15(t *testing.T) {
58+
ctx := context.Background()
59+
60+
db := NewTestDBWithVersion(t, 14)
61+
62+
// We need to insert some test data that will be affected by the
63+
// migration number 15.
64+
InsertTestdata(t, db.BaseDB, "migrations_test_00015_dummy_data.sql")
65+
66+
// And now that we have test data inserted, we can migrate to the latest
67+
// version.
68+
err := db.ExecuteMigrations(TargetLatest)
69+
require.NoError(t, err)
70+
71+
// Make sure the single asset that was inserted actually has two
72+
// witnesses with the correct order.
73+
_, assetStore := newAssetStoreFromDB(db.BaseDB)
74+
assets, err := assetStore.FetchAllAssets(ctx, false, false, nil)
75+
require.NoError(t, err)
76+
77+
require.Len(t, assets, 1)
78+
require.Len(t, assets[0].PrevWitnesses, 2)
79+
require.Equal(
80+
t, wire.TxWitness{{0xaa}}, assets[0].PrevWitnesses[0].TxWitness,
81+
)
82+
require.Equal(
83+
t, wire.TxWitness{{0xbb}}, assets[0].PrevWitnesses[1].TxWitness,
84+
)
85+
}

tapdb/sqlc/migrations/000015_asset_witnesses.up.sql

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
-- The witness index indicates the order of the witness in the list of witnesses
2-
-- for a given asset. We didn't really support more than one witness before, so
3-
-- the default value of 0 should be fine for all existing assets.
4-
ALTER TABLE asset_witnesses ADD COLUMN witness_index INTEGER NOT NULL DEFAULT 0;
2+
-- for a given asset. We'll be inserting an actual value in the next query, so
3+
-- we just start with -1.
4+
ALTER TABLE asset_witnesses ADD COLUMN witness_index INTEGER NOT NULL DEFAULT -1;
5+
6+
-- Update the witness index to be the same as the witness id. We'll use the
7+
-- witness_index for sorting only, so setting the default to the witness_id is
8+
-- just to make sure we preserve the current order of witnesses while also
9+
-- satisfying the unique constraint we add below.
10+
UPDATE asset_witnesses SET witness_index = CAST(witness_id AS INTEGER)
11+
WHERE witness_index = -1;
512

613
-- We need to be able to upsert witnesses, so we need a unique constraint on
714
-- (asset_id, witness_index).
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- This dummy data inserts a single asset that has two witness entries so we can
2+
-- test the migration 15 that adds the witness_index column to the
3+
-- asset_witnesses table.
4+
INSERT INTO chain_txns VALUES(1,X'a1594fc379308b2a209f6d0bdb8602e9f87cf71fc232c69032b9a5fed28f9331',1980,X'02000000000101022cd51ca4d850c5f71ceedf7c50a08ff82d66612b22f631eac95e6b52cbbd2d0000000000ffffffff02e80300000000000022512018ac5a65a0d12e7846c89d24705e2697b1da14627978ba8db24bdbce21fc2aa85cd5f5050000000022512030263d67b4275144b2b00921d220a1311b9a4465fa656ba7d5754b421cb4308402483045022100fa32af97cab8a765dc347c3ff57b14f9810b6dbfc4d02727fb099d1ed875660602204cb66f3bbd92925707158b4aa67338c50a9ffddceb023875eb82b78b3967e007012102eb9cd2a22fd11c40823cb7b0f0fba4156138af69cf73c0644be54f4d46ba480700000000',441,X'4295613d85ccbc455159eb4ddd1e266ca10041d3c75726286b7dfeb3132c9c4f',1);
5+
6+
INSERT INTO genesis_points VALUES(1,X'022cd51ca4d850c5f71ceedf7c50a08ff82d66612b22f631eac95e6b52cbbd2d00000000',1);
7+
8+
INSERT INTO assets_meta VALUES(1,X'2b990b7adb1faf51ccb9b1c73bc5e73926db39cdec8906d4fd3c6c423a3c9821',X'736f6d65206d65746164617461',0);
9+
10+
INSERT INTO genesis_assets VALUES(1,X'add7d0d7cc37e58a7c0d8ad40b6904050d2baa25a1829f00689c4b27b524dd04','itestbuxx-collectible',1,0,1,1);
11+
12+
INSERT INTO internal_keys VALUES(1,X'02827d74858d152da1fae12010ad8d3c46b595c2d4480512a6575925424617124f',212,0);
13+
INSERT INTO internal_keys VALUES(2,X'03efbcf2878876bae81ca9a7f6476764d2da38d565b9fb2b691e7bb22fd99f9e5e',212,2);
14+
15+
INSERT INTO managed_utxos VALUES(1,X'a1594fc379308b2a209f6d0bdb8602e9f87cf71fc232c69032b9a5fed28f933100000000',1000,1,X'1dd3e2cf0bbbee32832c4deb57bbae58779fa599be0b8eb1f61e8c624157e2fa',NULL,X'1dd3e2cf0bbbee32832c4deb57bbae58779fa599be0b8eb1f61e8c624157e2fa',1,NULL,NULL);
16+
17+
INSERT INTO script_keys VALUES(1,2,X'029c571fffcac1a1a7cd3372bd202ad8562f28e48b90f8a4eb714eca062f576ee6',NULL);
18+
19+
INSERT INTO assets VALUES(1,1,1,1,NULL,0,1,0,0,NULL,NULL,1,false);
20+
21+
INSERT INTO asset_witnesses VALUES(1,1,X'a1594fc379308b2a209f6d0bdb8602e9f87cf71fc232c69032b9a5fed28f933100000000',X'add7d0d7cc37e58a7c0d8ad40b6904050d2baa25a1829f00689c4b27b524dd04',X'02827d74858d152da1fae12010ad8d3c46b595c2d4480512a6575925424617124f',X'0101aa',NULL);
22+
INSERT INTO asset_witnesses VALUES(2,1,X'a1594fc379308b2a209f6d0bdb8602e9f87cf71fc232c69032b9a5fed28f933101000000',X'add7d0d7cc37e58a7c0d8ad40b6904050d2baa25a1829f00689c4b27b524dd04',X'02827d74858d152da1fae12010ad8d3c46b595c2d4480512a6575925424617124f',X'0101bb',NULL);

0 commit comments

Comments
 (0)