|
| 1 | +//go:build dev |
| 2 | + |
| 3 | +package migrationstreams |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "database/sql" |
| 8 | + "fmt" |
| 9 | + "path/filepath" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/golang-migrate/migrate/v4" |
| 13 | + "github.com/golang-migrate/migrate/v4/database" |
| 14 | + "github.com/lightninglabs/lightning-terminal/accounts" |
| 15 | + "github.com/lightninglabs/lightning-terminal/db/sqlcmig6" |
| 16 | + "github.com/lightninglabs/lightning-terminal/firewalldb" |
| 17 | + "github.com/lightninglabs/lightning-terminal/session" |
| 18 | + "github.com/lightningnetwork/lnd/clock" |
| 19 | + "github.com/lightningnetwork/lnd/sqldb/v2" |
| 20 | +) |
| 21 | + |
| 22 | +// MakePostStepCallbacksMig6 turns the post migration checks into a map of post |
| 23 | +// step callbacks that can be used with the migrate package. The keys of the map |
| 24 | +// are the migration versions, and the values are the callbacks that will be |
| 25 | +// executed after the migration with the corresponding version is applied. |
| 26 | +func MakePostStepCallbacksMig6(ctx context.Context, db *sqldb.BaseDB, |
| 27 | + macPath string, clock clock.Clock, |
| 28 | + migVersion uint) migrate.PostStepCallback { |
| 29 | + |
| 30 | + mig6queries := sqlcmig6.NewForType(db, db.BackendType) |
| 31 | + mig6executor := sqldb.NewTransactionExecutor( |
| 32 | + db, func(tx *sql.Tx) *sqlcmig6.Queries { |
| 33 | + return mig6queries.WithTx(tx) |
| 34 | + }, |
| 35 | + ) |
| 36 | + |
| 37 | + return func(_ *migrate.Migration, _ database.Driver) error { |
| 38 | + // We ignore the actual driver that's being returned here, since |
| 39 | + // we use migrate.NewWithInstance() to create the migration |
| 40 | + // instance from our already instantiated database backend that |
| 41 | + // is also passed into this function. |
| 42 | + return mig6executor.ExecTx( |
| 43 | + ctx, sqldb.NewWriteTx(), |
| 44 | + func(q6 *sqlcmig6.Queries) error { |
| 45 | + log.Infof("Running post migration callback "+ |
| 46 | + "for migration version %d", migVersion) |
| 47 | + |
| 48 | + return kvdbToSqlMigrationCallback( |
| 49 | + ctx, macPath, db, clock, q6, |
| 50 | + ) |
| 51 | + }, sqldb.NoOpReset, |
| 52 | + ) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func kvdbToSqlMigrationCallback(ctx context.Context, macPath string, |
| 57 | + _ *sqldb.BaseDB, clock clock.Clock, q *sqlcmig6.Queries) error { |
| 58 | + |
| 59 | + start := time.Now() |
| 60 | + log.Infof("Starting KVDB to SQL migration for all stores") |
| 61 | + |
| 62 | + accountStore, err := accounts.NewBoltStore( |
| 63 | + filepath.Dir(macPath), accounts.DBFilename, clock, |
| 64 | + ) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + err = accounts.MigrateAccountStoreToSQL(ctx, accountStore.DB, q) |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("error migrating account store to "+ |
| 72 | + "SQL: %v", err) |
| 73 | + } |
| 74 | + |
| 75 | + sessionStore, err := session.NewDB( |
| 76 | + filepath.Dir(macPath), session.DBFilename, |
| 77 | + clock, accountStore, |
| 78 | + ) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + err = session.MigrateSessionStoreToSQL(ctx, sessionStore.DB, q) |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("error migrating session store to "+ |
| 86 | + "SQL: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + firewallStore, err := firewalldb.NewBoltDB( |
| 90 | + filepath.Dir(macPath), firewalldb.DBFilename, |
| 91 | + sessionStore, accountStore, clock, |
| 92 | + ) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + err = firewalldb.MigrateFirewallDBToSQL(ctx, firewallStore.DB, q) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("error migrating firewalldb store "+ |
| 100 | + "to SQL: %v", err) |
| 101 | + } |
| 102 | + |
| 103 | + log.Infof("Succesfully migrated all KVDB stores to SQL in: %v", |
| 104 | + time.Since(start)) |
| 105 | + |
| 106 | + return nil |
| 107 | +} |
0 commit comments