wtxmgr/migrations: add migration.Manager implementation
In this commit, we add an implementation of the recently introduced migration.Manager interface for the transaction manager. With this, we'll now be able to only expose the things required for the migration to happen, but have the actual migration logic live at a much higher level. There are no existing migrations for the transaction manager, but since the latest version was already defined as 1, we'll start from there to be backwards-compatible.
This commit is contained in:
parent
3bdfb6cc23
commit
ec1213aeab
1 changed files with 83 additions and 0 deletions
83
wtxmgr/migrations.go
Normal file
83
wtxmgr/migrations.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
package wtxmgr
|
||||
|
||||
import (
|
||||
"github.com/btcsuite/btcwallet/walletdb"
|
||||
"github.com/btcsuite/btcwallet/walletdb/migration"
|
||||
)
|
||||
|
||||
// versions is a list of the different database versions. The last entry should
|
||||
// reflect the latest database state. If the database happens to be at a version
|
||||
// number lower than the latest, migrations will be performed in order to catch
|
||||
// it up.
|
||||
var versions = []migration.Version{
|
||||
{
|
||||
Number: 1,
|
||||
Migration: nil,
|
||||
},
|
||||
}
|
||||
|
||||
// getLatestVersion returns the version number of the latest database version.
|
||||
func getLatestVersion() uint32 {
|
||||
return versions[len(versions)-1].Number
|
||||
}
|
||||
|
||||
// MigrationManager is an implementation of the migration.Manager interface that
|
||||
// will be used to handle migrations for the address manager. It exposes the
|
||||
// necessary parameters required to successfully perform migrations.
|
||||
type MigrationManager struct {
|
||||
ns walletdb.ReadWriteBucket
|
||||
}
|
||||
|
||||
// A compile-time assertion to ensure that MigrationManager implements the
|
||||
// migration.Manager interface.
|
||||
var _ migration.Manager = (*MigrationManager)(nil)
|
||||
|
||||
// NewMigrationManager creates a new migration manager for the transaction
|
||||
// manager. The given bucket should reflect the top-level bucket in which all
|
||||
// of the transaction manager's data is contained within.
|
||||
func NewMigrationManager(ns walletdb.ReadWriteBucket) *MigrationManager {
|
||||
return &MigrationManager{ns: ns}
|
||||
}
|
||||
|
||||
// Name returns the name of the service we'll be attempting to upgrade.
|
||||
//
|
||||
// NOTE: This method is part of the migration.Manager interface.
|
||||
func (m *MigrationManager) Name() string {
|
||||
return "wallet transaction manager"
|
||||
}
|
||||
|
||||
// Namespace returns the top-level bucket of the service.
|
||||
//
|
||||
// NOTE: This method is part of the migration.Manager interface.
|
||||
func (m *MigrationManager) Namespace() walletdb.ReadWriteBucket {
|
||||
return m.ns
|
||||
}
|
||||
|
||||
// CurrentVersion returns the current version of the service's database.
|
||||
//
|
||||
// NOTE: This method is part of the migration.Manager interface.
|
||||
func (m *MigrationManager) CurrentVersion(ns walletdb.ReadBucket) (uint32, error) {
|
||||
if ns == nil {
|
||||
ns = m.ns
|
||||
}
|
||||
return fetchVersion(m.ns)
|
||||
}
|
||||
|
||||
// SetVersion sets the version of the service's database.
|
||||
//
|
||||
// NOTE: This method is part of the migration.Manager interface.
|
||||
func (m *MigrationManager) SetVersion(ns walletdb.ReadWriteBucket,
|
||||
version uint32) error {
|
||||
|
||||
if ns == nil {
|
||||
ns = m.ns
|
||||
}
|
||||
return putVersion(m.ns, version)
|
||||
}
|
||||
|
||||
// Versions returns all of the available database versions of the service.
|
||||
//
|
||||
// NOTE: This method is part of the migration.Manager interface.
|
||||
func (m *MigrationManager) Versions() []migration.Version {
|
||||
return versions
|
||||
}
|
Loading…
Reference in a new issue