@@ -70,6 +70,10 @@ const (
7070 // invoiceMigration is the version of the migration that will be used to
7171 // migrate invoices from the kvdb to the sql database.
7272 invoiceMigration = 7
73+
74+ // graphMigration is the version number for the graph migration
75+ // that migrates the KV graph to the native SQL schema.
76+ graphMigration = 10
7377)
7478
7579// GrpcRegistrar is an interface that must be satisfied by an external subserver
@@ -1091,6 +1095,11 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
10911095 if d .cfg .DB .UseNativeSQL {
10921096 migrations := sqldb .GetMigrations ()
10931097
1098+ queryCfg := & d .cfg .DB .Sqlite .QueryConfig
1099+ if d .cfg .DB .Backend == lncfg .PostgresBackend {
1100+ queryCfg = & d .cfg .DB .Postgres .QueryConfig
1101+ }
1102+
10941103 // If the user has not explicitly disabled the SQL invoice
10951104 // migration, attach the custom migration function to invoice
10961105 // migration (version 7). Even if this custom migration is
@@ -1115,17 +1124,42 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
11151124 d .logger .Debugf ("Setting invoice bucket " +
11161125 "tombstone" )
11171126
1118- return dbs .ChanStateDB .SetInvoiceBucketTombstone () //nolint:ll
1127+ //nolint:ll
1128+ return dbs .ChanStateDB .SetInvoiceBucketTombstone ()
1129+ }
1130+
1131+ graphMig := func (tx * sqlc.Queries ) error {
1132+ cfg := & graphdb.SQLStoreConfig {
1133+ //nolint:ll
1134+ ChainHash : * d .cfg .ActiveNetParams .GenesisHash ,
1135+ QueryCfg : queryCfg ,
1136+ }
1137+ err := graphdb .MigrateGraphToSQL (
1138+ ctx , cfg , dbs .ChanStateDB .Backend , tx ,
1139+ )
1140+ if err != nil {
1141+ return fmt .Errorf ("failed to migrate " +
1142+ "graph to SQL: %w" , err )
1143+ }
1144+
1145+ return nil
11191146 }
11201147
11211148 // Make sure we attach the custom migration function to
11221149 // the correct migration version.
11231150 for i := 0 ; i < len (migrations ); i ++ {
11241151 version := migrations [i ].Version
1125- if version == invoiceMigration {
1152+ switch version {
1153+ case invoiceMigration :
11261154 migrations [i ].MigrationFn = invoiceMig
11271155
11281156 continue
1157+ case graphMigration :
1158+ migrations [i ].MigrationFn = graphMig
1159+
1160+ continue
1161+
1162+ default :
11291163 }
11301164
11311165 migFn , ok := d .getSQLMigration (
@@ -1167,8 +1201,18 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
11671201
11681202 dbs .InvoiceDB = sqlInvoiceDB
11691203
1170- graphStore , err = d .getGraphStore (
1171- baseDB , databaseBackends .GraphDB , graphDBOptions ... ,
1204+ graphExecutor := sqldb .NewTransactionExecutor (
1205+ baseDB , func (tx * sql.Tx ) graphdb.SQLQueries {
1206+ return baseDB .WithTx (tx )
1207+ },
1208+ )
1209+
1210+ graphStore , err = graphdb .NewSQLStore (
1211+ & graphdb.SQLStoreConfig {
1212+ ChainHash : * d .cfg .ActiveNetParams .GenesisHash ,
1213+ QueryCfg : queryCfg ,
1214+ },
1215+ graphExecutor , graphDBOptions ... ,
11721216 )
11731217 if err != nil {
11741218 err = fmt .Errorf ("unable to get graph store: %w" , err )
0 commit comments