@@ -3,12 +3,18 @@ package invoices_test
3
3
import (
4
4
"context"
5
5
"database/sql"
6
+ "os"
7
+ "path"
6
8
"testing"
7
9
"time"
8
10
9
11
"github.com/lightningnetwork/lnd/channeldb"
10
12
"github.com/lightningnetwork/lnd/clock"
11
13
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"
12
18
"github.com/lightningnetwork/lnd/sqldb"
13
19
"github.com/lightningnetwork/lnd/sqldb/sqlc"
14
20
"github.com/stretchr/testify/require"
@@ -132,15 +138,66 @@ func TestMigrationWithChannelDB(t *testing.T) {
132
138
for _ , test := range tests {
133
139
test := test
134
140
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
+ }
136
184
137
185
t .Run ("Postgres" , func (t * testing.T ) {
138
- migrationTest (t , store , false )
186
+ migrationTest (t , kvStore , false )
139
187
})
140
188
141
189
t .Run ("SQLite" , func (t * testing.T ) {
142
- migrationTest (t , store , true )
190
+ migrationTest (t , kvStore , true )
143
191
})
144
192
})
145
193
}
146
194
}
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