2020-07-01 11:08:20 +00:00
|
|
|
package migration_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdata/influxdb/v2/bolt"
|
|
|
|
"github.com/influxdata/influxdb/v2/inmem"
|
|
|
|
"github.com/influxdata/influxdb/v2/kv"
|
|
|
|
"github.com/influxdata/influxdb/v2/kv/migration"
|
|
|
|
influxdbtesting "github.com/influxdata/influxdb/v2/testing"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newMigrator(t *testing.T, logger *zap.Logger, store kv.SchemaStore, now influxdbtesting.NowFunc) *migration.Migrator {
|
|
|
|
migrator, err := migration.NewMigrator(logger, store)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
migration.MigratorSetNow(migrator, now)
|
|
|
|
return migrator
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Inmem_Migrator(t *testing.T) {
|
|
|
|
influxdbtesting.Migrator(t, inmem.NewKVStore(), newMigrator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Bolt_Migrator(t *testing.T) {
|
2021-08-31 20:43:45 +00:00
|
|
|
store, closeBolt, err := newTestBoltStoreWithoutMigrations(t)
|
2020-07-01 11:08:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create new kv store: %v", err)
|
|
|
|
}
|
|
|
|
defer closeBolt()
|
|
|
|
|
|
|
|
influxdbtesting.Migrator(t, store, newMigrator)
|
|
|
|
}
|
|
|
|
|
2021-08-31 20:43:45 +00:00
|
|
|
func newTestBoltStoreWithoutMigrations(t *testing.T) (kv.SchemaStore, func(), error) {
|
2020-07-01 11:08:20 +00:00
|
|
|
f, err := ioutil.TempFile("", "influxdata-bolt-")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.New("unable to open temporary boltdb file")
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
path := f.Name()
|
2020-07-28 22:59:45 +00:00
|
|
|
s := bolt.NewKVStore(zaptest.NewLogger(t), path, bolt.WithNoSync)
|
2020-07-01 11:08:20 +00:00
|
|
|
if err := s.Open(context.Background()); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
close := func() {
|
|
|
|
s.Close()
|
|
|
|
os.Remove(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, close, nil
|
|
|
|
}
|