|
| 1 | +package firewalldb |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "go.etcd.io/bbolt" |
| 8 | +) |
| 9 | + |
| 10 | +// migration is a function which takes a prior outdated version of the database |
| 11 | +// instance and mutates the key/bucket structure to arrive at a more up-to-date |
| 12 | +// version of the database. |
| 13 | +type migration func(tx *bbolt.Tx) error |
| 14 | + |
| 15 | +var ( |
| 16 | + // metadataBucketKey stores all the metadata concerning the state of the |
| 17 | + // database. |
| 18 | + metadataBucketKey = []byte("metadata") |
| 19 | + |
| 20 | + // dbVersionKey is the key used for storing/retrieving the current |
| 21 | + // database version. |
| 22 | + dbVersionKey = []byte("version") |
| 23 | + |
| 24 | + // ErrDBReversion is returned when detecting an attempt to revert to a |
| 25 | + // prior database version. |
| 26 | + ErrDBReversion = errors.New("cannot revert to prior version") |
| 27 | + |
| 28 | + // dbVersions is storing all versions of database. If the current |
| 29 | + // version of the database doesn't match the latest version this list |
| 30 | + // will be used for retrieving all migration function that are need to |
| 31 | + // apply to the current db. |
| 32 | + dbVersions []migration |
| 33 | + |
| 34 | + latestDBVersion = uint32(len(dbVersions)) |
| 35 | +) |
| 36 | + |
| 37 | +// getDBVersion retrieves the current database version. |
| 38 | +func getDBVersion(bucket *bbolt.Bucket) (uint32, error) { |
| 39 | + versionBytes := bucket.Get(dbVersionKey) |
| 40 | + if versionBytes == nil { |
| 41 | + return 0, errors.New("database version not found") |
| 42 | + } |
| 43 | + return byteOrder.Uint32(versionBytes), nil |
| 44 | +} |
| 45 | + |
| 46 | +// setDBVersion updates the current database version. |
| 47 | +func setDBVersion(bucket *bbolt.Bucket, version uint32) error { |
| 48 | + var b [4]byte |
| 49 | + byteOrder.PutUint32(b[:], version) |
| 50 | + return bucket.Put(dbVersionKey, b[:]) |
| 51 | +} |
| 52 | + |
| 53 | +// getBucket retrieves the bucket with the given key. |
| 54 | +func getBucket(tx *bbolt.Tx, key []byte) (*bbolt.Bucket, error) { |
| 55 | + bucket := tx.Bucket(key) |
| 56 | + if bucket == nil { |
| 57 | + return nil, fmt.Errorf("bucket \"%v\" does not exist", |
| 58 | + string(key)) |
| 59 | + } |
| 60 | + return bucket, nil |
| 61 | +} |
| 62 | + |
| 63 | +// syncVersions function is used for safe db version synchronization. It |
| 64 | +// applies migration functions to the current database and recovers the |
| 65 | +// previous state of db if at least one error/panic appeared during migration. |
| 66 | +func syncVersions(db *bbolt.DB) error { |
| 67 | + var currentVersion uint32 |
| 68 | + err := db.View(func(tx *bbolt.Tx) error { |
| 69 | + metadata, err := getBucket(tx, metadataBucketKey) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + currentVersion, err = getDBVersion(metadata) |
| 74 | + return err |
| 75 | + }) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + |
| 80 | + log.Infof("Checking for schema update: latest_version=%v, "+ |
| 81 | + "db_version=%v", latestDBVersion, currentVersion) |
| 82 | + |
| 83 | + switch { |
| 84 | + // If the database reports a higher version that we are aware of, the |
| 85 | + // user is probably trying to revert to a prior version of lnd. We fail |
| 86 | + // here to prevent reversions and unintended corruption. |
| 87 | + case currentVersion > latestDBVersion: |
| 88 | + log.Errorf("Refusing to revert from db_version=%d to "+ |
| 89 | + "lower version=%d", currentVersion, |
| 90 | + latestDBVersion) |
| 91 | + |
| 92 | + return ErrDBReversion |
| 93 | + |
| 94 | + // If the current database version matches the latest version number, |
| 95 | + // then we don't need to perform any migrations. |
| 96 | + case currentVersion == latestDBVersion: |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + log.Infof("Performing database schema migration") |
| 101 | + |
| 102 | + // Otherwise, we execute the migrations serially within a single |
| 103 | + // database transaction to ensure the migration is atomic. |
| 104 | + return db.Update(func(tx *bbolt.Tx) error { |
| 105 | + for v := currentVersion; v < latestDBVersion; v++ { |
| 106 | + log.Infof("Applying migration #%v", v+1) |
| 107 | + |
| 108 | + migration := dbVersions[v] |
| 109 | + if err := migration(tx); err != nil { |
| 110 | + log.Infof("Unable to apply migration #%v", v+1) |
| 111 | + return err |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + metadata, err := getBucket(tx, metadataBucketKey) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + return setDBVersion(metadata, latestDBVersion) |
| 120 | + }) |
| 121 | +} |
0 commit comments