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