Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tapdb: fix migration 15 #784

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions tapdb/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/btcsuite/btcd/wire"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -51,3 +52,34 @@ func TestMigrationSteps(t *testing.T) {
// the steps that can be taken to test migrations, so we are done for
// this test.
}

// TestMigration15 tests that the migration to version 15 works as expected.
func TestMigration15(t *testing.T) {
ctx := context.Background()

db := NewTestDBWithVersion(t, 14)

// We need to insert some test data that will be affected by the
// migration number 15.
InsertTestdata(t, db.BaseDB, "migrations_test_00015_dummy_data.sql")

// And now that we have test data inserted, we can migrate to the latest
// version.
err := db.ExecuteMigrations(TargetLatest)
require.NoError(t, err)

// Make sure the single asset that was inserted actually has two
// witnesses with the correct order.
_, assetStore := newAssetStoreFromDB(db.BaseDB)
assets, err := assetStore.FetchAllAssets(ctx, false, false, nil)
require.NoError(t, err)

require.Len(t, assets, 1)
require.Len(t, assets[0].PrevWitnesses, 2)
require.Equal(
t, wire.TxWitness{{0xaa}}, assets[0].PrevWitnesses[0].TxWitness,
)
require.Equal(
t, wire.TxWitness{{0xbb}}, assets[0].PrevWitnesses[1].TxWitness,
)
}
13 changes: 10 additions & 3 deletions tapdb/sqlc/migrations/000015_asset_witnesses.up.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
-- The witness index indicates the order of the witness in the list of witnesses
-- for a given asset. We didn't really support more than one witness before, so
-- the default value of 0 should be fine for all existing assets.
ALTER TABLE asset_witnesses ADD COLUMN witness_index INTEGER NOT NULL DEFAULT 0;
-- for a given asset. We'll be inserting an actual value in the next query, so
-- we just start with -1.
ALTER TABLE asset_witnesses ADD COLUMN witness_index INTEGER NOT NULL DEFAULT -1;

-- Update the witness index to be the same as the witness id. We'll use the
-- witness_index for sorting only, so setting the default to the witness_id is
-- just to make sure we preserve the current order of witnesses while also
-- satisfying the unique constraint we add below.
UPDATE asset_witnesses SET witness_index = CAST(witness_id AS INTEGER)
WHERE witness_index = -1;

-- We need to be able to upsert witnesses, so we need a unique constraint on
-- (asset_id, witness_index).
Expand Down
22 changes: 22 additions & 0 deletions tapdb/testdata/migrations_test_00015_dummy_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- This dummy data inserts a single asset that has two witness entries so we can
-- test the migration 15 that adds the witness_index column to the
-- asset_witnesses table.
INSERT INTO chain_txns VALUES(1,X'a1594fc379308b2a209f6d0bdb8602e9f87cf71fc232c69032b9a5fed28f9331',1980,X'02000000000101022cd51ca4d850c5f71ceedf7c50a08ff82d66612b22f631eac95e6b52cbbd2d0000000000ffffffff02e80300000000000022512018ac5a65a0d12e7846c89d24705e2697b1da14627978ba8db24bdbce21fc2aa85cd5f5050000000022512030263d67b4275144b2b00921d220a1311b9a4465fa656ba7d5754b421cb4308402483045022100fa32af97cab8a765dc347c3ff57b14f9810b6dbfc4d02727fb099d1ed875660602204cb66f3bbd92925707158b4aa67338c50a9ffddceb023875eb82b78b3967e007012102eb9cd2a22fd11c40823cb7b0f0fba4156138af69cf73c0644be54f4d46ba480700000000',441,X'4295613d85ccbc455159eb4ddd1e266ca10041d3c75726286b7dfeb3132c9c4f',1);

INSERT INTO genesis_points VALUES(1,X'022cd51ca4d850c5f71ceedf7c50a08ff82d66612b22f631eac95e6b52cbbd2d00000000',1);

INSERT INTO assets_meta VALUES(1,X'2b990b7adb1faf51ccb9b1c73bc5e73926db39cdec8906d4fd3c6c423a3c9821',X'736f6d65206d65746164617461',0);

INSERT INTO genesis_assets VALUES(1,X'add7d0d7cc37e58a7c0d8ad40b6904050d2baa25a1829f00689c4b27b524dd04','itestbuxx-collectible',1,0,1,1);

INSERT INTO internal_keys VALUES(1,X'02827d74858d152da1fae12010ad8d3c46b595c2d4480512a6575925424617124f',212,0);
INSERT INTO internal_keys VALUES(2,X'03efbcf2878876bae81ca9a7f6476764d2da38d565b9fb2b691e7bb22fd99f9e5e',212,2);

INSERT INTO managed_utxos VALUES(1,X'a1594fc379308b2a209f6d0bdb8602e9f87cf71fc232c69032b9a5fed28f933100000000',1000,1,X'1dd3e2cf0bbbee32832c4deb57bbae58779fa599be0b8eb1f61e8c624157e2fa',NULL,X'1dd3e2cf0bbbee32832c4deb57bbae58779fa599be0b8eb1f61e8c624157e2fa',1,NULL,NULL);

INSERT INTO script_keys VALUES(1,2,X'029c571fffcac1a1a7cd3372bd202ad8562f28e48b90f8a4eb714eca062f576ee6',NULL);

INSERT INTO assets VALUES(1,1,1,1,NULL,0,1,0,0,NULL,NULL,1,false);

INSERT INTO asset_witnesses VALUES(1,1,X'a1594fc379308b2a209f6d0bdb8602e9f87cf71fc232c69032b9a5fed28f933100000000',X'add7d0d7cc37e58a7c0d8ad40b6904050d2baa25a1829f00689c4b27b524dd04',X'02827d74858d152da1fae12010ad8d3c46b595c2d4480512a6575925424617124f',X'0101aa',NULL);
INSERT INTO asset_witnesses VALUES(2,1,X'a1594fc379308b2a209f6d0bdb8602e9f87cf71fc232c69032b9a5fed28f933101000000',X'add7d0d7cc37e58a7c0d8ad40b6904050d2baa25a1829f00689c4b27b524dd04',X'02827d74858d152da1fae12010ad8d3c46b595c2d4480512a6575925424617124f',X'0101bb',NULL);
Loading