From ea98933317a0b9b4470aca6494099e6975716d73 Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Fri, 10 Jan 2025 11:24:14 +0100 Subject: [PATCH] invoices: allow migration test to work on kv sqlite channeldb --- invoices/kv_sql_migration_test.go | 63 +++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/invoices/kv_sql_migration_test.go b/invoices/kv_sql_migration_test.go index 5d36b5c64..b3048a17b 100644 --- a/invoices/kv_sql_migration_test.go +++ b/invoices/kv_sql_migration_test.go @@ -3,12 +3,18 @@ package invoices_test import ( "context" "database/sql" + "os" + "path" "testing" "time" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/clock" invpkg "github.com/lightningnetwork/lnd/invoices" + "github.com/lightningnetwork/lnd/kvdb" + "github.com/lightningnetwork/lnd/kvdb/sqlbase" + "github.com/lightningnetwork/lnd/kvdb/sqlite" + "github.com/lightningnetwork/lnd/lncfg" "github.com/lightningnetwork/lnd/sqldb" "github.com/lightningnetwork/lnd/sqldb/sqlc" "github.com/stretchr/testify/require" @@ -132,15 +138,66 @@ func TestMigrationWithChannelDB(t *testing.T) { for _, test := range tests { test := test t.Run(test.name, func(t *testing.T) { - store := channeldb.OpenForTesting(t, test.dbPath) + var kvStore *channeldb.DB + + // First check if we have a channel.sqlite file in the + // testdata directory. If we do, we'll use that as the + // channel db for the migration test. + chanDBPath := path.Join( + test.dbPath, lncfg.SqliteChannelDBName, + ) + + // Just some sane defaults for the sqlite config. + const ( + timeout = 5 * time.Second + maxConns = 50 + ) + + sqliteConfig := &sqlite.Config{ + Timeout: timeout, + BusyTimeout: timeout, + MaxConnections: maxConns, + } + + if fileExists(chanDBPath) { + sqlbase.Init(maxConns) + + sqliteBackend, err := kvdb.Open( + kvdb.SqliteBackendName, + context.Background(), + sqliteConfig, test.dbPath, + lncfg.SqliteChannelDBName, + lncfg.NSChannelDB, + ) + + require.NoError(t, err) + kvStore, err = channeldb.CreateWithBackend( + sqliteBackend, + ) + + require.NoError(t, err) + } else { + kvStore = channeldb.OpenForTesting( + t, test.dbPath, + ) + } t.Run("Postgres", func(t *testing.T) { - migrationTest(t, store, false) + migrationTest(t, kvStore, false) }) t.Run("SQLite", func(t *testing.T) { - migrationTest(t, store, true) + migrationTest(t, kvStore, true) }) }) } } + +func fileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + + return !info.IsDir() +}