Skip to content

Commit 37ae765

Browse files
authored
Merge pull request #10697 from ziggie1984/sqldb-v2-rebase
sqldb/v2: introduce sqldb/v2 module
2 parents 0a87772 + 1b3bea5 commit 37ae765

17 files changed

Lines changed: 3112 additions & 0 deletions

sqldb/v2/config.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package sqldb
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
"time"
7+
)
8+
9+
const (
10+
// defaultMaxConns is the number of permitted active and idle
11+
// connections. We want to limit this so it isn't unlimited. We use the
12+
// same value for the number of idle connections as, this can speed up
13+
// queries given a new connection doesn't need to be established each
14+
// time.
15+
defaultMaxConns = 25
16+
17+
// defaultMaxIdleConns is the number of permitted idle connections.
18+
defaultMaxIdleConns = 6
19+
20+
// defaultConnMaxIdleTime is the amount of time a connection can be
21+
// idle before it is closed.
22+
defaultConnMaxIdleTime = 5 * time.Minute
23+
24+
// defaultConnMaxLifetime is the maximum amount of time a connection can
25+
// be reused for before it is closed.
26+
defaultConnMaxLifetime = 10 * time.Minute
27+
)
28+
29+
// SqliteConfig holds all the config arguments needed to interact with our
30+
// sqlite DB.
31+
//
32+
//nolint:ll
33+
type SqliteConfig struct {
34+
Timeout time.Duration `long:"timeout" description:"The time after which a database query should be timed out."`
35+
BusyTimeout time.Duration `long:"busytimeout" description:"The maximum amount of time to wait for a database connection to become available for a query."`
36+
MaxConnections int `long:"maxconnections" description:"The maximum number of open connections to the database."`
37+
MaxIdleConnections int `long:"maxidleconnections" description:"Max number of idle connections to keep in the connection pool."`
38+
ConnMaxLifetime time.Duration `long:"connmaxlifetime" description:"Max amount of time a connection can be reused for before it is closed. Valid time units are {s, m, h}."`
39+
PragmaOptions []string `long:"pragmaoptions" description:"A list of pragma options to set on a database connection. For example, 'auto_vacuum=incremental'. Note that the flag must be specified multiple times if multiple options are to be set."`
40+
SkipMigrations bool `long:"skipmigrations" description:"Skip applying migrations on startup."`
41+
42+
// SkipMigrationDbBackup if true, then a backup of the database will not
43+
// be created before applying migrations.
44+
SkipMigrationDbBackup bool `long:"skipmigrationdbbackup" description:"Skip creating a backup of the database before applying migrations."`
45+
46+
QueryConfig `group:"query" namespace:"query"`
47+
}
48+
49+
const (
50+
// DefaultSqliteBusyTimeout is the default busy_timeout value used
51+
// when no BusyTimeout is configured.
52+
DefaultSqliteBusyTimeout = 5 * time.Second
53+
)
54+
55+
// busyTimeoutMs returns the busy_timeout value in milliseconds. If
56+
// BusyTimeout is not set, it returns the default value.
57+
func (s *SqliteConfig) busyTimeoutMs() int64 {
58+
if s.BusyTimeout > 0 {
59+
return s.BusyTimeout.Milliseconds()
60+
}
61+
62+
return DefaultSqliteBusyTimeout.Milliseconds()
63+
}
64+
65+
// Validate checks that the SqliteConfig values are valid.
66+
func (p *SqliteConfig) Validate() error {
67+
if err := p.QueryConfig.Validate(true); err != nil {
68+
return fmt.Errorf("invalid query config: %w", err)
69+
}
70+
71+
return nil
72+
}
73+
74+
// PostgresConfig holds the postgres database configuration.
75+
//
76+
//nolint:ll
77+
type PostgresConfig struct {
78+
Dsn string `long:"dsn" description:"Database connection string."`
79+
Timeout time.Duration `long:"timeout" description:"Database connection timeout. Set to zero to disable."`
80+
MaxOpenConnections int `long:"maxconnections" description:"Max open connections to keep alive to the database server."`
81+
MaxIdleConnections int `long:"maxidleconnections" description:"Max number of idle connections to keep in the connection pool."`
82+
ConnMaxLifetime time.Duration `long:"connmaxlifetime" description:"Max amount of time a connection can be reused for before it is closed. Valid time units are {s, m, h}."`
83+
ConnMaxIdleTime time.Duration `long:"connmaxidletime" description:"Max amount of time a connection can be idle for before it is closed. Valid time units are {s, m, h}."`
84+
RequireSSL bool `long:"requiressl" description:"Whether to require using SSL (mode: require) when connecting to the server."`
85+
SkipMigrations bool `long:"skipmigrations" description:"Skip applying migrations on startup."`
86+
QueryConfig `group:"query" namespace:"query"`
87+
}
88+
89+
// Validate checks that the PostgresConfig values are valid.
90+
func (p *PostgresConfig) Validate() error {
91+
if p.Dsn == "" {
92+
return fmt.Errorf("DSN is required")
93+
}
94+
95+
// Parse the DSN as a URL.
96+
_, err := url.Parse(p.Dsn)
97+
if err != nil {
98+
return fmt.Errorf("invalid DSN: %w", err)
99+
}
100+
101+
if err := p.QueryConfig.Validate(false); err != nil {
102+
return fmt.Errorf("invalid query config: %w", err)
103+
}
104+
105+
return nil
106+
}

