Skip to content

Commit e6b96fb

Browse files
firewalldb: use sqldb v2 in firewalldb package
1 parent 243b56b commit e6b96fb

File tree

8 files changed

+82
-61
lines changed

8 files changed

+82
-61
lines changed

config_dev.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,6 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
103103
return stores, err
104104
}
105105

106-
// Until we have fully added support for sqldb/v2 in all of our
107-
// stores, we need to use the db packages definition of the
108-
// SQLite store for the packages that still haven't added
109-
// support for sqldb/v2. This is only temporary and will be
110-
// removed once all stores have been updated to use sqldb/v2.
111-
legacySqlStore, err := db.NewSqliteStore(cfg.Sqlite)
112-
113106
sqlStore, err := sqldb.NewSqliteStore(&sqldb.SqliteConfig{
114107
SkipMigrations: cfg.Sqlite.SkipMigrations,
115108
SkipMigrationDbBackup: cfg.Sqlite.SkipMigrationDbBackup,
@@ -138,7 +131,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
138131
sqlStore.BaseDB, queries, clock,
139132
)
140133
firewallStore := firewalldb.NewSQLDB(
141-
legacySqlStore.BaseDB, clock,
134+
sqlStore.BaseDB, queries, clock,
142135
)
143136

144137
stores.accounts = acctStore
@@ -147,13 +140,6 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
147140
stores.closeFns["sqlite"] = sqlStore.BaseDB.Close
148141

149142
case DatabaseBackendPostgres:
150-
// Until we have fully added support for sqldb/v2 in all of our
151-
// stores, we need to use the db packages definition of the
152-
// Postgres store for the packages that still haven't added
153-
// support for sqldb/v2. This is only temporary and will be
154-
// removed once all stores have been updated to use sqldb/v2.
155-
legacySqlStore, err := db.NewPostgresStore(cfg.Postgres)
156-
157143
sqlStore, err := sqldb.NewPostgresStore(&sqldb.PostgresConfig{
158144
Dsn: cfg.Postgres.DSN(false),
159145
MaxOpenConnections: cfg.Postgres.MaxOpenConnections,
@@ -187,7 +173,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
187173
sqlStore.BaseDB, queries, clock,
188174
)
189175
firewallStore := firewalldb.NewSQLDB(
190-
legacySqlStore.BaseDB, clock,
176+
sqlStore.BaseDB, queries, clock,
191177
)
192178

193179
stores.accounts = acctStore

firewalldb/actions_sql.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/lightninglabs/lightning-terminal/db/sqlc"
1313
"github.com/lightninglabs/lightning-terminal/session"
1414
"github.com/lightningnetwork/lnd/fn"
15-
"github.com/lightningnetwork/lnd/sqldb"
15+
"github.com/lightningnetwork/lnd/sqldb/v2"
1616
)
1717

1818
// SQLAccountQueries is a subset of the sqlc.Queries interface that can be used
@@ -167,7 +167,7 @@ func (s *SQLDB) AddAction(ctx context.Context,
167167
}
168168

169169
return nil
170-
})
170+
}, sqldb.NoOpReset)
171171
if err != nil {
172172
return nil, err
173173
}
@@ -202,7 +202,7 @@ func (s *SQLDB) SetActionState(ctx context.Context, al ActionLocator,
202202
Valid: errReason != "",
203203
},
204204
})
205-
})
205+
}, sqldb.NoOpReset)
206206
}
207207

208208
// ListActions returns a list of Actions. The query IndexOffset and MaxNum
@@ -350,7 +350,7 @@ func (s *SQLDB) ListActions(ctx context.Context,
350350
}
351351

352352
return nil
353-
})
353+
}, sqldb.NoOpReset)
354354

355355
return actions, lastIndex, uint64(totalCount), err
356356
}

firewalldb/kvstores_sql.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/lightninglabs/lightning-terminal/db/sqlc"
1212
"github.com/lightninglabs/lightning-terminal/session"
1313
"github.com/lightningnetwork/lnd/fn"
14+
"github.com/lightningnetwork/lnd/sqldb/v2"
1415
)
1516

1617
// SQLKVStoreQueries is a subset of the sqlc.Queries interface that can be
@@ -46,7 +47,7 @@ func (s *SQLDB) DeleteTempKVStores(ctx context.Context) error {
4647

4748
return s.db.ExecTx(ctx, &writeTxOpts, func(tx SQLQueries) error {
4849
return tx.DeleteAllTempKVStores(ctx)
49-
})
50+
}, sqldb.NoOpReset)
5051
}
5152

