2019-10-24 13:04:26 +03:00
|
|
|
package migration_01_to_11
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-errors/errors"
|
2019-12-13 05:22:19 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
2019-10-24 13:04:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// applyMigration is a helper test function that encapsulates the general steps
|
|
|
|
// which are needed to properly check the result of applying migration function.
|
|
|
|
func applyMigration(t *testing.T, beforeMigration, afterMigration func(d *DB),
|
|
|
|
migrationFunc migration, shouldFail bool) {
|
|
|
|
|
|
|
|
cdb, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a test node that will be our source node.
|
|
|
|
testNode, err := createTestVertex(cdb)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
graph := cdb.ChannelGraph()
|
|
|
|
if err := graph.SetSourceNode(testNode); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// beforeMigration usually used for populating the database
|
|
|
|
// with test data.
|
|
|
|
beforeMigration(cdb)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = errors.New(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil && shouldFail {
|
|
|
|
t.Fatal("error wasn't received on migration stage")
|
|
|
|
} else if err != nil && !shouldFail {
|
|
|
|
t.Fatalf("error was received on migration stage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// afterMigration usually used for checking the database state and
|
|
|
|
// throwing the error if something went wrong.
|
|
|
|
afterMigration(cdb)
|
|
|
|
}()
|
|
|
|
|
2019-10-31 13:25:04 +03:00
|
|
|
// Apply migration.
|
2019-12-13 05:22:19 +03:00
|
|
|
err = kvdb.Update(cdb, func(tx kvdb.RwTx) error {
|
2019-10-31 13:25:04 +03:00
|
|
|
return migrationFunc(tx)
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {})
|
2019-10-24 13:04:26 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|