Skip to content

Commit

Permalink
clean up the cli
Browse files Browse the repository at this point in the history
  • Loading branch information
krehermann committed Jun 18, 2024
1 parent dbd5cf7 commit 1715ce5
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 49 deletions.
121 changes: 89 additions & 32 deletions core/cmd/shell_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ func initLocalSubCmds(s *Shell, safe bool) []cli.Command {
Before: s.validateDB,
Flags: []cli.Flag{
cli.StringFlag{
Name: "plugin-type",
Usage: "if set, limit results the specified plugin type. [relayer,app] ",
Name: "relayer",
Usage: "type of relayer migration to run",
Hidden: true,
},
cli.StringFlag{
Name: "plugin-kind",
Usage: "if set, limit results for specified plugin kind for the given plugin type ",
Name: "chain-id",
Usage: "chain id for the relayer migration",
Hidden: true,
},
},
Expand All @@ -214,13 +214,13 @@ func initLocalSubCmds(s *Shell, safe bool) []cli.Command {
Before: s.validateDB,
Flags: []cli.Flag{
cli.StringFlag{
Name: "plugin-type",
Usage: "if set, limit results the specified plugin type. [relayer,app] ",
Name: "relayer",
Usage: "type of relayer migration to run",
Hidden: true,
},
cli.StringFlag{
Name: "plugin-kind",
Usage: "if set, limit results for specified plugin kind for the given plugin type ",
Name: "chain-id",
Usage: "chain id for the relayer migration",
Hidden: true,
},
},
Expand Down Expand Up @@ -250,13 +250,13 @@ func initLocalSubCmds(s *Shell, safe bool) []cli.Command {
Before: s.validateDB,
Flags: []cli.Flag{
cli.StringFlag{
Name: "plugin-type",
Usage: "if set, only roll back migrations for the specified plugin type.[relayer,app] ",
Name: "relayer",
Usage: "type of relayer migration to run",
Hidden: true,
},
cli.StringFlag{
Name: "plugin-kind",
Usage: "if set, only roll back migrations for the specified plugin kind for the given plugin type ",
Name: "chain-id",
Usage: "chain id for the relayer migration",
Hidden: true,
},
},
Expand Down Expand Up @@ -969,6 +969,17 @@ type relayMigrationOpts struct {
ChainID string
}

func (r *relayMigrationOpts) evmDbCfg() (evmdb.Cfg, error) {
id, err := strconv.Atoi(r.ChainID)
if err != nil {
return evmdb.Cfg{}, fmt.Errorf("failed to parse evm chain id %s: %w", r.ChainID, err)
}
return evmdb.Cfg{
Schema: "evm_" + r.ChainID,
ChainID: ubig.NewI(int64(id)),
}, nil
}

func newRelayMigrationOpts(c *cli.Context) *relayMigrationOpts {
if c.String("relayer") == "" || c.String("chain-id") == "" {
return nil
Expand Down Expand Up @@ -1019,42 +1030,87 @@ func (s *Shell) RollbackDatabase(c *cli.Context) error {
return fmt.Errorf("failed to initialize orm: %v", err)
}

if err := migrate.Rollback(ctx, db.DB, version); err != nil {
return fmt.Errorf("migrateDB failed: %v", err)
opts := newRelayMigrationOpts(c)
if opts == nil {
if err := migrate.Rollback(ctx, db.DB, version); err != nil {
return fmt.Errorf("migrateDB failed: %v", err)
}
} else if opts.Relayer == "evm" {
cfg, err := opts.evmDbCfg()
if err != nil {
return fmt.Errorf("failed to convert opts %v to evm db cfg %w", opts, err)
}

err = evmdb.Rollback(ctx, db.DB, version, cfg)
if err != nil {
return fmt.Errorf("evm for cfg %v rollback failed: %w", cfg, err)
}
} else {
err = fmt.Errorf("unknown relayer '%s'", opts.Relayer)
}
return err

Check failure on line 1051 in core/cmd/shell_local.go

View workflow job for this annotation

GitHub Actions / lint

unreachable-code: unreachable code after this statement (revive)

return nil
}

// VersionDatabase displays the current database version.
func (s *Shell) VersionDatabase(_ *cli.Context) error {
func (s *Shell) VersionDatabase(c *cli.Context) error {
ctx := s.ctx()
db, err := newConnection(s.Config.Database())
if err != nil {
return fmt.Errorf("failed to initialize orm: %v", err)
}

version, err := migrate.Current(ctx, db.DB)
if err != nil {
return fmt.Errorf("migrateDB failed: %v", err)
}
opts := newRelayMigrationOpts(c)
if opts == nil {
version, err := migrate.Current(ctx, db.DB)
if err != nil {
return fmt.Errorf("migrateDB failed: %v", err)
}

s.Logger.Infof("Database version: %v", version)
return nil
s.Logger.Infof("Database version: %v", version)
} else if opts.Relayer == "evm" {
cfg, err := opts.evmDbCfg()
if err != nil {
return fmt.Errorf("failed to convert opts %v to evm db cfg %w", opts, err)
}
version, err := evmdb.Current(ctx, db.DB, cfg)
if err != nil {
return fmt.Errorf("evm for cfg %v current failed: %w", cfg, err)
}
s.Logger.Infof("EVM database version: %v", version)
} else {
err = fmt.Errorf("unknown relayer '%s'", opts.Relayer)
}
return err
}

// StatusDatabase displays the database migration status
func (s *Shell) StatusDatabase(_ *cli.Context) error {
func (s *Shell) StatusDatabase(c *cli.Context) error {
ctx := s.ctx()
db, err := newConnection(s.Config.Database())
if err != nil {
return fmt.Errorf("failed to initialize orm: %v", err)
}

if err = migrate.Status(ctx, db.DB); err != nil {
return fmt.Errorf("Status failed: %v", err)
opts := newRelayMigrationOpts(c)
if opts == nil {
if err = migrate.Status(ctx, db.DB); err != nil {
return fmt.Errorf("Status failed: %v", err)
}
} else if opts.Relayer == "evm" {
cfg, err := opts.evmDbCfg()
if err != nil {
return fmt.Errorf("failed to convert opts %v to evm db cfg %w", opts, err)
}
err = evmdb.Status(ctx, db.DB, cfg)
if err != nil {
return fmt.Errorf("evm for cfg %v status failed: %w", cfg, err)
}
} else {
err = fmt.Errorf("unknown relayer '%s'", opts.Relayer)
}
return nil
return err
}

// CreateMigration displays the database migration status
Expand Down Expand Up @@ -1195,22 +1251,23 @@ func migrateDB(ctx context.Context, config dbConfig, lggr logger.Logger, opts *r
if err != nil {
return fmt.Errorf("failed to initialize orm: %v", err)
}
if opts != nil {
if opts == nil {
if err = migrate.Migrate(ctx, db.DB); err != nil {
return fmt.Errorf("migrateDB failed: %v", err)
}
} else if opts.Relayer == "evm" {

cid, err := strconv.Atoi(opts.ChainID)
cfg, err := opts.evmDbCfg()
if err != nil {
return fmt.Errorf("failed to parse chain as int id: %v", err)
return fmt.Errorf("failed to convert opts %v to evm db cfg %w", opts, err)
}
err = evmdb.Migrate(ctx, db.DB, evmdb.Cfg{Schema: opts.Relayer + "_" + opts.ChainID, ChainID: ubig.NewI(int64(cid))})
err = evmdb.Migrate(ctx, db.DB, cfg)
if err != nil {
return fmt.Errorf("migrateDB failed: %v", err)
return fmt.Errorf("evm db migrate failed for cfg %v: %w", cfg, err)
}
} else {
err = fmt.Errorf("unknown migration type %s", opts.Relayer)
}
return fmt.Errorf("unknown migration type %s", opts.Relayer)
return err
}

func downAndUpDB(ctx context.Context, cfg dbConfig, lggr logger.Logger, baseVersionID int64) error {
Expand Down
1 change: 0 additions & 1 deletion core/scripts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/guregu/null.v2 v2.1.2 // indirect
gopkg.in/guregu/null.v4 v4.0.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
10 changes: 7 additions & 3 deletions core/store/migrate/plugins/relayer/evm/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"path/filepath"

"github.com/pressly/goose/v3"
"gopkg.in/guregu/null.v2"
"gopkg.in/guregu/null.v4"
)

func setupPluginMigrations(cfg Cfg) {
goose.SetBaseFS(nil)
// reset the base fs and the global migrations
goose.SetBaseFS(nil) // we don't want to use the base fs for plugin migrations because the embedded fs contains templates, not sql files
goose.ResetGlobalMigrations()
goose.SetTableName(fmt.Sprintf("goose_migration_relayer_%s_%s", cfg.Schema, cfg.ChainID.String()))
Register0002(cfg)

Check failure on line 19 in core/store/migrate/plugins/relayer/evm/migrate.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)
Expand Down Expand Up @@ -47,7 +48,6 @@ func Migrate(ctx context.Context, db *sql.DB, cfg Cfg) error {
}

func Rollback(ctx context.Context, db *sql.DB, version null.Int, cfg Cfg) error {

tmpDir := os.TempDir()
defer os.RemoveAll(tmpDir)

Expand Down Expand Up @@ -80,5 +80,9 @@ func Current(ctx context.Context, db *sql.DB, cfg Cfg) (int64, error) {

func Status(ctx context.Context, db *sql.DB, cfg Cfg) error {
setupPluginMigrations(cfg)
// set the base fs only for status so that the templates are listed
// an alternative would be to somehow keep track of the generated sql files, but that would be more complex
// and error prone WRT to restarts
goose.SetBaseFS(embeddedTmplFS)
return goose.Status(db, MigrationRootDir)
}
2 changes: 1 addition & 1 deletion core/store/migrate/plugins/relayer/evm/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v2"
"gopkg.in/guregu/null.v4"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big"
"github.com/smartcontractkit/chainlink/v2/core/internal/cltest/heavyweight"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ require (
gonum.org/v1/gonum v0.14.0
google.golang.org/grpc v1.59.0
google.golang.org/protobuf v1.33.0
gopkg.in/guregu/null.v2 v2.1.2
gopkg.in/guregu/null.v4 v4.0.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)
Expand Down Expand Up @@ -332,6 +331,7 @@ require (
google.golang.org/genproto v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
gopkg.in/guregu/null.v2 v2.1.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ require (
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
github.com/opencontainers/runc v1.1.7 // indirect
github.com/opentracing-contrib/go-grpc v0.0.0-20210225150812-73cb765af46e // indirect
github.com/opentracing-contrib/go-stdlib v1.0.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
Expand All @@ -354,6 +353,7 @@ require (
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/pressly/goose/v3 v3.16.0 // indirect
github.com/prometheus/alertmanager v0.26.0 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
Expand All @@ -371,6 +371,7 @@ require (
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 // indirect
github.com/sercand/kuberesolver/v5 v5.1.1 // indirect
github.com/sethvargo/go-retry v0.2.4 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/shirou/gopsutil/v3 v3.24.3 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
Expand Down
Loading

0 comments on commit 1715ce5

Please sign in to comment.