Skip to content

Commit 8d6f205

Browse files
committed
cmd/merge-sql-schemas: include named indexes in generated schema
In this commit, we fix a bug in the existing final schema generation script to ensure it includes named indexes. From the sqlite docs: > The sqlite_schema.type column will be one of the following text strings: 'table', 'index', 'view', or 'trigger' according to the type of object defined. The 'table' string is used for both ordinary and virtual tables.
1 parent 581c881 commit 8d6f205

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

cmd/merge-sql-schemas/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,14 @@ func main() {
6565
//
6666
// This way, we can consolidate and export the complete database schema
6767
// as it stands after all migrations have been applied.
68+
//
69+
// We filter our where sql is NOT NULL, as for the internal sqlite
70+
// creates, the sql column will be NULL.
6871
// ---------------------------------------------------------------------
6972
rows, err := db.Query(`
7073
SELECT type, name, sql FROM sqlite_master
71-
WHERE type IN ('table','view') ORDER BY name`,
74+
WHERE type IN ('table','view', 'index') AND sql IS NOT NULL
75+
ORDER BY name`,
7276
)
7377
if err != nil {
7478
log.Fatalf("failed to query schema: %v", err)

tapdb/sqlc/schemas/generated_schema.sql

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
CREATE INDEX addr_asset_genesis_ids ON addrs (genesis_asset_id);
2+
3+
CREATE INDEX addr_creation_time ON addrs (creation_time);
4+
15
CREATE TABLE addr_events (
26
id INTEGER PRIMARY KEY,
37

@@ -35,6 +39,10 @@ CREATE TABLE addr_events (
3539
UNIQUE(addr_id, chain_txn_id, chain_txn_output_index)
3640
);
3741

42+
CREATE INDEX addr_group_keys ON addrs (group_key);
43+
44+
CREATE INDEX addr_managed_from ON addrs (managed_from);
45+
3846
CREATE TABLE addrs (
3947
id INTEGER PRIMARY KEY,
4048

@@ -136,6 +144,10 @@ CREATE TABLE asset_groups (
136144
, version INTEGER NOT NULL DEFAULT 0, custom_subtree_root_id INTEGER
137145
REFERENCES tapscript_roots(root_id));
138146

147+
CREATE INDEX asset_id_idx ON addr_events(asset_id);
148+
149+
CREATE INDEX asset_ids on genesis_assets(asset_id);
150+
139151
CREATE TABLE asset_minting_batches (
140152
batch_id INTEGER PRIMARY KEY REFERENCES internal_keys(key_id),
141153

@@ -154,6 +166,8 @@ CREATE TABLE asset_minting_batches (
154166
creation_time_unix TIMESTAMP NOT NULL
155167
, tapscript_sibling BLOB, assets_output_index INTEGER, universe_commitments BOOLEAN NOT NULL DEFAULT FALSE);
156168

169+
CREATE INDEX asset_proof_id_idx ON addr_events(asset_proof_id);
170+
157171
CREATE TABLE asset_proofs (
158172
proof_id INTEGER PRIMARY KEY,
159173

@@ -238,6 +252,11 @@ CREATE TABLE asset_transfer_outputs (
238252
proof_courier_addr BLOB
239253
, lock_time INTEGER, relative_lock_time INTEGER, proof_delivery_complete BOOL, position INTEGER NOT NULL DEFAULT -1);
240254

255+
CREATE UNIQUE INDEX asset_transfer_outputs_transfer_id_position_unique
256+
ON asset_transfer_outputs (
257+
transfer_id, position
258+
);
259+
241260
CREATE TABLE asset_transfers (
242261
id INTEGER PRIMARY KEY,
243262

@@ -266,6 +285,11 @@ CREATE TABLE asset_witnesses (
266285
split_commitment_proof BLOB
267286
, witness_index INTEGER NOT NULL DEFAULT -1);
268287

288+
CREATE UNIQUE INDEX asset_witnesses_asset_id_witness_index_unique
289+
ON asset_witnesses (
290+
asset_id, witness_index
291+
);
292+
269293
CREATE TABLE assets (
270294
asset_id INTEGER PRIMARY KEY,
271295

@@ -304,6 +328,11 @@ CREATE TABLE assets (
304328
UNIQUE(asset_id, genesis_id, script_key_id)
305329
);
306330

331+
CREATE UNIQUE INDEX assets_genesis_id_script_key_id_anchor_utxo_id_unique
332+
ON assets (
333+
genesis_id, script_key_id, anchor_utxo_id
334+
);
335+
307336
CREATE TABLE assets_meta (
308337
meta_id INTEGER PRIMARY KEY,
309338

@@ -317,6 +346,8 @@ CREATE TABLE assets_meta (
317346
CHECK(LENGTH(meta_canonical_universes) <= 4096), meta_delegation_key BLOB
318347
CHECK(LENGTH(meta_delegation_key) <= 33));
319348

349+
CREATE INDEX batch_state_lookup on asset_minting_batches (batch_state);
350+
320351
CREATE TABLE chain_txns (
321352
txn_id INTEGER PRIMARY KEY,
322353

@@ -333,6 +364,8 @@ CREATE TABLE chain_txns (
333364
tx_index INTEGER
334365
);
335366

367+
CREATE INDEX creation_time_idx ON addr_events(creation_time);
368+
336369
CREATE TABLE federation_global_sync_config (
337370
proof_type TEXT NOT NULL PRIMARY KEY REFERENCES proof_types(proof_type),
338371
allow_sync_insert BOOLEAN NOT NULL,
@@ -365,6 +398,14 @@ CREATE TABLE federation_proof_sync_log (
365398
servers_id BIGINT NOT NULL REFERENCES universe_servers(id)
366399
);
367400

401+
CREATE UNIQUE INDEX federation_proof_sync_log_unique_index_proof_leaf_id_servers_id
402+
ON federation_proof_sync_log (
403+
sync_direction,
404+
proof_leaf_id,
405+
universe_root_id,
406+
servers_id
407+
);
408+
368409
CREATE TABLE federation_uni_sync_config (
369410
-- namespace is the string representation of the universe identifier, and
370411
-- ensures that there are no duplicate configs.
@@ -435,6 +476,14 @@ CREATE TABLE genesis_points (
435476
anchor_tx_id BIGINT REFERENCES chain_txns(txn_id)
436477
);
437478

479+
CREATE INDEX idx_mssmt_nodes_composite
480+
ON mssmt_nodes(namespace, key, hash_key, sum);
481+
482+
CREATE INDEX idx_universe_leaves_asset
483+
ON universe_leaves(asset_genesis_id, universe_root_id);
484+
485+
CREATE INDEX idx_universe_roots_composite ON universe_roots(namespace_root, proof_type, asset_id);
486+
438487
CREATE TABLE internal_keys (
439488
key_id INTEGER PRIMARY KEY,
440489

@@ -526,6 +575,9 @@ CREATE TABLE mint_anchor_uni_commitments (
526575
group_key BLOB
527576
);
528577

578+
CREATE UNIQUE INDEX mint_anchor_uni_commitments_unique
579+
ON mint_anchor_uni_commitments (batch_id, tx_output_index);
580+
529581
CREATE TABLE mssmt_nodes (
530582
-- hash_key is the hash key by which we reference all nodes.
531583
hash_key BLOB NOT NULL,
@@ -557,6 +609,10 @@ CREATE TABLE mssmt_nodes (
557609
PRIMARY KEY (hash_key, namespace)
558610
);
559611

612+
CREATE INDEX mssmt_nodes_l_hash_key_idx ON mssmt_nodes (l_hash_key);
613+
614+
CREATE INDEX mssmt_nodes_r_hash_key_idx ON mssmt_nodes (r_hash_key);
615+
560616
CREATE TABLE mssmt_roots (
561617
-- namespace allows us to store several root hash pointers for distinct
562618
-- trees.
@@ -590,6 +646,10 @@ CREATE TABLE multiverse_leaves (
590646
)
591647
);
592648

649+
CREATE UNIQUE INDEX multiverse_leaves_unique ON multiverse_leaves (
650+
leaf_node_key, leaf_node_namespace
651+
);
652+
593653
CREATE TABLE multiverse_roots (
594654
id INTEGER PRIMARY KEY,
595655

@@ -623,6 +683,12 @@ CREATE TABLE passive_assets (
623683
new_proof BLOB
624684
);
625685

686+
CREATE INDEX passive_assets_idx
687+
ON passive_assets (transfer_id);
688+
689+
CREATE INDEX proof_locator_hash_index
690+
ON proof_transfer_log (proof_locator_hash);
691+
626692
CREATE TABLE proof_transfer_log (
627693
-- The type of proof transfer attempt. The transfer is either a proof
628694
-- delivery to the transfer counterparty or receiving a proof from the
@@ -655,6 +721,8 @@ CREATE TABLE script_keys (
655721
tweak BLOB
656722
, declared_known BOOLEAN);
657723

724+
CREATE INDEX status_idx ON addr_events(status);
725+
658726
CREATE TABLE tapscript_edges (
659727
edge_id INTEGER PRIMARY KEY,
660728

@@ -669,6 +737,10 @@ CREATE TABLE tapscript_edges (
669737
raw_node_id BIGINT NOT NULL REFERENCES tapscript_nodes(node_id)
670738
);
671739

740+
CREATE UNIQUE INDEX tapscript_edges_unique ON tapscript_edges (
741+
root_hash_id, node_index, raw_node_id
742+
);
743+
672744
CREATE TABLE tapscript_nodes (
673745
node_id INTEGER PRIMARY KEY,
674746

@@ -687,6 +759,18 @@ CREATE TABLE tapscript_roots (
687759
branch_only BOOLEAN NOT NULL DEFAULT FALSE
688760
);
689761

762+
CREATE INDEX transfer_inputs_idx
763+
ON asset_transfer_inputs (transfer_id);
764+
765+
CREATE INDEX transfer_outputs_idx
766+
ON asset_transfer_outputs (transfer_id);
767+
768+
CREATE INDEX transfer_time_idx
769+
ON asset_transfers (transfer_time_unix);
770+
771+
CREATE INDEX transfer_txn_idx
772+
ON asset_transfers (anchor_txn_id);
773+
690774
CREATE TABLE universe_events (
691775
event_id INTEGER PRIMARY KEY,
692776

@@ -699,6 +783,10 @@ CREATE TABLE universe_events (
699783
event_time TIMESTAMP NOT NULL
700784
, event_timestamp BIGINT NOT NULL DEFAULT 0);
701785

786+
CREATE INDEX universe_events_event_time_idx ON universe_events(event_time);
787+
788+
CREATE INDEX universe_events_type_idx ON universe_events(event_type);
789+
702790
CREATE TABLE universe_leaves (
703791
id INTEGER PRIMARY KEY,
704792

@@ -717,6 +805,10 @@ CREATE TABLE universe_leaves (
717805
UNIQUE(minting_point, script_key_bytes)
718806
);
719807

808+
CREATE INDEX universe_leaves_key_idx ON universe_leaves(leaf_node_key);
809+
810+
CREATE INDEX universe_leaves_namespace ON universe_leaves(leaf_node_namespace);
811+
720812
CREATE TABLE universe_roots (
721813
id INTEGER PRIMARY KEY,
722814

@@ -737,6 +829,10 @@ CREATE TABLE universe_roots (
737829
-- universe.
738830
proof_type TEXT REFERENCES proof_types(proof_type));
739831

832+
CREATE INDEX universe_roots_asset_id_idx ON universe_roots(asset_id);
833+
834+
CREATE INDEX universe_roots_group_key_idx ON universe_roots(group_key);
835+
740836
CREATE TABLE universe_servers (
741837
id INTEGER PRIMARY KEY,
742838

@@ -750,6 +846,8 @@ CREATE TABLE universe_servers (
750846
-- etc? also sync mode, ones that should get everything pushed, etc
751847
);
752848

849+
CREATE INDEX universe_servers_host ON universe_servers(server_host);
850+
753851
CREATE VIEW universe_stats AS
754852
WITH sync_counts AS (
755853
SELECT universe_root_id, COUNT(*) AS count

0 commit comments

Comments
 (0)