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.
This commit is contained in:
Andras Banki-Horvath
2025-01-21 17:00:15 +01:00
parent ea98933317
commit 84598b6dc1
8 changed files with 44 additions and 20 deletions

View File

@@ -29,8 +29,7 @@ var (
// has some differences.
postgresSchemaReplacements = map[string]string{
"BLOB": "BYTEA",
"INTEGER PRIMARY KEY": "SERIAL PRIMARY KEY",
"BIGINT PRIMARY KEY": "BIGSERIAL PRIMARY KEY",
"INTEGER PRIMARY KEY": "BIGSERIAL PRIMARY KEY",
"TIMESTAMP": "TIMESTAMP WITHOUT TIME ZONE",
}

View File

@@ -591,7 +591,7 @@ INSERT INTO invoice_payment_hashes (
`
type InsertKVInvoiceKeyAndAddIndexParams struct {
ID int32
ID int64
AddIndex int64
}
@@ -675,7 +675,7 @@ WHERE id = $1
`
type SetKVInvoicePaymentHashParams struct {
ID int32
ID int64
Hash []byte
}

View File

@@ -11,7 +11,7 @@ INSERT INTO invoice_sequences(name, current_value) VALUES ('settle_index', 0);
-- invoices table contains all the information shared by all the invoice types.
CREATE TABLE IF NOT EXISTS invoices (
-- The id of the invoice. Translates to the AddIndex.
id BIGINT PRIMARY KEY,
id INTEGER PRIMARY KEY,
-- The hash for this invoice. The invoice hash will always identify that
-- invoice.
@@ -102,8 +102,8 @@ CREATE INDEX IF NOT EXISTS invoice_feature_invoice_id_idx ON invoice_features(in
CREATE TABLE IF NOT EXISTS invoice_htlcs (
-- The id for this htlc. Used in foreign keys instead of the
-- htlc_id/chan_id combination.
id BIGINT PRIMARY KEY,
id INTEGER PRIMARY KEY,
-- Short chan id indicating the htlc's origin. uint64 stored as text.
chan_id TEXT NOT NULL,
@@ -111,7 +111,7 @@ CREATE TABLE IF NOT EXISTS invoice_htlcs (
-- int64 in the database. The application layer must check that there is no
-- overflow when storing/loading this column.
htlc_id BIGINT NOT NULL,
-- The htlc's amount in millisatoshis.
amount_msat BIGINT NOT NULL,

View File

@@ -29,7 +29,7 @@ VALUES
-- AMP sub invoices. This table can be used to create a historical view of what
-- happened to the node's invoices.
CREATE TABLE IF NOT EXISTS invoice_events (
id BIGINT PRIMARY KEY,
id INTEGER PRIMARY KEY,
-- added_at is the timestamp when this event was added.
added_at TIMESTAMP NOT NULL,

View File

@@ -58,7 +58,7 @@ type InvoiceEvent struct {
}
type InvoiceEventType struct {
ID int32
ID int64
Description string
}
@@ -88,7 +88,7 @@ type InvoiceHtlcCustomRecord struct {
}
type InvoicePaymentHash struct {
ID int32
ID int64
AddIndex int64
Hash []byte
}

View File

@@ -28,13 +28,10 @@ const (
)
var (
// sqliteSchemaReplacements is a map of schema strings that need to be
// replaced for sqlite. This is needed because sqlite doesn't directly
// support the BIGINT type for primary keys, so we need to replace it
// with INTEGER.
sqliteSchemaReplacements = map[string]string{
"BIGINT PRIMARY KEY": "INTEGER PRIMARY KEY",
}
// sqliteSchemaReplacements maps schema strings to their SQLite
// compatible replacements. Currently, no replacements are needed as our
// SQL schema definition files are designed for SQLite compatibility.
sqliteSchemaReplacements = map[string]string{}
// Make sure SqliteStore implements the MigrationExecutor interface.
_ MigrationExecutor = (*SqliteStore)(nil)