channeldb/db: adds optional dry-run abort for migrations

This commit is contained in:
Conner Fromknecht committed 2020-05-11 15:38:45 -07:00
1 parent f3a427724b
commit c775819372
4 files changed
+87 -4

No files matched your search

+45 -3
View File
@@ -12,13 +12,14 @@ import (
// 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) {
migrationFunc migration, shouldFail bool, dryRun bool) {
cdb, cleanUp, err := makeTestDB()
defer cleanUp()
if err != nil {
t.Fatal(err)
}
cdb.dryRun = dryRun
// Create a test node that will be our source node.
testNode, err := createTestVertex(cdb)
@@ -54,6 +55,9 @@ func applyMigration(t *testing.T, beforeMigration, afterMigration func(d *DB),
defer func() {
if r := recover(); r != nil {
if dryRun && r != ErrDryRunMigrationOK {
t.Fatalf("expected dry run migration OK")
}
err = errors.New(r)
}
@@ -256,7 +260,8 @@ func TestMigrationWithPanic(t *testing.T) {
beforeMigrationFunc,
afterMigrationFunc,
migrationWithPanic,
true)
true,
false)
}
// TestMigrationWithFatal asserts that migrations which fail do not modify the
@@ -330,7 +335,8 @@ func TestMigrationWithFatal(t *testing.T) {
beforeMigrationFunc,
afterMigrationFunc,
migrationWithFatal,
true)
true,
false)
}
// TestMigrationWithoutErrors asserts that a successful migration has its
@@ -404,6 +410,7 @@ func TestMigrationWithoutErrors(t *testing.T) {
beforeMigrationFunc,
afterMigrationFunc,
migrationWithoutErrors,
false,
false)
}
@@ -446,3 +453,38 @@ func TestMigrationReversion(t *testing.T) {
"want: %v, got: %v", ErrDBReversion, err)
}
}
// TestMigrationDryRun ensures that opening the database in dry run migration
// mode will fail and not commit the migration.
func TestMigrationDryRun(t *testing.T) {
t.Parallel()
// Nothing to do, will inspect version number.
beforeMigrationFunc := func(d *DB) {}
// Check that version of database version is not modified.
afterMigrationFunc := func(d *DB) {
err := kvdb.View(d, func(tx kvdb.ReadTx) error {
meta, err := d.FetchMeta(nil)
if err != nil {
t.Fatal(err)
}
if meta.DbVersionNumber != 0 {
t.Fatal("dry run migration was not aborted")
}
return nil
})
if err != nil {
t.Fatalf("unable to apply after func: %v", err)
}
}
applyMigration(t,
beforeMigrationFunc,
afterMigrationFunc,
func(kvdb.RwTx) error { return nil },
true,
true)
}