What changed, and why it matters
This commit is a routine internal refactoring of the database helper code in LND's new sqldb/v2 package. It removes a dependency on the older sqldb/sqlc package, adds a way to track whether SQLite or Postgres is being used at runtime, and adds a flag to optionally skip database migrations. There is no indication this fixes or introduces a security vulnerability.
No security action required. Review as normal code-quality/refactoring change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors sqldb/v2.BaseDB so it no longer embeds *sqlc.Queries from github.com/lightningnetwork/lnd/sqldb. It introduces a BackendType enum (Unknown/Sqlite/Postgres), a BaseQuerier interface with a Backend() method, and constrains BatchedTx and TransactionExecutor generic parameters to BaseQuerier. BaseDB now stores BackendType and SkipMigrations fields, and PostgresStore/SqliteStore constructors populate them. The dependency on lnd/sqldb is removed from go.mod. No security-relevant behavior changes are visible in the diff.
Changed components
sqldb/v2/interfaces.gosqldb/v2/postgres.gosqldb/v2/sqlite.gosqldb/v2/go.modInspect captured patch +46 / −33
diff --git a/sqldb/v2/go.mod b/sqldb/v2/go.mod
index 5e62626..56747ef 100644
--- a/sqldb/v2/go.mod
+++ b/sqldb/v2/go.mod
@@ -8,7 +8,6 @@ require (
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438
github.com/jackc/pgx/v5 v5.5.4
github.com/lightningnetwork/lnd/fn/v2 v2.0.8
- github.com/lightningnetwork/lnd/sqldb v1.0.10
github.com/ory/dockertest/v3 v3.10.0
github.com/pmezard/go-difflib v1.0.0
github.com/stretchr/testify v1.10.0
diff --git a/sqldb/v2/go.sum b/sqldb/v2/go.sum
index 69570b5..3153a6f 100644
--- a/sqldb/v2/go.sum
+++ b/sqldb/v2/go.sum
@@ -93,8 +93,6 @@ github.com/lightninglabs/migrate/v4 v4.18.2-9023d66a-fork-pr-2.0.20251211093704-
github.com/lightninglabs/migrate/v4 v4.18.2-9023d66a-fork-pr-2.0.20251211093704-71c1eef09789/go.mod h1:99BKpIi6ruaaXRM1A77eqZ+FWPQ3cfRa+ZVy5bmWMaY=
github.com/lightningnetwork/lnd/fn/v2 v2.0.8 h1:r2SLz7gZYQPVc3IZhU82M66guz3Zk2oY+Rlj9QN5S3g=
github.com/lightningnetwork/lnd/fn/v2 v2.0.8/go.mod h1:TOzwrhjB/Azw1V7aa8t21ufcQmdsQOQMDtxVOQWNl8s=
-github.com/lightningnetwork/lnd/sqldb v1.0.10 h1:ZLV7TGwjnKupVfCd+DJ43MAc9BKVSFCnvhpSPGKdN3M=
-github.com/lightningnetwork/lnd/sqldb v1.0.10/go.mod h1:c/vWoQfcxu6FAfHzGajkIQi7CEIeIZFhhH4DYh1BJpc=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
diff --git a/sqldb/v2/interfaces.go b/sqldb/v2/interfaces.go
index b9cf552..4c9102f 100644
--- a/sqldb/v2/interfaces.go
+++ b/sqldb/v2/interfaces.go
@@ -4,7 +4,6 @@ import (
"context"
"database/sql"
"fmt"
- "github.com/lightningnetwork/lnd/sqldb/sqlc"
"math"
"math/rand"
prand "math/rand"
@@ -31,6 +30,21 @@ const (
DefaultMaxRetryDelay = time.Second
)
+// BackendType is an enum that represents the type of database backend we're
+// using.
+type BackendType uint8
+
+const (
+ // BackendTypeUnknown indicates we're using an unknown backend.
+ BackendTypeUnknown BackendType = iota
+
+ // BackendTypeSqlite indicates we're using a SQLite backend.
+ BackendTypeSqlite
+
+ // BackendTypePostgres indicates we're using a Postgres backend.
+ BackendTypePostgres
+)
+
// TxOptions represents a set of options one can use to control what type of
// database transaction is created. Transaction can be either read or write.
type TxOptions interface {
@@ -67,12 +81,20 @@ func ReadTxOpt() TxOptions {
}
}
+// BaseQuerier is a generic interface that represents the base methods that any
+// database backend implementation which uses a Querier for its operations must
+// implement.
+type BaseQuerier interface {
+ // Backend returns the type of the database backend used.
+ Backend() BackendType
+}
+
// BatchedTx is a generic interface that represents the ability to execute
// several operations to a given storage interface in a single atomic
// transaction. Typically, Q here will be some subset of the main sqlc.Querier
// interface allowing it to only depend on the routines it needs to implement
// any additional business logic.
-type BatchedTx[Q any] interface {
+type BatchedTx[Q BaseQuerier] interface {
// ExecTx will execute the passed txBody, operating upon generic
// parameter Q (usually a storage interface) in a single transaction.
//
@@ -81,6 +103,9 @@ type BatchedTx[Q any] interface {
// type of concurrency control should be used.
ExecTx(ctx context.Context, txOptions TxOptions,
txBody func(Q) error, reset func()) error
+
+ // Backend returns the type of the database backend used.
+ Backend() BackendType
}
// Tx represents a database transaction that can be committed or rolled back.
@@ -158,7 +183,7 @@ func WithTxRetryDelay(delay time.Duration) TxExecutorOption {
// query a type needs to run under a database transaction, and also the set of
// options for that transaction. The QueryCreator is used to create a query
// given a database transaction created by the BatchedQuerier.
-type TransactionExecutor[Query any] struct {
+type TransactionExecutor[Query BaseQuerier] struct {
BatchedQuerier
createQuery QueryCreator[Query]
@@ -169,7 +194,7 @@ type TransactionExecutor[Query any] struct {
// NewTransactionExecutor creates a new instance of a TransactionExecutor given
// a Querier query object and a concrete type for the type of transactions the
// Querier understands.
-func NewTransactionExecutor[Querier any](db BatchedQuerier,
+func NewTransactionExecutor[Querier BaseQuerier](db BatchedQuerier,
createQuery QueryCreator[Querier],
opts ...TxExecutorOption) *TransactionExecutor[Querier] {
@@ -399,7 +424,12 @@ type DB interface {
type BaseDB struct {
*sql.DB
- *sqlc.Queries
+ // BackendType defines the type of database backend the database is.
+ BackendType BackendType
+
+ // SkipMigrations can be set to true to skip running any migrations
+ // during the iinitialization of the database.
+ SkipMigrations bool
}
// BeginTx wraps the normal sql specific BeginTx method with the TxOptions
@@ -413,3 +443,8 @@ func (s *BaseDB) BeginTx(ctx context.Context, opts TxOptions) (*sql.Tx, error) {
return s.DB.BeginTx(ctx, &sqlOptions)
}
+
+// Backend returns the type of the database backend used.
+func (s *BaseDB) Backend() BackendType {
+ return s.BackendType
+}
diff --git a/sqldb/v2/postgres.go b/sqldb/v2/postgres.go
index 6db0e40..8dcbdad 100644
--- a/sqldb/v2/postgres.go
+++ b/sqldb/v2/postgres.go
@@ -12,7 +12,6 @@ import (
_ "github.com/golang-migrate/migrate/v4/source/file" // Read migrations from files. // nolint:ll
_ "github.com/jackc/pgx/v5"
"github.com/lightningnetwork/lnd/fn/v2"
- "github.com/lightningnetwork/lnd/sqldb/sqlc"
)
var (
@@ -131,13 +130,12 @@ func NewPostgresStore(cfg *PostgresConfig) (*PostgresStore, error) {
db.SetMaxIdleConns(maxConns)
db.SetConnMaxLifetime(connIdleLifetime)
- queries := sqlc.New(db)
-
return &PostgresStore{
cfg: cfg,
BaseDB: &BaseDB{
- DB: db,
- Queries: queries,
+ DB: db,
+ BackendType: BackendTypePostgres,
+ SkipMigrations: cfg.SkipMigrations,
},
}, nil
}
diff --git a/sqldb/v2/postgres_test.go b/sqldb/v2/postgres_test.go
index de6bac1..6f94699 100644
--- a/sqldb/v2/postgres_test.go
+++ b/sqldb/v2/postgres_test.go
@@ -7,14 +7,6 @@ import (
"testing"
)
-// isSQLite is false if the build tag is set to test_db_postgres. It is used in
-// tests that compile for both SQLite and Postgres databases to determine
-// which database implementation is being used.
-//
-// TODO(elle): once we've updated to using sqldbv2, we can remove this since
-// then we will have access to the DatabaseType on the BaseDB struct at runtime.
-const isSQLite = false
-
// NewTestDB is a helper function that creates a Postgres database for testing.
func NewTestDB(t *testing.T, sets []MigrationSet) *PostgresStore {
pgFixture := NewTestPgFixture(t, DefaultPostgresFixtureLifetime)
diff --git a/sqldb/v2/sqlite.go b/sqldb/v2/sqlite.go
index 017eb06..4775afc 100644
--- a/sqldb/v2/sqlite.go
+++ b/sqldb/v2/sqlite.go
@@ -13,7 +13,6 @@ import (
"time"
sqlite_migrate "github.com/golang-migrate/migrate/v4/database/sqlite"
- "github.com/lightningnetwork/lnd/sqldb/sqlc"
"github.com/stretchr/testify/require"
_ "modernc.org/sqlite" // Register relevant drivers.
)
@@ -143,14 +142,14 @@ func NewSqliteStore(cfg *SqliteConfig, dbPath string) (*SqliteStore, error) {
db.SetMaxOpenConns(defaultMaxConns)
db.SetMaxIdleConns(defaultMaxConns)
db.SetConnMaxLifetime(connIdleLifetime)
- queries := sqlc.New(db)
s := &SqliteStore{
Config: cfg,
DbPath: dbPath,
BaseDB: &BaseDB{
- DB: db,
- Queries: queries,
+ DB: db,
+ BackendType: BackendTypeSqlite,
+ SkipMigrations: cfg.SkipMigrations,
},
}
diff --git a/sqldb/v2/sqlite_test.go b/sqldb/v2/sqlite_test.go
index 89d2a09..6105080 100644
--- a/sqldb/v2/sqlite_test.go
+++ b/sqldb/v2/sqlite_test.go
@@ -6,14 +6,6 @@ import (
"testing"
)
-// isSQLite is true if the build tag is set to test_db_sqlite. It is used in
-// tests that compile for both SQLite and Postgres databases to determine
-// which database implementation is being used.
-//
-// TODO(elle): once we've updated to using sqldbv2, we can remove this since
-// then we will have access to the DatabaseType on the BaseDB struct at runtime.
-const isSQLite = true
-
// NewTestDB is a helper function that creates an SQLite database for testing.
func NewTestDB(t *testing.T, sets []MigrationSet) *SqliteStore {
return NewTestSqliteDB(t, sets)
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.