sqldb/v2/go.mod

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
module github.com/lightningnetwork/lnd/sqldb/v2
2+
3+
require (
4+
github.com/btcsuite/btclog/v2 v2.0.1-0.20250728225537-6090e87c6c5b
5+
github.com/davecgh/go-spew v1.1.1
6+
github.com/golang-migrate/migrate/v4 v4.19.0
7+
github.com/jackc/pgconn v1.14.3
8+
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438
9+
github.com/jackc/pgx/v5 v5.7.4
10+
github.com/lightningnetwork/lnd/fn/v2 v2.0.8
11+
github.com/ory/dockertest/v3 v3.10.0
12+
github.com/pmezard/go-difflib v1.0.0
13+
github.com/stretchr/testify v1.10.0
14+
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b
15+
modernc.org/sqlite v1.38.2
16+
)
17+
18+
require (
19+
dario.cat/mergo v1.0.2 // indirect
20+
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
21+
github.com/Microsoft/go-winio v0.6.2 // indirect
22+
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
23+
github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c // indirect
24+
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
25+
github.com/containerd/continuity v0.3.0 // indirect
26+
github.com/containerd/errdefs v1.0.0 // indirect
27+
github.com/containerd/errdefs/pkg v0.3.0 // indirect
28+
github.com/docker/cli v28.1.1+incompatible // indirect
29+
github.com/docker/docker v28.3.3+incompatible // indirect
30+
github.com/docker/go-connections v0.5.0 // indirect
31+
github.com/docker/go-units v0.5.0 // indirect
32+
github.com/dustin/go-humanize v1.0.1 // indirect
33+
github.com/go-viper/mapstructure/v2 v2.3.0 // indirect
34+
github.com/gogo/protobuf v1.3.2 // indirect
35+
github.com/google/go-cmp v0.7.0 // indirect
36+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
37+
github.com/google/uuid v1.6.0 // indirect
38+
github.com/hashicorp/errwrap v1.1.0 // indirect
39+
github.com/hashicorp/go-multierror v1.1.1 // indirect
40+
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
41+
github.com/jackc/pgio v1.0.0 // indirect
42+
github.com/jackc/pgpassfile v1.0.0 // indirect
43+
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
44+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
45+
github.com/jackc/puddle/v2 v2.2.2 // indirect
46+
github.com/mattn/go-isatty v0.0.20 // indirect
47+
github.com/moby/docker-image-spec v1.3.1 // indirect
48+
github.com/moby/sys/user v0.3.0 // indirect
49+
github.com/moby/term v0.5.0 // indirect
50+
github.com/ncruces/go-strftime v0.1.9 // indirect
51+
github.com/opencontainers/go-digest v1.0.0 // indirect
52+
github.com/opencontainers/image-spec v1.1.0 // indirect
53+
github.com/opencontainers/runc v1.2.0 // indirect
54+
github.com/pkg/errors v0.9.1 // indirect
55+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
56+
github.com/sirupsen/logrus v1.9.3 // indirect
57+
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
58+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
59+
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
60+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect
61+
go.opentelemetry.io/otel/trace v1.36.0 // indirect
62+
go.uber.org/atomic v1.10.0 // indirect
63+
golang.org/x/crypto v0.37.0 // indirect
64+
golang.org/x/sync v0.15.0 // indirect
65+
golang.org/x/sys v0.34.0 // indirect
66+
golang.org/x/text v0.24.0 // indirect
67+
gopkg.in/yaml.v3 v3.0.1 // indirect
68+
modernc.org/libc v1.66.3 // indirect; indirectv
69+
modernc.org/mathutil v1.7.1 // indirect
70+
modernc.org/memory v1.11.0 // indirect
71+
)
72+
73+
// We are using a fork of the migration library with custom functionality that
74+
// did not yet make it into the upstream repository.
75+
replace github.com/golang-migrate/migrate/v4 => github.com/lightninglabs/migrate/v4 v4.18.2-9023d66a-fork-pr-2.0.20251211093704-71c1eef09789
76+
77+
go 1.23.12

0 commit comments

Comments
 (0)