5253
// GetKVStores constructs a new rules.KVStores in a namespace defined by the

firewalldb/sql_migration_test.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ import (
99
"time"
1010

1111
"github.com/lightninglabs/lightning-terminal/accounts"
12-
"github.com/lightninglabs/lightning-terminal/db"
1312
"github.com/lightninglabs/lightning-terminal/db/sqlc"
1413
"github.com/lightninglabs/lightning-terminal/session"
1514
"github.com/lightningnetwork/lnd/clock"
1615
"github.com/lightningnetwork/lnd/fn"
17-
"github.com/lightningnetwork/lnd/sqldb"
16+
"github.com/lightningnetwork/lnd/sqldb/v2"
1817
"github.com/stretchr/testify/require"
1918
"golang.org/x/exp/rand"
2019
)
@@ -54,7 +53,7 @@ func TestFirewallDBMigration(t *testing.T) {
5453
}
5554

5655
makeSQLDB := func(t *testing.T, sessionsStore session.Store) (*SQLDB,
57-
*db.TransactionExecutor[SQLQueries]) {
56+
*SQLQueriesExecutor[SQLQueries]) {
5857

5958
testDBStore := NewTestDBWithSessions(t, sessionsStore, clock)
6059

@@ -63,13 +62,9 @@ func TestFirewallDBMigration(t *testing.T) {
6362

6463
baseDB := store.BaseDB
6564

66-
genericExecutor := db.NewTransactionExecutor(
67-
baseDB, func(tx *sql.Tx) SQLQueries {
68-
return baseDB.WithTx(tx)
69-
},
70-
)
65+
queries := sqlc.NewForType(baseDB, baseDB.BackendType)
7166

72-
return store, genericExecutor
67+
return store, NewSQLQueriesExecutor(baseDB, queries)
7368
}
7469

7570
// The assertMigrationResults function will currently assert that
@@ -88,7 +83,9 @@ func TestFirewallDBMigration(t *testing.T) {
8883
getRuleID := func(ruleName string) int64 {
8984
ruleID, ok := ruleIDs[ruleName]
9085
if !ok {
91-
ruleID, err = store.GetRuleID(ctx, ruleName)
86+
ruleID, err = store.db.GetRuleID(
87+
ctx, ruleName,
88+
)
9289
require.NoError(t, err)
9390

9491
ruleIDs[ruleName] = ruleID
@@ -100,7 +97,7 @@ func TestFirewallDBMigration(t *testing.T) {
10097
getGroupID := func(groupAlias []byte) int64 {
10198
groupID, ok := groupIDs[string(groupAlias)]
10299
if !ok {
103-
groupID, err = store.GetSessionIDByAlias(
100+
groupID, err = store.db.GetSessionIDByAlias(
104101
ctx, groupAlias,
105102
)
106103
require.NoError(t, err)
@@ -114,7 +111,7 @@ func TestFirewallDBMigration(t *testing.T) {
114111
getFeatureID := func(featureName string) int64 {
115112
featureID, ok := featureIDs[featureName]
116113
if !ok {
117-
featureID, err = store.GetFeatureID(
114+
featureID, err = store.db.GetFeatureID(
118115
ctx, featureName,
119116
)
120117
require.NoError(t, err)
@@ -128,7 +125,7 @@ func TestFirewallDBMigration(t *testing.T) {
128125
// First we extract all migrated kv entries from the SQLDB,
129126
// in order to be able to compare them to the original kv
130127
// entries, to ensure that the migration was successful.
131-
sqlKvEntries, err := store.ListAllKVStoresRecords(ctx)
128+
sqlKvEntries, err := store.db.ListAllKVStoresRecords(ctx)
132129
require.NoError(t, err)
133130
require.Equal(t, len(kvEntries), len(sqlKvEntries))
134131

@@ -144,7 +141,7 @@ func TestFirewallDBMigration(t *testing.T) {
144141
ruleID := getRuleID(entry.ruleName)
145142

146143
if entry.groupAlias.IsNone() {
147-
sqlVal, err := store.GetGlobalKVStoreRecord(
144+
sqlVal, err := store.db.GetGlobalKVStoreRecord(
148145
ctx,
149146
sqlc.GetGlobalKVStoreRecordParams{
150147
Key: entry.key,
@@ -162,7 +159,7 @@ func TestFirewallDBMigration(t *testing.T) {
162159
groupAlias := entry.groupAlias.UnwrapOrFail(t)
163160
groupID := getGroupID(groupAlias[:])
164161

165-
v, err := store.GetGroupKVStoreRecord(
162+
v, err := store.db.GetGroupKVStoreRecord(
166163
ctx,
167164
sqlc.GetGroupKVStoreRecordParams{
168165
Key: entry.key,
@@ -187,7 +184,7 @@ func TestFirewallDBMigration(t *testing.T) {
187184
entry.featureName.UnwrapOrFail(t),
188185
)
189186

190-
sqlVal, err := store.GetFeatureKVStoreRecord(
187+
sqlVal, err := store.db.GetFeatureKVStoreRecord(
191188
ctx,
192189
sqlc.GetFeatureKVStoreRecordParams{
193190
Key: entry.key,
@@ -296,12 +293,16 @@ func TestFirewallDBMigration(t *testing.T) {
296293
sqlStore, txEx := makeSQLDB(t, sessionsStore)
297294

298295
// Perform the migration.
299-
err = txEx.ExecTx(ctx, sqldb.WriteTxOpt(),
296+
//
297+
// TODO(viktor): remove sqldb.MigrationTxOptions once
298+
// sqldb v2 is based on the latest version of lnd/sqldb.
299+
var opts sqldb.MigrationTxOptions
300+
err = txEx.ExecTx(ctx, &opts,
300301
func(tx SQLQueries) error {
301302
return MigrateFirewallDBToSQL(
302303
ctx, firewallStore.DB, tx,
303304
)
304-
},
305+
}, sqldb.NoOpReset,
305306
)
306307
require.NoError(t, err)
307308

firewalldb/sql_store.go

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"database/sql"
66

77
"github.com/lightninglabs/lightning-terminal/db"
8+
"github.com/lightninglabs/lightning-terminal/db/sqlc"
89
"github.com/lightningnetwork/lnd/clock"
10+
"github.com/lightningnetwork/lnd/sqldb/v2"
911
)
1012

1113
// SQLSessionQueries is a subset of the sqlc.Queries interface that can be used
@@ -18,17 +20,20 @@ type SQLSessionQueries interface {
1820
// SQLQueries is a subset of the sqlc.Queries interface that can be used to
1921
// interact with various firewalldb tables.
2022
type SQLQueries interface {
23+
sqldb.BaseQuerier
24+
2125
SQLKVStoreQueries
2226
SQLPrivacyPairQueries
2327
SQLActionQueries
2428
}
2529

26-
// BatchedSQLQueries is a version of the SQLQueries that's capable of batched
27-
// database operations.
30+
// BatchedSQLQueries combines the SQLQueries interface with the BatchedTx
31+
// interface, allowing for multiple queries to be executed in single SQL
32+
// transaction.
2833
type BatchedSQLQueries interface {
2934
SQLQueries
3035

31-
db.BatchedTx[SQLQueries]
36+
sqldb.BatchedTx[SQLQueries]
3237
}
3338

3439
// SQLDB represents a storage backend.
@@ -38,11 +43,31 @@ type SQLDB struct {
3843
db BatchedSQLQueries
3944

4045
// BaseDB represents the underlying database connection.
41-
*db.BaseDB
46+
*sqldb.BaseDB
4247

4348
clock clock.Clock
4449
}
4550

51+
type SQLQueriesExecutor[T sqldb.BaseQuerier] struct {
52+
*sqldb.TransactionExecutor[T]
53+
54+
SQLQueries
55+
}
56+
57+
func NewSQLQueriesExecutor(baseDB *sqldb.BaseDB,
58+
queries *sqlc.Queries) *SQLQueriesExecutor[SQLQueries] {
59+
60+
executor := sqldb.NewTransactionExecutor(
61+
baseDB, func(tx *sql.Tx) SQLQueries {
62+
return queries.WithTx(tx)
63+
},
64+
)
65+
return &SQLQueriesExecutor[SQLQueries]{
66+
TransactionExecutor: executor,
67+
SQLQueries: queries,
68+
}
69+
}
70+
4671
// A compile-time assertion to ensure that SQLDB implements the RulesDB
4772
// interface.
4873
var _ RulesDB = (*SQLDB)(nil)
@@ -53,12 +78,10 @@ var _ ActionDB = (*SQLDB)(nil)
5378

5479
// NewSQLDB creates a new SQLStore instance given an open SQLQueries
5580
// storage backend.
56-
func NewSQLDB(sqlDB *db.BaseDB, clock clock.Clock) *SQLDB {
57-
executor := db.NewTransactionExecutor(
58-
sqlDB, func(tx *sql.Tx) SQLQueries {
59-
return sqlDB.WithTx(tx)
60-
},
61-
)
81+
func NewSQLDB(sqlDB *sqldb.BaseDB, queries *sqlc.Queries,
82+
clock clock.Clock) *SQLDB {
83+
84+
executor := NewSQLQueriesExecutor(sqlDB, queries)
6285

6386
return &SQLDB{
6487
db: executor,
@@ -88,7 +111,7 @@ func (e *sqlExecutor[T]) Update(ctx context.Context,
88111
var txOpts db.QueriesTxOptions
89112
return e.db.ExecTx(ctx, &txOpts, func(queries SQLQueries) error {
90113
return fn(ctx, e.wrapTx(queries))
91-
})
114+
}, sqldb.NoOpReset)
92115
}
93116

94117
// View opens a database read transaction and executes the function f with the
@@ -104,5 +127,5 @@ func (e *sqlExecutor[T]) View(ctx context.Context,
104127

105128
return e.db.ExecTx(ctx, &txOpts, func(queries SQLQueries) error {
106129
return fn(ctx, e.wrapTx(queries))
107-
})
130+
}, sqldb.NoOpReset)
108131
}

firewalldb/test_postgres.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111

1212
// NewTestDB is a helper function that creates an BBolt database for testing.
1313
func NewTestDB(t *testing.T, clock clock.Clock) FirewallDBs {
14-
return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock)
14+
return createStore(t, db.NewTestPostgresV2DB(t).BaseDB, clock)
1515
}
1616

1717
// NewTestDBFromPath is a helper function that creates a new BoltStore with a
1818
// connection to an existing BBolt database for testing.
1919
func NewTestDBFromPath(t *testing.T, _ string, clock clock.Clock) FirewallDBs {
20-
return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock)
20+
return createStore(t, db.NewTestPostgresV2DB(t).BaseDB, clock)
2121
}

firewalldb/test_sql.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import (
66
"testing"
77
"time"
88

9+
"github.com/lightninglabs/lightning-terminal/db/sqlc"
10+
911
"github.com/lightninglabs/lightning-terminal/accounts"
10-
"github.com/lightninglabs/lightning-terminal/db"
1112
"github.com/lightninglabs/lightning-terminal/session"
1213
"github.com/lightningnetwork/lnd/clock"
14+
"github.com/lightningnetwork/lnd/sqldb/v2"
1315
"github.com/stretchr/testify/require"
1416
)
1517

@@ -55,8 +57,10 @@ func assertEqualActions(t *testing.T, expected, got *Action) {
5557

5658
// createStore is a helper function that creates a new SQLDB and ensure that
5759
// it is closed when during the test cleanup.
58-
func createStore(t *testing.T, sqlDB *db.BaseDB, clock clock.Clock) *SQLDB {
59-
store := NewSQLDB(sqlDB, clock)
60+
func createStore(t *testing.T, sqlDB *sqldb.BaseDB, clock clock.Clock) *SQLDB {
61+
queries := sqlc.NewForType(sqlDB, sqlDB.BackendType)
62+
63+
store := NewSQLDB(sqlDB, queries, clock)
6064
t.Cleanup(func() {
6165
require.NoError(t, store.Close())
6266
})

firewalldb/test_sqlite.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@ import (
77

88
"github.com/lightninglabs/lightning-terminal/db"
99
"github.com/lightningnetwork/lnd/clock"
10+
"github.com/lightningnetwork/lnd/sqldb/v2"
1011
)
1112

1213
// NewTestDB is a helper function that creates an BBolt database for testing.
1314
func NewTestDB(t *testing.T, clock clock.Clock) FirewallDBs {
14-
return createStore(t, db.NewTestSqliteDB(t).BaseDB, clock)
15+
return createStore(
16+
t, sqldb.NewTestSqliteDB(t, db.LitdMigrationStreams).BaseDB,
17+
clock,
18+
)
1519
}
1620

1721
// NewTestDBFromPath is a helper function that creates a new BoltStore with a
1822
// connection to an existing BBolt database for testing.
19-
func NewTestDBFromPath(t *testing.T, dbPath string, clock clock.Clock) FirewallDBs {
20-
return createStore(
21-
t, db.NewTestSqliteDbHandleFromPath(t, dbPath).BaseDB, clock,
22-
)
23+
func NewTestDBFromPath(t *testing.T, dbPath string,
24+
clock clock.Clock) FirewallDBs {
25+
26+
tDb := sqldb.NewTestSqliteDBFromPath(t, dbPath, db.LitdMigrationStreams)
27+
28+
return createStore(t, tDb.BaseDB, clock)
2329
}

0 commit comments

Comments
 (0)