2019-03-07 02:19:57 +00:00
|
|
|
package loopdb
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/coreos/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// metaBucket stores all the meta information concerning the state of
|
|
|
|
// the database.
|
2019-05-15 11:59:05 +00:00
|
|
|
metaBucketKey = []byte("metadata")
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
// dbVersionKey is a boltdb key and it's used for storing/retrieving
|
|
|
|
// current database version.
|
|
|
|
dbVersionKey = []byte("dbp")
|
|
|
|
|
|
|
|
// ErrDBReversion is returned when detecting an attempt to revert to a
|
|
|
|
// prior database version.
|
|
|
|
ErrDBReversion = fmt.Errorf("channel db cannot revert to prior version")
|
|
|
|
)
|
|
|
|
|
|
|
|
// migration is a function which takes a prior outdated version of the database
|
|
|
|
// instances and mutates the key/bucket structure to arrive at a more
|
|
|
|
// up-to-date version of the database.
|
|
|
|
type migration func(tx *bbolt.Tx) error
|
|
|
|
|
|
|
|
var (
|
|
|
|
// dbVersions is storing all versions of database. If current version
|
|
|
|
// of database don't match with latest version this list will be used
|
|
|
|
// for retrieving all migration function that are need to apply to the
|
|
|
|
// current db.
|
2019-05-15 12:01:27 +00:00
|
|
|
migrations = []migration{migrateCosts}
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
latestDBVersion = uint32(len(migrations))
|
|
|
|
)
|
|
|
|
|
|
|
|
// getDBVersion retrieves the current db version.
|
|
|
|
func getDBVersion(db *bbolt.DB) (uint32, error) {
|
|
|
|
var version uint32
|
|
|
|
|
|
|
|
err := db.View(func(tx *bbolt.Tx) error {
|
2019-05-15 11:59:05 +00:00
|
|
|
metaBucket := tx.Bucket(metaBucketKey)
|
2019-03-06 20:13:50 +00:00
|
|
|
if metaBucket == nil {
|
|
|
|
return errors.New("bucket does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
data := metaBucket.Get(dbVersionKey)
|
|
|
|
// If no version key found, assume version is 0.
|
|
|
|
if data != nil {
|
|
|
|
version = byteOrder.Uint32(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return version, nil
|
|
|
|
}
|
|
|
|
|
2019-05-15 11:59:05 +00:00
|
|
|
// setDBVersion updates the current db version.
|
2019-03-06 20:13:50 +00:00
|
|
|
func setDBVersion(tx *bbolt.Tx, version uint32) error {
|
2019-05-15 11:59:05 +00:00
|
|
|
metaBucket, err := tx.CreateBucketIfNotExists(metaBucketKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("set db version: %v", err)
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scratch := make([]byte, 4)
|
|
|
|
byteOrder.PutUint32(scratch, version)
|
|
|
|
return metaBucket.Put(dbVersionKey, scratch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// syncVersions function is used for safe db version synchronization. It
|
|
|
|
// applies migration functions to the current database and recovers the
|
|
|
|
// previous state of db if at least one error/panic appeared during migration.
|
|
|
|
func syncVersions(db *bbolt.DB) error {
|
|
|
|
currentVersion, err := getDBVersion(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:19:57 +00:00
|
|
|
log.Infof("Checking for schema update: latest_version=%v, "+
|
2019-03-06 20:13:50 +00:00
|
|
|
"db_version=%v", latestDBVersion, currentVersion)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
|
|
|
// If the database reports a higher version that we are aware of, the
|
|
|
|
// user is probably trying to revert to a prior version of lnd. We fail
|
|
|
|
// here to prevent reversions and unintended corruption.
|
|
|
|
case currentVersion > latestDBVersion:
|
2019-03-07 02:19:57 +00:00
|
|
|
log.Errorf("Refusing to revert from db_version=%d to "+
|
2019-03-06 20:13:50 +00:00
|
|
|
"lower version=%d", currentVersion,
|
|
|
|
latestDBVersion)
|
|
|
|
|
|
|
|
return ErrDBReversion
|
|
|
|
|
|
|
|
// If the current database version matches the latest version number,
|
|
|
|
// then we don't need to perform any migrations.
|
|
|
|
case currentVersion == latestDBVersion:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:19:57 +00:00
|
|
|
log.Infof("Performing database schema migration")
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-07 02:19:57 +00:00
|
|
|
// Otherwise we execute the migrations serially within a single
|
|
|
|
// database transaction to ensure the migration is atomic.
|
2019-03-06 20:13:50 +00:00
|
|
|
return db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
for v := currentVersion; v < latestDBVersion; v++ {
|
2019-03-07 02:19:57 +00:00
|
|
|
log.Infof("Applying migration #%v", v+1)
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
migration := migrations[v]
|
|
|
|
if err := migration(tx); err != nil {
|
2019-03-07 02:19:57 +00:00
|
|
|
log.Infof("Unable to apply migration #%v",
|
2019-03-06 20:13:50 +00:00
|
|
|
v+1)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return setDBVersion(tx, latestDBVersion)
|
|
|
|
})
|
|
|
|
}
|