channeldb: export DB migration related functions
We'll need to know whether a database was migrated to the latest version in our upcoming data migration tool. To be able to determine the current version of a DB and the total number of migrations in existence, we need to export some of the functions in channeldb. We also add some helper functions for adding tombstone and other markers to a database.
This commit is contained in:
3 files changed
+189
-7
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwallet/walletdb"
|
||||
"github.com/go-errors/errors"
|
||||
"github.com/lightningnetwork/lnd/kvdb"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -574,3 +575,110 @@ func TestApplyOptionalVersions(t *testing.T) {
|
||||
require.NoError(t, err, "failed to apply optional migration")
|
||||
require.Equal(t, 1, migrateCount, "expected no migration")
|
||||
}
|
||||
|
||||
// TestFetchMeta tests that the FetchMeta returns the latest DB version for a
|
||||
// freshly created DB instance.
|
||||
func TestFetchMeta(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db, cleanUp, err := MakeTestDB()
|
||||
defer cleanUp()
|
||||
require.NoError(t, err)
|
||||
|
||||
meta := &Meta{}
|
||||
err = db.View(func(tx walletdb.ReadTx) error {
|
||||
return FetchMeta(meta, tx)
|
||||
}, func() {
|
||||
meta = &Meta{}
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, LatestDBVersion(), meta.DbVersionNumber)
|
||||
}
|
||||
|
||||
// TestMarkerAndTombstone tests that markers like a tombstone can be added to a
|
||||
// DB.
|
||||
func TestMarkerAndTombstone(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db, cleanUp, err := MakeTestDB()
|
||||
defer cleanUp()
|
||||
require.NoError(t, err)
|
||||
|
||||
// Test that a generic marker is not present in a fresh DB.
|
||||
var marker []byte
|
||||
err = db.View(func(tx walletdb.ReadTx) error {
|
||||
var err error
|
||||
marker, err = CheckMarkerPresent(tx, []byte("foo"))
|
||||
return err
|
||||
}, func() {
|
||||
marker = nil
|
||||
})
|
||||
require.ErrorIs(t, err, ErrMarkerNotPresent)
|
||||
require.Nil(t, marker)
|
||||
|
||||
// Only adding the marker bucket should not be enough to be counted as
|
||||
// a marker, we explicitly also want the value to be set.
|
||||
err = db.Update(func(tx walletdb.ReadWriteTx) error {
|
||||
_, err := tx.CreateTopLevelBucket([]byte("foo"))
|
||||
return err
|
||||
}, func() {})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = db.View(func(tx walletdb.ReadTx) error {
|
||||
var err error
|
||||
marker, err = CheckMarkerPresent(tx, []byte("foo"))
|
||||
return err
|
||||
}, func() {
|
||||
marker = nil
|
||||
})
|
||||
require.ErrorIs(t, err, ErrMarkerNotPresent)
|
||||
require.Nil(t, marker)
|
||||
|
||||
// Test that a tombstone marker is not present in a fresh DB.
|
||||
err = db.View(EnsureNoTombstone, func() {})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Add a generic marker now and assert that it can be read.
|
||||
err = db.Update(func(tx walletdb.ReadWriteTx) error {
|
||||
return AddMarker(tx, []byte("foo"), []byte("bar"))
|
||||
}, func() {})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = db.View(func(tx walletdb.ReadTx) error {
|
||||
var err error
|
||||
marker, err = CheckMarkerPresent(tx, []byte("foo"))
|
||||
return err
|
||||
}, func() {
|
||||
marker = nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []byte("bar"), marker)
|
||||
|
||||
// A tombstone should still not be present.
|
||||
err = db.View(EnsureNoTombstone, func() {})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Finally, add a tombstone.
|
||||
tombstoneText := []byte("RIP test DB")
|
||||
err = db.Update(func(tx walletdb.ReadWriteTx) error {
|
||||
return AddMarker(tx, TombstoneKey, tombstoneText)
|
||||
}, func() {})
|
||||
require.NoError(t, err)
|
||||
|
||||
// We can read it as a normal marker.
|
||||
err = db.View(func(tx walletdb.ReadTx) error {
|
||||
var err error
|
||||
marker, err = CheckMarkerPresent(tx, TombstoneKey)
|
||||
return err
|
||||
}, func() {
|
||||
marker = nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tombstoneText, marker)
|
||||
|
||||
// But also as a tombstone, and now we should get an error that the DB
|
||||
// cannot be used anymore.
|
||||
err = db.View(EnsureNoTombstone, func() {})
|
||||
require.ErrorContains(t, err, string(tombstoneText))
|
||||
}
|
||||
Reference in new issue
Block a user