diff --git a/CHANGELOG.md b/CHANGELOG.md index fb5e15d40d4..6a821cfa950 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,12 +11,13 @@ Because of the version bump to `go`, the macOS build for this release requires a ### Bug Fixes -1. [21748](https://github.com/influxdata/influxdb/pull/21748): rename arm rpms with yum-compatible names. +1. [21748](https://github.com/influxdata/influxdb/pull/21748): Rename arm rpms with yum-compatible names. 1. [21851](https://github.com/influxdata/influxdb/pull/21851): Upgrade to latest version of `influxdata/cron` so that tasks can be created with interval of `every: 1w`. 1. [21859](https://github.com/influxdata/influxdb/pull/21859): Avoid rewriting `fields.idx` unnecessarily. 1. [21860](https://github.com/influxdata/influxdb/pull/21860): Do not close connection twice in DigestWithOptions. 1. [21866](https://github.com/influxdata/influxdb/pull/21866): Remove incorrect optimization for group-by. 1. [21867](https://github.com/influxdata/influxdb/pull/21867): Return an error instead of panicking when InfluxQL statement rewrites fail. +1. [21868](https://github.com/influxdata/influxdb/pull/21868): Migrate restored KV snapshots to latest schema before using them. ## v2.0.7 [2021-06-04] ---------------------- diff --git a/bolt/kv.go b/bolt/kv.go index 2a0de90f879..bab1bc962ba 100644 --- a/bolt/kv.go +++ b/bolt/kv.go @@ -13,6 +13,8 @@ import ( "github.com/influxdata/influxdb/v2/kit/tracing" "github.com/influxdata/influxdb/v2/kv" + "github.com/influxdata/influxdb/v2/kv/migration" + "github.com/influxdata/influxdb/v2/kv/migration/all" "github.com/influxdata/influxdb/v2/pkg/fs" bolt "go.etcd.io/bbolt" "go.uber.org/zap" @@ -221,6 +223,11 @@ func (s *KVStore) Restore(ctx context.Context, r io.Reader) error { return err } + // Run the migrations on the restored database prior to swapping it in. + if err := s.migrateRestored(ctx); err != nil { + return err + } + // Swap and reopen under lock. s.mu.Lock() defer s.mu.Unlock() @@ -243,6 +250,33 @@ func (s *KVStore) Restore(ctx context.Context, r io.Reader) error { return nil } +// migrateRestored opens the database at the temporary path and applies the +// migrations to it. The database at the temporary path is closed after the +// migrations are complete. This should be used as part of the restore +// operation, prior to swapping the restored database with the active database. +func (s *KVStore) migrateRestored(ctx context.Context) error { + restoredClient := NewClient(s.log.With(zap.String("service", "restored bolt"))) + restoredClient.Path = s.tempPath() + if err := restoredClient.Open(ctx); err != nil { + return err + } + defer restoredClient.Close() + + restoredKV := NewKVStore(s.log.With(zap.String("service", "restored kvstore-bolt")), s.tempPath()) + restoredKV.WithDB(restoredClient.DB()) + + migrator, err := migration.NewMigrator( + s.log.With(zap.String("service", "bolt restore migrations")), + restoredKV, + all.Migrations[:]..., + ) + if err != nil { + return err + } + + return migrator.Up(ctx) +} + // Tx is a light wrapper around a boltdb transaction. It implements kv.Tx. type Tx struct { tx *bolt.Tx