Skip to content

Commit cbb3aa6

Browse files
committed
migrations: re-apply migration 14 as 23.
Due to a git branch cherry-pick mishap, the migration number 14 wasn't included in a released version and therefore wasn't applied on some systems. Because the tables in that migration file were only being used with a recently released version (v0.4.0/v0.4.1), this wasn't noticed for a long time. We fix this by re-applying the same migration as number 23, slightly modified so that it's a no-op if the tables and entries already exist on a system.
1 parent b09d4e3 commit cbb3aa6

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

tapdb/migrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const (
2222
// daemon.
2323
//
2424
// NOTE: This MUST be updated when a new migration is added.
25-
LatestMigrationVersion = 22
25+
LatestMigrationVersion = 23
2626
)
2727

2828
// MigrationTarget is a functional option that can be passed to applyMigrations
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
DROP INDEX IF EXISTS multiverse_leaves_unique;
2+
DROP TABLE IF EXISTS multiverse_leaves;
3+
DROP TABLE IF EXISTS multiverse_roots;
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
-- This is a slightly modified duplicate of migration 14, which due to a git
2+
-- branch cherry-pick mistake wasn't included in a released version.
3+
-- If this is running on a version that already has the tables, then this is a
4+
-- no-op.
5+
6+
CREATE TABLE IF NOT EXISTS multiverse_roots (
7+
id BIGINT PRIMARY KEY,
8+
9+
-- For the namespace root, we set the foreign key constraint evaluation to
10+
-- be deferred until after the database transaction ends. Otherwise, if the
11+
-- root of the SMT is deleted temporarily before inserting a new root, then
12+
-- this constraint is violated as there's no longer a root that this
13+
-- universe tree can point to.
14+
namespace_root VARCHAR UNIQUE NOT NULL REFERENCES mssmt_roots(namespace) DEFERRABLE INITIALLY DEFERRED,
15+
16+
-- This field is an enum representing the proof type stored in the given
17+
-- universe.
18+
proof_type TEXT NOT NULL CHECK(proof_type IN ('issuance', 'transfer'))
19+
);
20+
21+
CREATE TABLE IF NOT EXISTS multiverse_leaves (
22+
id BIGINT PRIMARY KEY,
23+
24+
multiverse_root_id BIGINT NOT NULL REFERENCES multiverse_roots(id),
25+
26+
asset_id BLOB CHECK(length(asset_id) = 32),
27+
28+
-- We use the 32 byte schnorr key here as this is what's used to derive the
29+
-- top-level Taproot Asset commitment key.
30+
group_key BLOB CHECK(LENGTH(group_key) = 32),
31+
32+
leaf_node_key BLOB NOT NULL,
33+
34+
leaf_node_namespace VARCHAR NOT NULL,
35+
36+
-- Both the asset ID and group key cannot be null at the same time.
37+
CHECK (
38+
(asset_id IS NOT NULL AND group_key IS NULL) OR
39+
(asset_id IS NULL AND group_key IS NOT NULL)
40+
)
41+
);
42+
43+
CREATE UNIQUE INDEX IF NOT EXISTS multiverse_leaves_unique ON multiverse_leaves (
44+
leaf_node_key, leaf_node_namespace
45+
);
46+
47+
-- If there already is a multiverse root entry in the mssmt_roots for the
48+
-- issuance or transfer multiverses, add them to the multiverse_roots table as
49+
-- well. Both statements are no-ops if the root doesn't exist yet.
50+
INSERT INTO multiverse_roots (namespace_root, proof_type)
51+
SELECT 'multiverse-issuance', 'issuance'
52+
WHERE EXISTS (
53+
SELECT 1 FROM mssmt_roots WHERE namespace = 'multiverse-issuance'
54+
) ON CONFLICT DO NOTHING;
55+
56+
INSERT INTO multiverse_roots (namespace_root, proof_type)
57+
SELECT 'multiverse-transfer', 'transfer'
58+
WHERE EXISTS (
59+
SELECT 1 FROM mssmt_roots WHERE namespace = 'multiverse-transfer'
60+
) ON CONFLICT DO NOTHING;
61+
62+
-- And now we create the multiverse_leaves entries for the multiverse roots.
63+
-- This is a no-op if the multiverse root doesn't exist yet.
64+
INSERT INTO multiverse_leaves (
65+
multiverse_root_id, asset_id, group_key, leaf_node_key, leaf_node_namespace
66+
) SELECT
67+
(SELECT id from multiverse_roots mr where mr.namespace_root = 'multiverse-issuance'),
68+
CASE WHEN ur.group_key IS NULL THEN ur.asset_id ELSE NULL END,
69+
ur.group_key,
70+
-- UNHEX() only exists in SQLite and it doesn't take a second argument
71+
-- (the 'hex' part). But it also doesn't complain about it, so we can
72+
-- leave it in for the Postgres version which is replaced in-memory to
73+
-- DECODE() which needs the 'hex' argument.
74+
UNHEX(REPLACE(ur.namespace_root, 'issuance-', ''), 'hex'),
75+
ur.namespace_root
76+
FROM universe_roots ur
77+
WHERE ur.namespace_root LIKE 'issuance-%'
78+
ON CONFLICT DO NOTHING;
79+
80+
INSERT INTO multiverse_leaves (
81+
multiverse_root_id, asset_id, group_key, leaf_node_key, leaf_node_namespace
82+
) SELECT
83+
(SELECT id from multiverse_roots mr where mr.namespace_root = 'multiverse-transfer'),
84+
CASE WHEN ur.group_key IS NULL THEN ur.asset_id ELSE NULL END,
85+
ur.group_key,
86+
-- UNHEX() only exists in SQLite and it doesn't take a second argument
87+
-- (the 'hex' part). But it also doesn't complain about it, so we can
88+
-- leave it in for the Postgres version which is replaced in-memory to
89+
-- DECODE() which needs the 'hex' argument.
90+
UNHEX(REPLACE(ur.namespace_root, 'transfer-', ''), 'hex'),
91+
ur.namespace_root
92+
FROM universe_roots ur
93+
WHERE ur.namespace_root LIKE 'transfer-%'
94+
ON CONFLICT DO NOTHING;

0 commit comments

Comments
 (0)