Skip to content

Commit 84598b6

Browse files
committed
sqldb: ensure schema definitions are fully SQLite compatible
Previously, we applied replacements to our schema definitions to make them compatible with both SQLite and Postgres backends, as the files were not fully compatible with either. With this change, the only replacement required for SQLite has been moved to the generator script. This adjustment ensures compatibility by enabling auto-incrementing primary keys that are treated as 64-bit integers by sqlc.
1 parent ea98933 commit 84598b6

File tree

8 files changed

+44
-20
lines changed

8 files changed

+44
-20
lines changed

invoices/sql_migration.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func createInvoiceHashIndex(ctx context.Context, db kvdb.Backend,
100100

101101
return tx.InsertKVInvoiceKeyAndAddIndex(ctx,
102102
sqlc.InsertKVInvoiceKeyAndAddIndexParams{
103-
ID: int32(invoiceKey),
103+
ID: int64(invoiceKey),
104104
AddIndex: int64(addIndexNo),
105105
},
106106
)
@@ -132,7 +132,7 @@ func createInvoiceHashIndex(ctx context.Context, db kvdb.Backend,
132132

133133
return tx.SetKVInvoicePaymentHash(ctx,
134134
sqlc.SetKVInvoicePaymentHashParams{
135-
ID: int32(invoiceKey),
135+
ID: int64(invoiceKey),
136136
Hash: k,
137137
},
138138
)

scripts/gen_sqlc_docker.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,40 @@
22

33
set -e
44

5+
# restore_files is a function to restore original schema files.
6+
restore_files() {
7+
echo "Restoring SQLite bigint patch..."
8+
for file in sqldb/sqlc/migrations/*.up.sql.bak; do
9+
mv "$file" "${file%.bak}"
10+
done
11+
}
12+
13+
14+
# Set trap to call restore_files on script exit. This makes sure the old files
15+
# are always restored.
16+
trap restore_files EXIT
17+
518
# Directory of the script file, independent of where it's called from.
619
DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
720
# Use the user's cache directories
821
GOCACHE=`go env GOCACHE`
922
GOMODCACHE=`go env GOMODCACHE`
1023

24+
# SQLite doesn't support "BIGINT PRIMARY KEY" for auto-incrementing primary
25+
# keys, only "INTEGER PRIMARY KEY". Internally it uses 64-bit integers for
26+
# numbers anyway, independent of the column type. So we can just use
27+
# "INTEGER PRIMARY KEY" and it will work the same under the hood, giving us
28+
# auto incrementing 64-bit integers.
29+
# _BUT_, sqlc will generate Go code with int32 if we use "INTEGER PRIMARY KEY",
30+
# even though we want int64. So before we run sqlc, we need to patch the
31+
# source schema SQL files to use "BIGINT PRIMARY KEY" instead of "INTEGER
32+
# PRIMARY KEY".
33+
echo "Applying SQLite bigint patch..."
34+
for file in sqldb/sqlc/migrations/*.up.sql; do
35+
echo "Patching $file"
36+
sed -i.bak -E 's/INTEGER PRIMARY KEY/BIGINT PRIMARY KEY/g' "$file"
37+
done
38+
1139
echo "Generating sql models and queries in go..."
1240

1341
docker run \

sqldb/postgres.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ var (
2929
// has some differences.
3030
postgresSchemaReplacements = map[string]string{
3131
"BLOB": "BYTEA",
32-
"INTEGER PRIMARY KEY": "SERIAL PRIMARY KEY",
33-
"BIGINT PRIMARY KEY": "BIGSERIAL PRIMARY KEY",
32+
"INTEGER PRIMARY KEY": "BIGSERIAL PRIMARY KEY",
3433
"TIMESTAMP": "TIMESTAMP WITHOUT TIME ZONE",
3534
}
3635

sqldb/sqlc/invoices.sql.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqldb/sqlc/migrations/000001_invoices.up.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ INSERT INTO invoice_sequences(name, current_value) VALUES ('settle_index', 0);
1111
-- invoices table contains all the information shared by all the invoice types.
1212
CREATE TABLE IF NOT EXISTS invoices (
1313
-- The id of the invoice. Translates to the AddIndex.
14-
id BIGINT PRIMARY KEY,
14+
id INTEGER PRIMARY KEY,
1515

1616
-- The hash for this invoice. The invoice hash will always identify that
1717
-- invoice.
@@ -102,16 +102,16 @@ CREATE INDEX IF NOT EXISTS invoice_feature_invoice_id_idx ON invoice_features(in
102102
CREATE TABLE IF NOT EXISTS invoice_htlcs (
103103
-- The id for this htlc. Used in foreign keys instead of the
104104
-- htlc_id/chan_id combination.
105-
id BIGINT PRIMARY KEY,
106-
105+
id INTEGER PRIMARY KEY,
106+
107107
-- Short chan id indicating the htlc's origin. uint64 stored as text.
108108
chan_id TEXT NOT NULL,
109109

110110
-- The uint64 htlc id. This field is a counter so it is safe to store it as
111111
-- int64 in the database. The application layer must check that there is no
112112
-- overflow when storing/loading this column.
113113
htlc_id BIGINT NOT NULL,
114-
114+
115115
-- The htlc's amount in millisatoshis.
116116
amount_msat BIGINT NOT NULL,
117117

sqldb/sqlc/migrations/000003_invoice_events.up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ VALUES
2929
-- AMP sub invoices. This table can be used to create a historical view of what
3030
-- happened to the node's invoices.
3131
CREATE TABLE IF NOT EXISTS invoice_events (
32-
id BIGINT PRIMARY KEY,
32+
id INTEGER PRIMARY KEY,
3333

3434
-- added_at is the timestamp when this event was added.
3535
added_at TIMESTAMP NOT NULL,

sqldb/sqlc/models.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqldb/sqlite.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@ const (
2828
)
2929

3030
var (
31-
// sqliteSchemaReplacements is a map of schema strings that need to be
32-
// replaced for sqlite. This is needed because sqlite doesn't directly
33-
// support the BIGINT type for primary keys, so we need to replace it
34-
// with INTEGER.
35-
sqliteSchemaReplacements = map[string]string{
36-
"BIGINT PRIMARY KEY": "INTEGER PRIMARY KEY",
37-
}
31+
// sqliteSchemaReplacements maps schema strings to their SQLite
32+
// compatible replacements. Currently, no replacements are needed as our
33+
// SQL schema definition files are designed for SQLite compatibility.
34+
sqliteSchemaReplacements = map[string]string{}
3835

3936
// Make sure SqliteStore implements the MigrationExecutor interface.
4037
_ MigrationExecutor = (*SqliteStore)(nil)

0 commit comments

Comments
 (0)