sqldb/sqlc: add chain_param schema and queries
What changed, and why it matters
This commit adds a new database table called chain_params to store which Bitcoin network (e.g., mainnet or testnet) the database was created for. It also adds simple read/write queries and registers a new migration. There is no security fix or vulnerability here; it is ordinary infrastructure code.
No security action required. Review as normal schema evolution if part of a larger feature.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces a new SQL schema migration (000015_chain_params) creating a single-row table that records the chain network. It adds sqlc-generated Go code (chain_params.sql.go), updates the Querier interface, and registers the migration version. No input validation, privilege, cryptographic, or network changes are present. The ON CONFLICT DO NOTHING and CHECK (single_row) constraints are benign.
Changed components
sqldb/migrations.gosqldb/sqlc/chain_params.sql.gosqldb/sqlc/migrations/000015_chain_params.down.sqlsqldb/sqlc/migrations/000015_chain_params.up.sqlsqldb/sqlc/models.gosqldb/sqlc/querier.gosqldb/sqlc/queries/chain_params.sqlInspect captured patch +62 / −0
diff --git a/sqldb/migrations.go b/sqldb/migrations.go
index 395bd66..241e5c0 100644
--- a/sqldb/migrations.go
+++ b/sqldb/migrations.go
@@ -131,6 +131,11 @@ var (
Version: 17,
SchemaVersion: 14,
},
+ {
+ Name: "000015_chain_params",
+ Version: 18,
+ SchemaVersion: 15,
+ },
}, migrationAdditions...)
// ErrMigrationMismatch is returned when a migrated record does not
diff --git a/sqldb/sqlc/chain_params.sql.go b/sqldb/sqlc/chain_params.sql.go
new file mode 100644
index 0000000..bb7b442
--- /dev/null
+++ b/sqldb/sqlc/chain_params.sql.go
@@ -0,0 +1,33 @@
+// Code generated by sqlc. DO NOT EDIT.
+// versions:
+// sqlc v1.29.0
+// source: chain_params.sql
+
+package sqlc
+
+import (
+ "context"
+)
+
+const getChainNetwork = `-- name: GetChainNetwork :one
+SELECT network FROM chain_params
+WHERE single_row = TRUE
+`
+
+func (q *Queries) GetChainNetwork(ctx context.Context) (string, error) {
+ row := q.db.QueryRowContext(ctx, getChainNetwork)
+ var network string
+ err := row.Scan(&network)
+ return network, err
+}
+
+const insertChainNetwork = `-- name: InsertChainNetwork :exec
+INSERT INTO chain_params (single_row, network)
+VALUES (TRUE, $1)
+ON CONFLICT (single_row) DO NOTHING
+`
+
+func (q *Queries) InsertChainNetwork(ctx context.Context, network string) error {
+ _, err := q.db.ExecContext(ctx, insertChainNetwork, network)
+ return err
+}
diff --git a/sqldb/sqlc/migrations/000015_chain_params.down.sql b/sqldb/sqlc/migrations/000015_chain_params.down.sql
new file mode 100644
index 0000000..4624cf8
--- /dev/null
+++ b/sqldb/sqlc/migrations/000015_chain_params.down.sql
@@ -0,0 +1 @@
+DROP TABLE IF EXISTS chain_params;
diff --git a/sqldb/sqlc/migrations/000015_chain_params.up.sql b/sqldb/sqlc/migrations/000015_chain_params.up.sql
new file mode 100644
index 0000000..cf2a3f6
--- /dev/null
+++ b/sqldb/sqlc/migrations/000015_chain_params.up.sql
@@ -0,0 +1,8 @@
+-- The chain_params table stores chain-level properties of the database. It is
+-- used to persist and validate chain-specific invariants across restarts, such
+-- as which Bitcoin network the database was initialised for. The single_row
+-- column is a boolean primary key that enforces exactly one row in the table.
+CREATE TABLE IF NOT EXISTS chain_params (
+ single_row BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (single_row),
+ network TEXT NOT NULL
+);
diff --git a/sqldb/sqlc/models.go b/sqldb/sqlc/models.go
index 513f045..ef9aa90 100644
--- a/sqldb/sqlc/models.go
+++ b/sqldb/sqlc/models.go
@@ -28,6 +28,11 @@ type AmpSubInvoiceHtlc struct {
Preimage []byte
}
+type ChainParam struct {
+ SingleRow bool
+ Network string
+}
+
type GraphChannel struct {
ID int64
Version int16
diff --git a/sqldb/sqlc/querier.go b/sqldb/sqlc/querier.go
index 3c16292..9b95a66 100644
--- a/sqldb/sqlc/querier.go
+++ b/sqldb/sqlc/querier.go
@@ -103,6 +103,7 @@ type Querier interface {
FilterPayments(ctx context.Context, arg FilterPaymentsParams) ([]FilterPaymentsRow, error)
FilterPaymentsDesc(ctx context.Context, arg FilterPaymentsDescParams) ([]FilterPaymentsDescRow, error)
GetAMPInvoiceID(ctx context.Context, setID []byte) (int64, error)
+ GetChainNetwork(ctx context.Context) (string, error)
GetChannelAndNodesBySCID(ctx context.Context, arg GetChannelAndNodesBySCIDParams) (GetChannelAndNodesBySCIDRow, error)
GetChannelByOutpointWithPolicies(ctx context.Context, arg GetChannelByOutpointWithPoliciesParams) (GetChannelByOutpointWithPoliciesRow, error)
GetChannelBySCID(ctx context.Context, arg GetChannelBySCIDParams) (GraphChannel, error)
@@ -168,6 +169,7 @@ type Querier interface {
HighestSCID(ctx context.Context, version int16) ([]byte, error)
InsertAMPSubInvoice(ctx context.Context, arg InsertAMPSubInvoiceParams) error
InsertAMPSubInvoiceHTLC(ctx context.Context, arg InsertAMPSubInvoiceHTLCParams) error
+ InsertChainNetwork(ctx context.Context, network string) error
InsertChannelFeature(ctx context.Context, arg InsertChannelFeatureParams) error
// NOTE: This query is only meant to be used by the graph SQL migration since
// for that migration, in order to be retry-safe, we don't want to error out if
diff --git a/sqldb/sqlc/queries/chain_params.sql b/sqldb/sqlc/queries/chain_params.sql
new file mode 100644
index 0000000..596ff4b
--- /dev/null
+++ b/sqldb/sqlc/queries/chain_params.sql
@@ -0,0 +1,8 @@
+-- name: InsertChainNetwork :exec
+INSERT INTO chain_params (single_row, network)
+VALUES (TRUE, sqlc.arg(network))
+ON CONFLICT (single_row) DO NOTHING;
+
+-- name: GetChainNetwork :one
+SELECT network FROM chain_params
+WHERE single_row = TRUE;
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.