2021-01-15 21:35:21 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-09-21 18:11:16 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/cmd"
|
2021-09-17 21:55:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/io/file"
|
2021-01-15 21:35:21 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/db/kv"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2021-03-03 17:05:37 +00:00
|
|
|
// MigrateUp for a validator database.
|
|
|
|
func MigrateUp(cliCtx *cli.Context) error {
|
2021-01-15 21:35:21 +00:00
|
|
|
dataDir := cliCtx.String(cmd.DataDirFlag.Name)
|
|
|
|
|
2021-09-17 21:55:24 +00:00
|
|
|
if !file.FileExists(path.Join(dataDir, kv.ProtectionDbFileName)) {
|
2021-01-18 19:32:17 +00:00
|
|
|
return errors.New("No validator db found at path, nothing to migrate")
|
2021-01-15 21:35:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2021-02-17 19:23:59 +00:00
|
|
|
log.Info("Opening DB")
|
2021-02-15 20:29:47 +00:00
|
|
|
validatorDB, err := kv.NewKVStore(ctx, dataDir, &kv.Config{})
|
2021-01-15 21:35:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-17 19:23:59 +00:00
|
|
|
log.Info("Running migrations")
|
2021-01-15 21:35:21 +00:00
|
|
|
return validatorDB.RunUpMigrations(ctx)
|
|
|
|
}
|
|
|
|
|
2021-03-03 17:05:37 +00:00
|
|
|
// MigrateDown for a validator database.
|
|
|
|
func MigrateDown(cliCtx *cli.Context) error {
|
2021-01-15 21:35:21 +00:00
|
|
|
dataDir := cliCtx.String(cmd.DataDirFlag.Name)
|
|
|
|
|
2021-09-17 21:55:24 +00:00
|
|
|
if !file.FileExists(path.Join(dataDir, kv.ProtectionDbFileName)) {
|
2021-01-15 21:35:21 +00:00
|
|
|
return errors.New("No validator db found at path, nothing to rollback")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2021-02-17 19:23:59 +00:00
|
|
|
log.Info("Opening DB")
|
2021-02-15 20:29:47 +00:00
|
|
|
validatorDB, err := kv.NewKVStore(ctx, dataDir, &kv.Config{})
|
2021-01-15 21:35:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-17 19:23:59 +00:00
|
|
|
log.Info("Running migrations")
|
2021-01-15 21:35:21 +00:00
|
|
|
return validatorDB.RunDownMigrations(ctx)
|
|
|
|
}
|