Skip to content

Commit ea98933

Browse files
committed
invoices: allow migration test to work on kv sqlite channeldb
1 parent 5e3ef3e commit ea98933

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

invoices/kv_sql_migration_test.go

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ package invoices_test
33
import (
44
"context"
55
"database/sql"
6+
"os"
7+
"path"
68
"testing"
79
"time"
810

911
"github.com/lightningnetwork/lnd/channeldb"
1012
"github.com/lightningnetwork/lnd/clock"
1113
invpkg "github.com/lightningnetwork/lnd/invoices"
14+
"github.com/lightningnetwork/lnd/kvdb"
15+
"github.com/lightningnetwork/lnd/kvdb/sqlbase"
16+
"github.com/lightningnetwork/lnd/kvdb/sqlite"
17+
"github.com/lightningnetwork/lnd/lncfg"
1218
"github.com/lightningnetwork/lnd/sqldb"
1319
"github.com/lightningnetwork/lnd/sqldb/sqlc"
1420
"github.com/stretchr/testify/require"
@@ -132,15 +138,66 @@ func TestMigrationWithChannelDB(t *testing.T) {
132138
for _, test := range tests {
133139
test := test
134140
t.Run(test.name, func(t *testing.T) {
135-
store := channeldb.OpenForTesting(t, test.dbPath)
141+
var kvStore *channeldb.DB
142+
143+
// First check if we have a channel.sqlite file in the
144+
// testdata directory. If we do, we'll use that as the
145+
// channel db for the migration test.
146+
chanDBPath := path.Join(
147+
test.dbPath, lncfg.SqliteChannelDBName,
148+
)
149+
150+
// Just some sane defaults for the sqlite config.
151+
const (
152+
timeout = 5 * time.Second
153+
maxConns = 50
154+
)
155+
156+
sqliteConfig := &sqlite.Config{
157+
Timeout: timeout,
158+
BusyTimeout: timeout,
159+
MaxConnections: maxConns,
160+
}
161+
162+
if fileExists(chanDBPath) {
163+
sqlbase.Init(maxConns)
164+
165+
sqliteBackend, err := kvdb.Open(
166+
kvdb.SqliteBackendName,
167+
context.Background(),
168+
sqliteConfig, test.dbPath,
169+
lncfg.SqliteChannelDBName,
170+
lncfg.NSChannelDB,
171+
)
172+
173+
require.NoError(t, err)
174+
kvStore, err = channeldb.CreateWithBackend(
175+
sqliteBackend,
176+
)
177+
178+
require.NoError(t, err)
179+
} else {
180+
kvStore = channeldb.OpenForTesting(
181+
t, test.dbPath,
182+
)
183+
}
136184

137185
t.Run("Postgres", func(t *testing.T) {
138-
migrationTest(t, store, false)
186+
migrationTest(t, kvStore, false)
139187
})
140188

141189
t.Run("SQLite", func(t *testing.T) {
142-
migrationTest(t, store, true)
190+
migrationTest(t, kvStore, true)
143191
})
144192
})
145193
}
146194
}
195+
196+
func fileExists(filename string) bool {
197+
info, err := os.Stat(filename)
198+
if os.IsNotExist(err) {
199+
return false
200+
}
201+
202+
return !info.IsDir()
203+
}

0 commit comments

Comments
 (0)