Skip to content

Commit a2abcd0

Browse files
committed
loopd: Run migration if boltdb exists
1 parent 69b4df0 commit a2abcd0

File tree

2 files changed

+108
-4
lines changed

2 files changed

+108
-4
lines changed

loopd/daemon.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,19 +370,31 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
370370
}
371371
}
372372

373+
// Both the client RPC server and and the swap server client should
374+
// stop on main context cancel. So we create it early and pass it down.
375+
d.mainCtx, d.mainCtxCancel = context.WithCancel(context.Background())
376+
373377
log.Infof("Swap server address: %v", d.cfg.Server.Host)
374378

379+
// Check if we need to migrate the database.
380+
if needSqlMigration(d.cfg) {
381+
log.Infof("Boltdb found, running migration")
382+
383+
err := migrateBoltdb(d.mainCtx, d.cfg)
384+
if err != nil {
385+
return fmt.Errorf("unable to migrate boltdb: %v", err)
386+
}
387+
388+
log.Infof("Successfully migrated boltdb")
389+
}
390+
375391
// Create an instance of the loop client library.
376392
swapclient, clientCleanup, err := getClient(d.cfg, &d.lnd.LndServices)
377393
if err != nil {
378394
return err
379395
}
380396
d.clientCleanup = clientCleanup
381397

382-
// Both the client RPC server and and the swap server client should
383-
// stop on main context cancel. So we create it early and pass it down.
384-
d.mainCtx, d.mainCtxCancel = context.WithCancel(context.Background())
385-
386398
// Add our debug permissions to our main set of required permissions
387399
// if compiled in.
388400
for endpoint, perm := range debugRequiredPermissions {

loopd/migration.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package loopd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/lightninglabs/lndclient"
10+
"github.com/lightninglabs/loop/loopdb"
11+
"github.com/lightningnetwork/lnd/lnrpc"
12+
)
13+
14+
// migrateBoltdb migrates the boltdb to sqlite.
15+
func migrateBoltdb(ctx context.Context, cfg *Config) error {
16+
// First get the chain params.
17+
chainParams, err := lndclient.Network(cfg.Network).ChainParams()
18+
if err != nil {
19+
return err
20+
}
21+
22+
// First open the bolt db.
23+
boltdb, err := loopdb.NewBoltSwapStore(cfg.DataDir, chainParams)
24+
if err != nil {
25+
return err
26+
}
27+
defer boltdb.Close()
28+
29+
var db loopdb.SwapStore
30+
switch cfg.DatabaseBackend {
31+
case DatabaseBackendSqlite:
32+
log.Infof("Opening sqlite3 database at: %v",
33+
cfg.Sqlite.DatabaseFileName)
34+
db, err = loopdb.NewSqliteStore(
35+
cfg.Sqlite, chainParams,
36+
)
37+
38+
case DatabaseBackendPostgres:
39+
log.Infof("Opening postgres database at: %v",
40+
cfg.Postgres.DSN(true))
41+
db, err = loopdb.NewPostgresStore(
42+
cfg.Postgres, chainParams,
43+
)
44+
45+
default:
46+
return fmt.Errorf("unknown database backend: %s",
47+
cfg.DatabaseBackend)
48+
}
49+
if err != nil {
50+
return fmt.Errorf("unable to open database: %v", err)
51+
}
52+
53+
defer db.Close()
54+
55+
// Create a new migrator manager.
56+
migrator := loopdb.NewMigratorManager(boltdb, db)
57+
58+
// Run the migration.
59+
err = migrator.RunMigrations(ctx)
60+
if err != nil {
61+
return err
62+
}
63+
64+
// If the migration was successfull we'll rename the bolt db to
65+
// loop.db.bk.
66+
err = os.Rename(
67+
filepath.Join(cfg.DataDir, "loop.db"),
68+
filepath.Join(cfg.DataDir, "loop.db.bk"),
69+
)
70+
if err != nil {
71+
return err
72+
}
73+
74+
return nil
75+
}
76+
77+
// needSqlMigration checks if the boltdb exists at it's default location
78+
// and returns true if it does.
79+
func needSqlMigration(cfg *Config) bool {
80+
// First check if the data directory exists.
81+
if !lnrpc.FileExists(cfg.DataDir) {
82+
return false
83+
}
84+
85+
// Now we'll check if the bolt db exists.
86+
if !lnrpc.FileExists(filepath.Join(cfg.DataDir, "loop.db")) {
87+
return false
88+
}
89+
90+
// If both the folder and the bolt db exist, we'll return true.
91+
return true
92+
}

0 commit comments

Comments
 (0)