Skip to content

Commit 17ae210

Browse files
committed
scripts+tapdb: do sqlc required replacement in script
Our SQL scripts on disk were neither SQLite nor Postgres compatible, we used a version suited for sqlc to generate the correct data types we wanted, then did some type replacements in-memory when actually applying the migration files. This is somewhat dangerous as it will lead to situations where if a user applies a migration file manually, they'll end up with table primary keys that ARE NOT auto incrementing. To fix this issue, we make sure that we always write fully SQLite compatible SQL code and only apply fixes for the sqlc code generation directly before generating the code.
1 parent cbb3aa6 commit 17ae210

12 files changed

+58
-40
lines changed

scripts/gen_sqlc_docker.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
88
GOCACHE=`go env GOCACHE`
99
GOMODCACHE=`go env GOMODCACHE`
1010

11+
# SQLite doesn't support "BIGINT PRIMARY KEY" for auto-incrementing primary
12+
# keys, only "INTEGER PRIMARY KEY". Internally it uses 64-bit integers for
13+
# numbers anyway, independent of the column type. So we can just use
14+
# "INTEGER PRIMARY KEY" and it will work the same under the hood, giving us
15+
# auto incrementing 64-bit integers.
16+
# _BUT_, sqlc will generate Go code with int32 if we use "INTEGER PRIMARY KEY",
17+
# even though we want int64. So before we run sqlc, we need to patch the
18+
# source schema SQL files to use "BIGINT PRIMARY KEY" instead of "INTEGER
19+
# PRIMARY KEY".
20+
echo "Applying SQLite bigint patch..."
21+
for file in tapdb/sqlc/migrations/*.up.sql; do
22+
echo "Patching $file"
23+
sed -i.bak -E 's/INTEGER PRIMARY KEY/BIGINT PRIMARY KEY/g' "$file"
24+
done
25+
26+
1127
echo "Generating sql models and queries in go..."
1228

1329
docker run \
@@ -17,3 +33,9 @@ docker run \
1733
-v "$DIR/../:/build" \
1834
-w /build \
1935
sqlc/sqlc:1.25.0 generate
36+
37+
# Restore the original schema files.
38+
echo "Restoring SQLite bigint patch..."
39+
for file in tapdb/sqlc/migrations/*.up.sql.bak; do
40+
mv "$file" "${file%.bak}"
41+
done

tapdb/postgres.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ var (
3636
// to work with sqlite primarily, and postgres has some differences.
3737
postgresSchemaReplacements = map[string]string{
3838
"BLOB": "BYTEA",
39-
"INTEGER PRIMARY KEY": "SERIAL PRIMARY KEY",
40-
"BIGINT PRIMARY KEY": "BIGSERIAL PRIMARY KEY",
39+
"INTEGER PRIMARY KEY": "BIGSERIAL PRIMARY KEY",
4140
"TIMESTAMP": "TIMESTAMP WITHOUT TIME ZONE",
4241
"UNHEX": "DECODE",
4342
}

tapdb/sqlc/migrations/000002_assets.up.sql

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
-- information, along with indexing information is stored.
44
-- TODO(roasbeef): also store SPV proof?
55
CREATE TABLE IF NOT EXISTS chain_txns (
6-
txn_id BIGINT PRIMARY KEY,
6+
txn_id INTEGER PRIMARY KEY,
77

88
txid BLOB UNIQUE NOT NULL,
99

@@ -23,7 +23,7 @@ CREATE TABLE IF NOT EXISTS chain_txns (
2323
-- outpoint itself, and also a references to the transaction that _spends_ that
2424
-- outpoint.
2525
CREATE TABLE IF NOT EXISTS genesis_points (
26-
genesis_id BIGINT PRIMARY KEY,
26+
genesis_id INTEGER PRIMARY KEY,
2727

2828
-- TODO(roasbeef): just need the input index here instead?
2929
prev_out BLOB UNIQUE NOT NULL,
@@ -35,7 +35,7 @@ CREATE TABLE IF NOT EXISTS genesis_points (
3535
-- assets that we either created, or bootstrapped from the relevant Base
3636
-- Universe.
3737
CREATE TABLE IF NOT EXISTS assets_meta (
38-
meta_id BIGINT PRIMARY KEY,
38+
meta_id INTEGER PRIMARY KEY,
3939

4040
meta_data_hash BLOB UNIQUE CHECK(length(meta_data_hash) = 32),
4141

@@ -50,7 +50,7 @@ CREATE TABLE IF NOT EXISTS assets_meta (
5050
-- reference the genesis point which is also a necessary component for
5151
-- computing an asset ID.
5252
CREATE TABLE IF NOT EXISTS genesis_assets (
53-
gen_asset_id BIGINT PRIMARY KEY,
53+
gen_asset_id INTEGER PRIMARY KEY,
5454

5555
asset_id BLOB UNIQUE,
5656

@@ -72,7 +72,7 @@ CREATE INDEX IF NOT EXISTS asset_ids on genesis_assets(asset_id);
7272
-- full KeyLocator is stored so we can use these keys without actually storing
7373
-- the private keys on disk.
7474
CREATE TABLE IF NOT EXISTS internal_keys (
75-
key_id BIGINT PRIMARY KEY,
75+
key_id INTEGER PRIMARY KEY,
7676

7777
-- We'll always store the full 33-byte key on disk, to make sure we're
7878
-- retaining full information.
@@ -88,7 +88,7 @@ CREATE TABLE IF NOT EXISTS internal_keys (
8888
-- tweaking the base group key by the associated genesis point. This table
8989
-- references the set of internal keys, and also the genesis_points table.
9090
CREATE TABLE IF NOT EXISTS asset_groups (
91-
group_id BIGINT PRIMARY KEY,
91+
group_id INTEGER PRIMARY KEY,
9292

9393
tweaked_group_key BLOB UNIQUE NOT NULL CHECK(length(tweaked_group_key) = 33),
9494

@@ -107,7 +107,7 @@ CREATE TABLE IF NOT EXISTS asset_groups (
107107
-- asset ID must also be included. This table reference the asset ID it's used
108108
-- to create as well as the group key that signed the asset in the first place.
109109
CREATE TABLE IF NOT EXISTS asset_group_witnesses (
110-
witness_id BIGINT PRIMARY KEY,
110+
witness_id INTEGER PRIMARY KEY,
111111

112112
-- The witness stack can contain either a single Schnorr signature for key
113113
-- spends of the tweaked group key, or a more complex script witness.
@@ -124,7 +124,7 @@ CREATE TABLE IF NOT EXISTS asset_group_witnesses (
124124
-- wallet, so the wallet is able to keep track of the amount of sats that are
125125
-- used to anchor Taproot assets.
126126
CREATE TABLE IF NOT EXISTS managed_utxos (
127-
utxo_id BIGINT PRIMARY KEY,
127+
utxo_id INTEGER PRIMARY KEY,
128128

129129
outpoint BLOB UNIQUE NOT NULL,
130130

@@ -163,7 +163,7 @@ CREATE TABLE IF NOT EXISTS managed_utxos (
163163
);
164164

165165
CREATE TABLE IF NOT EXISTS script_keys (
166-
script_key_id BIGINT PRIMARY KEY,
166+
script_key_id INTEGER PRIMARY KEY,
167167

168168
-- The actual internal key here that we hold the private key for. Applying
169169
-- the tweak to this gives us the tweaked_script_key.
@@ -184,7 +184,7 @@ CREATE TABLE IF NOT EXISTS script_keys (
184184
-- asset, along with the sibling taproot hash needed to properly reveal and
185185
-- spend the asset.
186186
CREATE TABLE IF NOT EXISTS assets (
187-
asset_id BIGINT PRIMARY KEY,
187+
asset_id INTEGER PRIMARY KEY,
188188

189189
genesis_id BIGINT NOT NULL REFERENCES genesis_assets(gen_asset_id),
190190

@@ -225,7 +225,7 @@ CREATE TABLE IF NOT EXISTS assets (
225225
-- asset. This then references the script key of an asset, creation a one to
226226
-- many relationship.
227227
CREATE TABLE IF NOT EXISTS asset_witnesses (
228-
witness_id BIGINT PRIMARY KEY,
228+
witness_id INTEGER PRIMARY KEY,
229229

230230
asset_id BIGINT NOT NULL REFERENCES assets(asset_id) ON DELETE CASCADE,
231231

@@ -243,7 +243,7 @@ CREATE TABLE IF NOT EXISTS asset_witnesses (
243243
);
244244

245245
CREATE TABLE IF NOT EXISTS asset_proofs (
246-
proof_id BIGINT PRIMARY KEY,
246+
proof_id INTEGER PRIMARY KEY,
247247

248248
-- We enforce that this value is unique so we can use an UPSERT to update a
249249
-- proof file that already exists.
@@ -260,7 +260,7 @@ CREATE TABLE IF NOT EXISTS asset_proofs (
260260
-- minting transaction which once signed and broadcast will actually create the
261261
-- assets.
262262
CREATE TABLE IF NOT EXISTS asset_minting_batches (
263-
batch_id BIGINT PRIMARY KEY REFERENCES internal_keys(key_id),
263+
batch_id INTEGER PRIMARY KEY REFERENCES internal_keys(key_id),
264264

265265
-- TODO(roasbeef): make into proper enum table or use check to ensure
266266
-- proper values
@@ -281,7 +281,7 @@ CREATE INDEX IF NOT EXISTS batch_state_lookup on asset_minting_batches (batch_st
281281
-- asset_seedlings are budding assets: the contain the base asset information
282282
-- need to create an asset, but doesn't yet have a genesis point.
283283
CREATE TABLE IF NOT EXISTS asset_seedlings (
284-
seedling_id BIGINT PRIMARY KEY,
284+
seedling_id INTEGER PRIMARY KEY,
285285

286286
-- TODO(roasbeef): data redundant w/ genesis_assets?
287287
-- move into asset details table?

tapdb/sqlc/migrations/000003_addrs.up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- a creation time and all the information needed to reconstruct the taproot
33
-- output on chain we'll use to send/recv to/from this address.
44
CREATE TABLE IF NOT EXISTS addrs (
5-
id BIGINT PRIMARY KEY,
5+
id INTEGER PRIMARY KEY,
66

77
-- version is the version of the Taproot Asset address format.
88
version SMALLINT NOT NULL,

tapdb/sqlc/migrations/000005_transfers.up.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CREATE TABLE IF NOT EXISTS asset_transfers (
2-
id BIGINT PRIMARY KEY,
2+
id INTEGER PRIMARY KEY,
33

44
height_hint INTEGER NOT NULL,
55

@@ -13,7 +13,7 @@ CREATE INDEX IF NOT EXISTS transfer_txn_idx
1313
ON asset_transfers (anchor_txn_id);
1414

1515
CREATE TABLE IF NOT EXISTS asset_transfer_inputs (
16-
input_id BIGINT PRIMARY KEY,
16+
input_id INTEGER PRIMARY KEY,
1717

1818
transfer_id BIGINT NOT NULL REFERENCES asset_transfers(id),
1919

@@ -29,7 +29,7 @@ CREATE INDEX IF NOT EXISTS transfer_inputs_idx
2929
ON asset_transfer_inputs (transfer_id);
3030

3131
CREATE TABLE IF NOT EXISTS asset_transfer_outputs (
32-
output_id BIGINT PRIMARY KEY,
32+
output_id INTEGER PRIMARY KEY,
3333

3434
transfer_id BIGINT NOT NULL REFERENCES asset_transfers(id),
3535

@@ -74,7 +74,7 @@ CREATE INDEX IF NOT EXISTS proof_locator_hash_index
7474
-- passive_assets is a table that stores the information needed to
7575
-- re-anchor a passive asset.
7676
CREATE TABLE IF NOT EXISTS passive_assets (
77-
passive_id BIGINT PRIMARY KEY,
77+
passive_id INTEGER PRIMARY KEY,
7878

7979
transfer_id BIGINT NOT NULL REFERENCES asset_transfers(id),
8080

tapdb/sqlc/migrations/000006_addr_event.up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- addr_events stores all events related to inbound (received) assets for
22
-- addresses.
33
CREATE TABLE IF NOT EXISTS addr_events (
4-
id BIGINT PRIMARY KEY,
4+
id INTEGER PRIMARY KEY,
55

66
-- creation_time is the creation time of this event.
77
creation_time TIMESTAMP NOT NULL,

tapdb/sqlc/migrations/000007_universe.up.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CREATE TABLE IF NOT EXISTS universe_roots (
2-
id BIGINT PRIMARY KEY,
2+
id INTEGER PRIMARY KEY,
33

44
-- For the namespace root, we set the foreign key constraint evaluation to
55
-- be deferred until after the database transaction ends. Otherwise, if the
@@ -23,7 +23,7 @@ CREATE INDEX IF NOT EXISTS universe_roots_asset_id_idx ON universe_roots(asset_i
2323
CREATE INDEX IF NOT EXISTS universe_roots_group_key_idx ON universe_roots(group_key);
2424

2525
CREATE TABLE IF NOT EXISTS universe_leaves (
26-
id BIGINT PRIMARY KEY,
26+
id INTEGER PRIMARY KEY,
2727

2828
asset_genesis_id BIGINT NOT NULL REFERENCES genesis_assets(gen_asset_id),
2929

@@ -44,7 +44,7 @@ CREATE INDEX IF NOT EXISTS universe_leaves_key_idx ON universe_leaves(leaf_node_
4444
CREATE INDEX IF NOT EXISTS universe_leaves_namespace ON universe_leaves(leaf_node_namespace);
4545

4646
CREATE TABLE IF NOT EXISTS universe_servers (
47-
id BIGINT PRIMARY KEY,
47+
id INTEGER PRIMARY KEY,
4848

4949
server_host TEXT UNIQUE NOT NULL,
5050

@@ -59,7 +59,7 @@ CREATE TABLE IF NOT EXISTS universe_servers (
5959
CREATE INDEX IF NOT EXISTS universe_servers_host ON universe_servers(server_host);
6060

6161
CREATE TABLE IF NOT EXISTS universe_events (
62-
event_id BIGINT PRIMARY KEY,
62+
event_id INTEGER PRIMARY KEY,
6363

6464
event_type VARCHAR NOT NULL CHECK (event_type IN ('SYNC', 'NEW_PROOF', 'NEW_ROOT')),
6565

tapdb/sqlc/migrations/000013_universe_fed_proof_sync_log.up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- This table stores the log of federation universe proof sync attempts. Rows
22
-- in this table are specific to a given proof leaf, server, and sync direction.
33
CREATE TABLE IF NOT EXISTS federation_proof_sync_log (
4-
id BIGINT PRIMARY KEY,
4+
id INTEGER PRIMARY KEY,
55

66
-- The status of the proof sync attempt.
77
status TEXT NOT NULL CHECK(status IN ('pending', 'complete')),

tapdb/sqlc/migrations/000014_multiverse_tree.up.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CREATE TABLE IF NOT EXISTS multiverse_roots (
2-
id BIGINT PRIMARY KEY,
2+
id INTEGER PRIMARY KEY,
33

44
-- For the namespace root, we set the foreign key constraint evaluation to
55
-- be deferred until after the database transaction ends. Otherwise, if the
@@ -14,7 +14,7 @@ CREATE TABLE IF NOT EXISTS multiverse_roots (
1414
);
1515

1616
CREATE TABLE IF NOT EXISTS multiverse_leaves (
17-
id BIGINT PRIMARY KEY,
17+
id INTEGER PRIMARY KEY,
1818

1919
multiverse_root_id BIGINT NOT NULL REFERENCES multiverse_roots(id),
2020

tapdb/sqlc/migrations/000016_tapscript_trees.up.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ALTER TABLE asset_minting_batches ADD COLUMN tapscript_sibling BLOB;
66
-- This table stores root hashes for tapscript trees, and a flag to ensure that
77
-- the stored tree nodes are decoded correctly.
88
CREATE TABLE IF NOT EXISTS tapscript_roots (
9-
root_id BIGINT PRIMARY KEY,
9+
root_id INTEGER PRIMARY KEY,
1010

1111
-- The root hash of a tapscript tree.
1212
root_hash BLOB NOT NULL UNIQUE CHECK(length(root_hash) = 32),
@@ -19,7 +19,7 @@ CREATE TABLE IF NOT EXISTS tapscript_roots (
1919
-- This table stores tapscript nodes, which are tapHashes or tapLeafs. A node
2020
-- may be included in multiple tapscript trees.
2121
CREATE TABLE IF NOT EXISTS tapscript_nodes (
22-
node_id BIGINT PRIMARY KEY,
22+
node_id INTEGER PRIMARY KEY,
2323

2424
-- The serialized tapscript node, which may be a tapHash or tapLeaf.
2525
raw_node BLOB NOT NULL UNIQUE
@@ -28,7 +28,7 @@ CREATE TABLE IF NOT EXISTS tapscript_nodes (
2828
-- This table stores tapscript edges, which link a serialized tapscript node
2929
-- to a tapscript tree root hash and preserve the node ordering in the tree.
3030
CREATE TABLE IF NOT EXISTS tapscript_edges (
31-
edge_id BIGINT PRIMARY KEY,
31+
edge_id INTEGER PRIMARY KEY,
3232

3333
-- The root hash of a tree that includes the referenced tapscript node.
3434
root_hash_id BIGINT NOT NULL REFERENCES tapscript_roots(root_id),

0 commit comments

Comments
 (0)