@@ -93,18 +93,29 @@ func MigrateFirewallDBToSQL(ctx context.Context, kvStore *bbolt.DB,
9393
9494 log .Infof ("Starting migration of the rules DB to SQL" )
9595
96- err := migrateKVStoresDBToSQL (ctx , kvStore , sqlTx )
96+ sessions , err := sessionDB .ListSessions (ctx )
97+ if err != nil {
98+ return fmt .Errorf ("listing sessions failed: %w" , err )
99+ }
100+
101+ sessionMap , err := mapSessions (sessions )
102+ if err != nil {
103+ return fmt .Errorf ("mapping sessions failed: %w" , err )
104+ }
105+
106+ err = migrateKVStoresDBToSQL (ctx , kvStore , sqlTx , sessionMap )
97107 if err != nil {
98108 return err
99109 }
100110
101- err = migratePrivacyMapperDBToSQL (ctx , kvStore , sqlTx )
111+ err = migratePrivacyMapperDBToSQL (ctx , kvStore , sqlTx , sessionMap )
102112 if err != nil {
103113 return err
104114 }
105115
106116 err = migrateActionsToSQL (
107117 ctx , kvStore , sqlTx , sessionDB , accountDB , macRootKeyIDs ,
118+ sessionMap ,
108119 )
109120 if err != nil {
110121 return err
@@ -119,7 +130,7 @@ func MigrateFirewallDBToSQL(ctx context.Context, kvStore *bbolt.DB,
119130// database to the SQL database. The function also asserts that the
120131// migrated values match the original values in the KV store.
121132func migrateKVStoresDBToSQL (ctx context.Context , kvStore * bbolt.DB ,
122- sqlTx SQLQueries ) error {
133+ sqlTx SQLQueries , sessMap map [[ 4 ] byte ]sqlc. Session ) error {
123134
124135 log .Infof ("Starting migration of the KV stores to SQL" )
125136
@@ -139,7 +150,7 @@ func migrateKVStoresDBToSQL(ctx context.Context, kvStore *bbolt.DB,
139150
140151 // 2) Insert all collected key-value pairs into the SQL database.
141152 for _ , entry := range pairs {
142- insertedPair , err := insertPair (ctx , sqlTx , entry )
153+ insertedPair , err := insertPair (ctx , sqlTx , sessMap , entry )
143154 if err != nil {
144155 return fmt .Errorf ("inserting kv pair %v failed: %w" ,
145156 entry .key , err )
@@ -382,7 +393,7 @@ func collectKVPairs(bkt *bbolt.Bucket, errorOnBuckets, perm bool,
382393
383394// insertPair inserts a single key-value pair into the SQL database.
384395func insertPair (ctx context.Context , tx SQLQueries ,
385- entry * kvEntry ) (* sqlKvEntry , error ) {
396+ sessMap map [[ 4 ] byte ]sqlc. Session , entry * kvEntry ) (* sqlKvEntry , error ) {
386397
387398 ruleID , err := tx .GetOrInsertRuleID (ctx , entry .ruleName )
388399 if err != nil {
@@ -397,15 +408,16 @@ func insertPair(ctx context.Context, tx SQLQueries,
397408 }
398409
399410 entry .groupAlias .WhenSome (func (alias []byte ) {
400- var groupID int64
401- groupID , err = tx .GetSessionIDByAlias (ctx , alias )
402- if err != nil {
403- err = fmt .Errorf ("getting group id by alias %x " +
404- "failed: %w" , alias , err )
405- return
411+ var groupAlias [4 ]byte
412+ copy (groupAlias [:], alias )
413+
414+ sess , ok := sessMap [groupAlias ]
415+ if ! ok {
416+ err = fmt .Errorf ("session group %x not found in map" ,
417+ alias )
406418 }
407419
408- p .GroupID = sqldb . SQLInt64 ( groupID )
420+ p .GroupID = sess . GroupID
409421 })
410422 if err != nil {
411423 return nil , err
@@ -515,12 +527,12 @@ func verifyBktKeys(bkt *bbolt.Bucket, errorOnKeyValues bool,
515527// from the KV database to the SQL database. The function also asserts that the
516528// migrated values match the original values in the privacy mapper store.
517529func migratePrivacyMapperDBToSQL (ctx context.Context , kvStore * bbolt.DB ,
518- sqlTx SQLQueries ) error {
530+ sqlTx SQLQueries , sessMap map [[ 4 ] byte ]sqlc. Session ) error {
519531
520532 log .Infof ("Starting migration of the privacy mapper store to SQL" )
521533
522534 // 1) Collect all privacy pairs from the KV store.
523- privPairs , err := collectPrivacyPairs (ctx , kvStore , sqlTx )
535+ privPairs , err := collectPrivacyPairs (ctx , kvStore , sessMap )
524536 if err != nil {
525537 return fmt .Errorf ("error migrating privacy mapper store: %w" ,
526538 err )
@@ -550,7 +562,7 @@ func migratePrivacyMapperDBToSQL(ctx context.Context, kvStore *bbolt.DB,
550562
551563// collectPrivacyPairs collects all privacy pairs from the KV store.
552564func collectPrivacyPairs (ctx context.Context , kvStore * bbolt.DB ,
553- sqlTx SQLQueries ) (privacyPairs , error ) {
565+ sessMap map [[ 4 ] byte ]sqlc. Session ) (privacyPairs , error ) {
554566
555567 groupPairs := make (privacyPairs )
556568
@@ -576,24 +588,28 @@ func collectPrivacyPairs(ctx context.Context, kvStore *bbolt.DB,
576588 "%s not found" , groupId )
577589 }
578590
579- groupSqlId , err := sqlTx .GetSessionIDByAlias (
580- ctx , groupId ,
581- )
582- if errors .Is (err , sql .ErrNoRows ) {
591+ var groupAlias [4 ]byte
592+ copy (groupAlias [:], groupId )
593+
594+ sess , ok := sessMap [groupAlias ]
595+ if ! ok {
583596 return fmt .Errorf ("session with group id %x " +
584597 "not found in sql db" , groupId )
585- } else if err != nil {
586- return err
598+ }
599+
600+ if ! sess .GroupID .Valid {
601+ return fmt .Errorf ("session group id for " +
602+ "session %d is not set " , sess .ID )
587603 }
588604
589605 groupRealToPseudoPairs , err := collectGroupPairs (gBkt )
590606 if err != nil {
591607 return fmt .Errorf ("processing group bkt " +
592608 "for group id %s (sqlID %d) failed: %w" ,
593- groupId , groupSqlId , err )
609+ groupId , sess . GroupID . Int64 , err )
594610 }
595611
596- groupPairs [groupSqlId ] = groupRealToPseudoPairs
612+ groupPairs [sess . GroupID . Int64 ] = groupRealToPseudoPairs
597613
598614 return nil
599615 })
@@ -794,7 +810,8 @@ func validateGroupPairsMigration(ctx context.Context, sqlTx SQLQueries,
794810// values match the original values in the actions store.
795811func migrateActionsToSQL (ctx context.Context , kvStore * bbolt.DB ,
796812 sqlTx SQLQueries , sessionDB session.SQLQueries ,
797- accountsDB accounts.SQLQueries , macRootKeyIDs [][]byte ) error {
813+ accountsDB accounts.SQLQueries , macRootKeyIDs [][]byte ,
814+ sessMap map [[4 ]byte ]sqlc.Session ) error {
798815
799816 log .Infof ("Starting migration of the actions store to SQL" )
800817
@@ -811,16 +828,6 @@ func migrateActionsToSQL(ctx context.Context, kvStore *bbolt.DB,
811828 return fmt .Errorf ("mapping accounts failed: %w" , err )
812829 }
813830
814- sessions , err := sessionDB .ListSessions (ctx )
815- if err != nil {
816- return fmt .Errorf ("listing sessions failed: %w" , err )
817- }
818-
819- sessionMap , err := mapSessions (sessions )
820- if err != nil {
821- return fmt .Errorf ("mapping sessions failed: %w" , err )
822- }
823-
824831 // Next, as the kvdb actions only have their last 4 bytes set for the
825832 // MacaroonRootKeyID field, we'll do a best effort attempt fetch the
826833 // full root key ID (all 8 bytes) from lnd when migrating each action.
@@ -927,8 +934,7 @@ func migrateActionsToSQL(ctx context.Context, kvStore *bbolt.DB,
927934 // migrated.
928935 err = migrateActionToSQL (
929936 ctx , sqlTx , sessionDB , accountsDB ,
930- acctsMap , sessionMap , action ,
931- macRootKeyID ,
937+ acctsMap , sessMap , action , macRootKeyID ,
932938 )
933939 if err != nil {
934940 return fmt .Errorf ("migrating action " +
0 commit comments