Skip to content

Commit 20f2573

Browse files
db+sqlc: introduce sqldb v2 types
1 parent 4c4d67c commit 20f2573

File tree

4 files changed

+66
-24
lines changed

4 files changed

+66
-24
lines changed

db/interfaces.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/lightninglabs/lightning-terminal/db/sqlc"
11+
"github.com/lightningnetwork/lnd/sqldb/v2"
1112
)
1213

1314
var (
@@ -56,7 +57,7 @@ type BatchedTx[Q any] interface {
5657
txBody func(Q) error) error
5758

5859
// Backend returns the type of the database backend used.
59-
Backend() sqlc.BackendType
60+
Backend() sqldb.BackendType
6061
}
6162

6263
// Tx represents a database transaction that can be committed or rolled back.
@@ -277,7 +278,7 @@ func (t *TransactionExecutor[Q]) ExecTx(ctx context.Context,
277278
}
278279

279280
// Backend returns the type of the database backend used.
280-
func (t *TransactionExecutor[Q]) Backend() sqlc.BackendType {
281+
func (t *TransactionExecutor[Q]) Backend() sqldb.BackendType {
281282
return t.BatchedQuerier.Backend()
282283
}
283284

@@ -301,7 +302,7 @@ func (s *BaseDB) BeginTx(ctx context.Context, opts TxOptions) (*sql.Tx, error) {
301302
}
302303

303304
// Backend returns the type of the database backend used.
304-
func (s *BaseDB) Backend() sqlc.BackendType {
305+
func (s *BaseDB) Backend() sqldb.BackendType {
305306
return s.Queries.Backend()
306307
}
307308

db/postgres.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
postgres_migrate "github.com/golang-migrate/migrate/v4/database/postgres"
1010
_ "github.com/golang-migrate/migrate/v4/source/file"
1111
"github.com/lightninglabs/lightning-terminal/db/sqlc"
12+
"github.com/lightningnetwork/lnd/sqldb/v2"
1213
"github.com/stretchr/testify/require"
1314
)
1415

@@ -164,8 +165,26 @@ func (s *PostgresStore) ExecuteMigrations(target MigrationTarget,
164165
)
165166
}
166167

168+
// NewTestPostgresV2DB is a helper function that creates a Postgres database for
169+
// testing, using the sqldb v2 package's definition of the PostgresStore.
170+
func NewTestPostgresV2DB(t *testing.T) *sqldb.PostgresStore {
171+
t.Helper()
172+
173+
t.Logf("Creating new Postgres DB for testing")
174+
175+
sqlFixture := sqldb.NewTestPgFixture(t, DefaultPostgresFixtureLifetime)
176+
t.Cleanup(func() {
177+
sqlFixture.TearDown(t)
178+
})
179+
180+
return sqldb.NewTestPostgresDB(t, sqlFixture, LitdMigrationStreams)
181+
}
182+
167183
// NewTestPostgresDB is a helper function that creates a Postgres database for
168-
// testing.
184+
// testing, using the litd db package's definition of the PostgresStore.
185+
//
186+
// TODO(viktor): remove this once the sqldb v2 package is implemented in
187+
// all of litd's packages.
169188
func NewTestPostgresDB(t *testing.T) *PostgresStore {
170189
t.Helper()
171190

db/sql_migrations.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package db
2+
3+
import (
4+
"github.com/golang-migrate/migrate/v4"
5+
"github.com/golang-migrate/migrate/v4/database/pgx/v5"
6+
"github.com/lightningnetwork/lnd/sqldb/v2"
7+
)
8+
9+
var (
10+
LitdMigrationStream = sqldb.MigrationStream{
11+
MigrateTableName: pgx.DefaultMigrationsTable,
12+
SQLFileDirectory: "sqlc/migrations",
13+
Schemas: sqlSchemas,
14+
15+
// LatestMigrationVersion is the latest migration version of the
16+
// database. This is used to implement downgrade protection for
17+
// the daemon.
18+
//
19+
// NOTE: This MUST be updated when a new migration is added.
20+
LatestMigrationVersion: LatestMigrationVersion,
21+
22+
MakePostMigrationChecks: func(
23+
db *sqldb.BaseDB) (map[uint]migrate.PostStepCallback,
24+
error) {
25+
26+
return make(map[uint]migrate.PostStepCallback), nil
27+
},
28+
}
29+
LitdMigrationStreams = []sqldb.MigrationStream{LitdMigrationStream}
30+
)

db/sqlc/db_custom.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,43 @@ package sqlc
22

33
import (
44
"context"
5-
)
6-
7-
// BackendType is an enum that represents the type of database backend we're
8-
// using.
9-
type BackendType uint8
10-
11-
const (
12-
// BackendTypeUnknown indicates we're using an unknown backend.
13-
BackendTypeUnknown BackendType = iota
145

15-
// BackendTypeSqlite indicates we're using a SQLite backend.
16-
BackendTypeSqlite
17-
18-
// BackendTypePostgres indicates we're using a Postgres backend.
19-
BackendTypePostgres
6+
"github.com/lightningnetwork/lnd/sqldb/v2"
207
)
218

229
// wrappedTX is a wrapper around a DBTX that also stores the database backend
2310
// type.
2411
type wrappedTX struct {
2512
DBTX
2613

27-
backendType BackendType
14+
backendType sqldb.BackendType
2815
}
2916

3017
// Backend returns the type of database backend we're using.
31-
func (q *Queries) Backend() BackendType {
18+
func (q *Queries) Backend() sqldb.BackendType {
3219
wtx, ok := q.db.(*wrappedTX)
3320
if !ok {
3421
// Shouldn't happen unless a new database backend type is added
3522
// but not initialized correctly.
36-
return BackendTypeUnknown
23+
return sqldb.BackendTypeUnknown
3724
}
3825

3926
return wtx.backendType
4027
}
4128

4229
// NewSqlite creates a new Queries instance for a SQLite database.
4330
func NewSqlite(db DBTX) *Queries {
44-
return &Queries{db: &wrappedTX{db, BackendTypeSqlite}}
31+
return &Queries{db: &wrappedTX{db, sqldb.BackendTypeSqlite}}
4532
}
4633

4734
// NewPostgres creates a new Queries instance for a Postgres database.
4835
func NewPostgres(db DBTX) *Queries {
49-
return &Queries{db: &wrappedTX{db, BackendTypePostgres}}
36+
return &Queries{db: &wrappedTX{db, sqldb.BackendTypePostgres}}
37+
}
38+
39+
// NewForType creates a new Queries instance for the given database type.
40+
func NewForType(db DBTX, typ sqldb.BackendType) *Queries {
41+
return &Queries{db: &wrappedTX{db, typ}}
5042
}
5143

5244
// CustomQueries defines a set of custom queries that we define in addition
@@ -62,5 +54,5 @@ type CustomQueries interface {
6254
arg ListActionsParams) ([]Action, error)
6355

6456
// Backend returns the type of the database backend used.
65-
Backend() BackendType
57+
Backend() sqldb.BackendType
6658
}

0 commit comments

Comments
 (0)