From 2d3d11487c34297a02aacbf4bbad83a6284e569f Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Tue, 19 Mar 2024 15:16:43 +0100 Subject: [PATCH] lnd: ensure that native SQL can only be used with a clean KV invoice DB This commit adds a check to ensure that we don't start LND with native SQL invoice DB if we already have any invoices in our KV channeldb. This is needed as native SQL invoices is an experimental feature and we do not yet support migration. --- config_builder.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/config_builder.go b/config_builder.go index 5195777e1..83974344e 100644 --- a/config_builder.go +++ b/config_builder.go @@ -1022,6 +1022,34 @@ func (d *DefaultDatabaseBuilder) BuildDatabase( // Instantiate a native SQL invoice store if the flag is set. if d.cfg.DB.UseNativeSQL { + // KV invoice db resides in the same database as the graph and + // channel state DB. Let's query the database to see if we have + // any invoices there. If we do, we won't allow the user to + // start lnd with native SQL enabled, as we don't currently + // migrate the invoices to the new database schema. + invoiceSlice, err := dbs.GraphDB.QueryInvoices( + ctx, invoices.InvoiceQuery{ + NumMaxInvoices: 1, + }, + ) + if err != nil { + cleanUp() + d.logger.Errorf("Unable to query KV invoice DB: %v", + err) + + return nil, nil, err + } + + if len(invoiceSlice.Invoices) > 0 { + cleanUp() + err := fmt.Errorf("found invoices in the KV invoice " + + "DB, migration to native SQL is not yet " + + "supported") + d.logger.Error(err) + + return nil, nil, err + } + executor := sqldb.NewTransactionExecutor( dbs.NativeSQLStore, func(tx *sql.Tx) sqldb.InvoiceQueries {