Skip to content

Commit 2110839

Browse files
authored
Merge pull request #1089 from lightninglabs/fix-migration-14
tapdb: fix multiple issues around migration file handling
2 parents 9ba2a07 + 17ae210 commit 2110839

17 files changed

+204
-43
lines changed

.github/workflows/main.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ jobs:
8181
- name: Run test vector creation check
8282
run: make test-vector-check
8383

84-
migration-version-check:
84+
migration-check:
8585
name: migration version check
8686
runs-on: ubuntu-latest
8787
steps:
8888
- name: git checkout
8989
uses: actions/checkout@v3
9090

91-
- name: Run migration version check
92-
run: make migration-version-check
91+
- name: Run migration check
92+
run: make migration-check
9393

9494
########################
9595
# Compilation check.

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,10 @@ test-vector-check: gen-deterministic-test-vectors
331331
@$(call print, "Checking deterministic test vectors.")
332332
if test -n "$$(git status | grep -e ".json")"; then echo "Test vectors not updated"; git status; git diff; exit 1; fi
333333

334-
migration-version-check:
334+
migration-check:
335+
@$(call print, "Checking migration numbering.")
336+
./scripts/check-migration-numbering.sh
337+
335338
@$(call print, "Checking migration version.")
336339
./scripts/check-migration-latest-version.sh
337340

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
3+
# The directory containing migration files.
4+
migrations_path="tapdb/sqlc/migrations"
5+
6+
# Check if the directory exists
7+
if [ ! -d "$migrations_path" ]; then
8+
echo "Directory $migrations_path does not exist."
9+
exit 1
10+
fi
11+
12+
# Get all unique prefixes (e.g., 000001, always 6 digits) from .up.sql files.
13+
prefixes=($(ls "$migrations_path"/*.up.sql 2>/dev/null | \
14+
sed -E 's/.*\/([0-9]{6})_.*\.up\.sql/\1/' | sort))
15+
16+
# Check if no prefixes were found.
17+
if [ ${#prefixes[@]} -eq 0 ]; then
18+
echo "No .up.sql migration files found in $migrations_path."
19+
exit 1
20+
fi
21+
22+
# Iterate over prefixes to ensure that there are no gaps and that each prefix
23+
# has a corresponding .down.sql file. Because the prefixes are sorted, and the
24+
# index starts at 0, we expect each prefix to be the index plus one.
25+
for i in "${!prefixes[@]}"; do
26+
expected_prefix=$(printf "%06d" $((i+1)))
27+
28+
if [ "${prefixes[$i]}" != "$expected_prefix" ]; then
29+
echo "Error: Missing migration with prefix $expected_prefix."
30+
exit 1
31+
fi
32+
33+
# Find the corresponding .down.sql file.
34+
base_filename=$(ls "$migrations_path/${prefixes[$i]}"_*.up.sql | \
35+
sed -E 's/\.up\.sql//')
36+
37+
if [ ! -f "$base_filename.down.sql" ]; then
38+
echo "Error: Missing .down.sql file for migration $expected_prefix."
39+
exit 1
40+
fi
41+
done
42+
43+
echo "All migration files are present and properly numbered."

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/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

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,

0 commit comments

Comments
 (0)