Skip to content

Commit 2c246ce

Browse files
firewalldb: use sqlcmig6 for kvdb to sql migration
This commit updates the firewalldb package to use the new `sqlcmig6` package for kvdb to SQL migration.
1 parent 6693fbc commit 2c246ce

File tree

2 files changed

+36
-37
lines changed

2 files changed

+36
-37
lines changed

firewalldb/sql_migration.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import (
66
"database/sql"
77
"errors"
88
"fmt"
9-
10-
"github.com/lightninglabs/lightning-terminal/db/sqlc"
9+
"github.com/lightninglabs/lightning-terminal/db/sqlcmig6"
1110
"github.com/lightningnetwork/lnd/fn"
1211
"github.com/lightningnetwork/lnd/sqldb"
1312
"go.etcd.io/bbolt"
@@ -80,7 +79,7 @@ type privacyPairs = map[int64]map[string]string
8079
// NOTE: As sessions may contain linked sessions and accounts, the sessions and
8180
// accounts sql migration MUST be run prior to this migration.
8281
func MigrateFirewallDBToSQL(ctx context.Context, kvStore *bbolt.DB,
83-
sqlTx SQLQueries) error {
82+
sqlTx *sqlcmig6.Queries) error {
8483

8584
log.Infof("Starting migration of the rules DB to SQL")
8685

@@ -105,7 +104,7 @@ func MigrateFirewallDBToSQL(ctx context.Context, kvStore *bbolt.DB,
105104
// database to the SQL database. The function also asserts that the
106105
// migrated values match the original values in the KV store.
107106
func migrateKVStoresDBToSQL(ctx context.Context, kvStore *bbolt.DB,
108-
sqlTx SQLQueries) error {
107+
sqlTx *sqlcmig6.Queries) error {
109108

110109
log.Infof("Starting migration of the KV stores to SQL")
111110

@@ -367,15 +366,15 @@ func collectKVPairs(bkt *bbolt.Bucket, errorOnBuckets, perm bool,
367366
}
368367

369368
// insertPair inserts a single key-value pair into the SQL database.
370-
func insertPair(ctx context.Context, tx SQLQueries,
369+
func insertPair(ctx context.Context, tx *sqlcmig6.Queries,
371370
entry *kvEntry) (*sqlKvEntry, error) {
372371

373372
ruleID, err := tx.GetOrInsertRuleID(ctx, entry.ruleName)
374373
if err != nil {
375374
return nil, err
376375
}
377376

378-
p := sqlc.InsertKVStoreRecordParams{
377+
p := sqlcmig6.InsertKVStoreRecordParams{
379378
Perm: entry.perm,
380379
RuleID: ruleID,
381380
EntryKey: entry.key,
@@ -427,13 +426,13 @@ func insertPair(ctx context.Context, tx SQLQueries,
427426

428427
// getSQLValue retrieves the key value for the given kvEntry from the SQL
429428
// database.
430-
func getSQLValue(ctx context.Context, tx SQLQueries,
429+
func getSQLValue(ctx context.Context, tx *sqlcmig6.Queries,
431430
entry *sqlKvEntry) ([]byte, error) {
432431

433432
switch {
434433
case entry.featureID.Valid && entry.groupID.Valid:
435434
return tx.GetFeatureKVStoreRecord(
436-
ctx, sqlc.GetFeatureKVStoreRecordParams{
435+
ctx, sqlcmig6.GetFeatureKVStoreRecordParams{
437436
Perm: entry.perm,
438437
RuleID: entry.ruleID,
439438
GroupID: entry.groupID,
@@ -443,7 +442,7 @@ func getSQLValue(ctx context.Context, tx SQLQueries,
443442
)
444443
case entry.groupID.Valid:
445444
return tx.GetGroupKVStoreRecord(
446-
ctx, sqlc.GetGroupKVStoreRecordParams{
445+
ctx, sqlcmig6.GetGroupKVStoreRecordParams{
447446
Perm: entry.perm,
448447
RuleID: entry.ruleID,
449448
GroupID: entry.groupID,
@@ -452,7 +451,7 @@ func getSQLValue(ctx context.Context, tx SQLQueries,
452451
)
453452
case !entry.featureID.Valid && !entry.groupID.Valid:
454453
return tx.GetGlobalKVStoreRecord(
455-
ctx, sqlc.GetGlobalKVStoreRecordParams{
454+
ctx, sqlcmig6.GetGlobalKVStoreRecordParams{
456455
Perm: entry.perm,
457456
RuleID: entry.ruleID,
458457
Key: entry.key,
@@ -501,7 +500,7 @@ func verifyBktKeys(bkt *bbolt.Bucket, errorOnKeyValues bool,
501500
// from the KV database to the SQL database. The function also asserts that the
502501
// migrated values match the original values in the privacy mapper store.
503502
func migratePrivacyMapperDBToSQL(ctx context.Context, kvStore *bbolt.DB,
504-
sqlTx SQLQueries) error {
503+
sqlTx *sqlcmig6.Queries) error {
505504

506505
log.Infof("Starting migration of the privacy mapper store to SQL")
507506

@@ -536,7 +535,7 @@ func migratePrivacyMapperDBToSQL(ctx context.Context, kvStore *bbolt.DB,
536535

537536
// collectPrivacyPairs collects all privacy pairs from the KV store.
538537
func collectPrivacyPairs(ctx context.Context, kvStore *bbolt.DB,
539-
sqlTx SQLQueries) (privacyPairs, error) {
538+
sqlTx *sqlcmig6.Queries) (privacyPairs, error) {
540539

541540
groupPairs := make(privacyPairs)
542541

@@ -665,7 +664,7 @@ func collectPairs(pairsBucket *bbolt.Bucket) (map[string]string, error) {
665664
}
666665

667666
// insertPrivacyPairs inserts the collected privacy pairs into the SQL database.
668-
func insertPrivacyPairs(ctx context.Context, sqlTx SQLQueries,
667+
func insertPrivacyPairs(ctx context.Context, sqlTx *sqlcmig6.Queries,
669668
pairs privacyPairs) error {
670669

671670
for groupId, groupPairs := range pairs {
@@ -684,12 +683,12 @@ func insertPrivacyPairs(ctx context.Context, sqlTx SQLQueries,
684683
// an error if a duplicate pair is found. The function takes a map of real
685684
// to pseudo values, where the key is the real value and the value is the
686685
// corresponding pseudo value.
687-
func insertGroupPairs(ctx context.Context, sqlTx SQLQueries, groupID int64,
688-
pairs map[string]string) error {
686+
func insertGroupPairs(ctx context.Context, sqlTx *sqlcmig6.Queries,
687+
groupID int64, pairs map[string]string) error {
689688

690689
for realVal, pseudoVal := range pairs {
691690
err := sqlTx.InsertPrivacyPair(
692-
ctx, sqlc.InsertPrivacyPairParams{
691+
ctx, sqlcmig6.InsertPrivacyPairParams{
693692
GroupID: groupID,
694693
RealVal: realVal,
695694
PseudoVal: pseudoVal,
@@ -706,7 +705,7 @@ func insertGroupPairs(ctx context.Context, sqlTx SQLQueries, groupID int64,
706705

707706
// validatePrivacyPairsMigration validates that the migrated privacy pairs
708707
// match the original values in the KV store.
709-
func validatePrivacyPairsMigration(ctx context.Context, sqlTx SQLQueries,
708+
func validatePrivacyPairsMigration(ctx context.Context, sqlTx *sqlcmig6.Queries,
710709
pairs privacyPairs) error {
711710

712711
for groupId, groupPairs := range pairs {
@@ -727,12 +726,12 @@ func validatePrivacyPairsMigration(ctx context.Context, sqlTx SQLQueries,
727726
// for each real value, the pseudo value in the SQL database matches the
728727
// original pseudo value, and vice versa. If any mismatch is found, it returns
729728
// an error indicating the mismatch.
730-
func validateGroupPairsMigration(ctx context.Context, sqlTx SQLQueries,
729+
func validateGroupPairsMigration(ctx context.Context, sqlTx *sqlcmig6.Queries,
731730
groupID int64, pairs map[string]string) error {
732731

733732
for realVal, pseudoVal := range pairs {
734733
resPseudoVal, err := sqlTx.GetPseudoForReal(
735-
ctx, sqlc.GetPseudoForRealParams{
734+
ctx, sqlcmig6.GetPseudoForRealParams{
736735
GroupID: groupID,
737736
RealVal: realVal,
738737
},
@@ -752,7 +751,7 @@ func validateGroupPairsMigration(ctx context.Context, sqlTx SQLQueries,
752751
}
753752

754753
resRealVal, err := sqlTx.GetRealForPseudo(
755-
ctx, sqlc.GetRealForPseudoParams{
754+
ctx, sqlcmig6.GetRealForPseudoParams{
756755
GroupID: groupID,
757756
PseudoVal: pseudoVal,
758757
},

firewalldb/sql_migration_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/lightninglabs/lightning-terminal/accounts"
13-
"github.com/lightninglabs/lightning-terminal/db/sqlc"
13+
"github.com/lightninglabs/lightning-terminal/db/sqlcmig6"
1414
"github.com/lightninglabs/lightning-terminal/session"
1515
"github.com/lightningnetwork/lnd/clock"
1616
"github.com/lightningnetwork/lnd/fn"
@@ -56,26 +56,26 @@ func TestFirewallDBMigration(t *testing.T) {
5656
t.Skipf("Skipping Firewall DB migration test for kvdb build")
5757
}
5858

59-
makeSQLDB := func(t *testing.T, sessionsStore session.Store) (*SQLDB,
60-
*SQLQueriesExecutor[SQLQueries]) {
59+
makeSQLDB := func(t *testing.T, sStore session.Store) (*SQLDB,
60+
*sqlcmig6.TxExecutor[*sqlcmig6.Queries]) {
6161

62-
testDBStore := NewTestDBWithSessions(t, sessionsStore, clock)
62+
testDBStore := NewTestDBWithSessions(t, sStore, clock)
6363

6464
store, ok := testDBStore.(*SQLDB)
6565
require.True(t, ok)
6666

6767
baseDB := store.BaseDB
6868

69-
queries := sqlc.NewForType(baseDB, baseDB.BackendType)
69+
queries := sqlcmig6.NewForType(baseDB, baseDB.BackendType)
7070

71-
return store, NewSQLQueriesExecutor(baseDB, queries)
71+
return store, sqlcmig6.NewTxExecutor(baseDB, queries)
7272
}
7373

7474
// The assertKvStoreMigrationResults function will currently assert that
7575
// the migrated kv stores entries in the SQLDB match the original kv
7676
// stores entries in the BoltDB.
77-
assertKvStoreMigrationResults := func(t *testing.T, store *sqlc.Queries,
78-
kvEntries []*kvEntry) {
77+
assertKvStoreMigrationResults := func(t *testing.T,
78+
store *sqlcmig6.Queries, kvEntries []*kvEntry) {
7979

8080
var (
8181
ruleIDs = make(map[string]int64)
@@ -145,7 +145,7 @@ func TestFirewallDBMigration(t *testing.T) {
145145
if entry.groupAlias.IsNone() {
146146
sqlVal, err := store.GetGlobalKVStoreRecord(
147147
ctx,
148-
sqlc.GetGlobalKVStoreRecordParams{
148+
sqlcmig6.GetGlobalKVStoreRecordParams{
149149
Key: entry.key,
150150
Perm: entry.perm,
151151
RuleID: ruleID,
@@ -163,7 +163,7 @@ func TestFirewallDBMigration(t *testing.T) {
163163

164164
v, err := store.GetGroupKVStoreRecord(
165165
ctx,
166-
sqlc.GetGroupKVStoreRecordParams{
166+
sqlcmig6.GetGroupKVStoreRecordParams{
167167
Key: entry.key,
168168
Perm: entry.perm,
169169
RuleID: ruleID,
@@ -188,7 +188,7 @@ func TestFirewallDBMigration(t *testing.T) {
188188

189189
sqlVal, err := store.GetFeatureKVStoreRecord(
190190
ctx,
191-
sqlc.GetFeatureKVStoreRecordParams{
191+
sqlcmig6.GetFeatureKVStoreRecordParams{
192192
Key: entry.key,
193193
Perm: entry.perm,
194194
RuleID: ruleID,
@@ -217,7 +217,7 @@ func TestFirewallDBMigration(t *testing.T) {
217217
// BoltDB. It also asserts that the SQL DB does not contain any other
218218
// privacy pairs than the expected ones.
219219
assertPrivacyMapperMigrationResults := func(t *testing.T,
220-
sqlStore *sqlc.Queries, privPairs privacyPairs) {
220+
sqlStore *sqlcmig6.Queries, privPairs privacyPairs) {
221221

222222
var totalExpectedPairs, totalPairs int
223223

@@ -269,7 +269,7 @@ func TestFirewallDBMigration(t *testing.T) {
269269
// The assertMigrationResults asserts that the migrated entries in the
270270
// firewall SQLDB match the expected results which should represent the
271271
// original entries in the BoltDB.
272-
assertMigrationResults := func(t *testing.T, sqlStore *sqlc.Queries,
272+
assertMigrationResults := func(t *testing.T, sqlStore *sqlcmig6.Queries,
273273
expRes *expectedResult) {
274274

275275
// Assert that the kv store migration results match the expected
@@ -388,7 +388,7 @@ func TestFirewallDBMigration(t *testing.T) {
388388

389389
// Perform the migration.
390390
err = txEx.ExecTx(ctx, sqldb.WriteTxOpt(),
391-
func(tx SQLQueries) error {
391+
func(tx *sqlcmig6.Queries) error {
392392
return MigrateFirewallDBToSQL(
393393
ctx, firewallStore.DB, tx,
394394
)
@@ -397,7 +397,7 @@ func TestFirewallDBMigration(t *testing.T) {
397397
require.NoError(t, err)
398398

399399
// Assert migration results.
400-
queries := sqlc.NewForType(
400+
queries := sqlcmig6.NewForType(
401401
sqlStore, sqlStore.BackendType,
402402
)
403403
assertMigrationResults(t, queries, entries)
@@ -795,7 +795,7 @@ func createPrivacyPairs(t *testing.T, ctx context.Context,
795795
sessSQLStore, ok := sessionStore.(*session.SQLStore)
796796
require.True(t, ok)
797797

798-
queries := sqlc.NewForType(sessSQLStore, sessSQLStore.BackendType)
798+
queries := sqlcmig6.NewForType(sessSQLStore, sessSQLStore.BackendType)
799799

800800
for i := range numSessions {
801801
sess, err := sessionStore.NewSession(
@@ -850,7 +850,7 @@ func randomPrivacyPairs(t *testing.T, ctx context.Context,
850850
sessSQLStore, ok := sessionStore.(*session.SQLStore)
851851
require.True(t, ok)
852852

853-
queries := sqlc.NewForType(sessSQLStore, sessSQLStore.BackendType)
853+
queries := sqlcmig6.NewForType(sessSQLStore, sessSQLStore.BackendType)
854854

855855
for i := range numSessions {
856856
sess, err := sessionStore.NewSession(

0 commit comments

Comments
 (0)