-
Notifications
You must be signed in to change notification settings - Fork 573
feat: scaffold migrations #4902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Pantani
wants to merge
6
commits into
main
Choose a base branch
from
feat/scaffold-migrations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b1d7f0f
add migration template
Pantani 65fa925
Merge branch 'main' into feat/scaffold-migrations
Pantani 7bcb0dc
remove migration name
Pantani 7006184
add changelog
Pantani 1eb7d64
Merge branch 'main' into feat/scaffold-migrations
Pantani cff31d6
add integration tests
Pantani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package ignitecmd | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/ignite/cli/v29/ignite/pkg/cache" | ||
| "github.com/ignite/cli/v29/ignite/pkg/cliui" | ||
| "github.com/ignite/cli/v29/ignite/pkg/xgenny" | ||
| "github.com/ignite/cli/v29/ignite/services/scaffolder" | ||
| ) | ||
|
|
||
| // NewScaffoldMigration returns the command to scaffold a module migration. | ||
| func NewScaffoldMigration() *cobra.Command { | ||
| c := &cobra.Command{ | ||
| Use: "migration [module] [name]", | ||
| Short: "Module migration boilerplate", | ||
| Long: `Scaffold no-op migration boilerplate for an existing Cosmos SDK module. | ||
|
|
||
| This command creates a new migration file in "x/<module>/migrations/vN/", | ||
| increments the module consensus version, and registers the new migration handler | ||
| inside "x/<module>/module/module.go".`, | ||
| Args: cobra.ExactArgs(2), | ||
| PreRunE: migrationPreRunHandler, | ||
| RunE: scaffoldMigrationHandler, | ||
| } | ||
|
|
||
| flagSetPath(c) | ||
| c.Flags().AddFlagSet(flagSetYes()) | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| func scaffoldMigrationHandler(cmd *cobra.Command, args []string) error { | ||
| var ( | ||
| moduleName = args[0] | ||
| migrationName = args[1] | ||
| appPath = flagGetPath(cmd) | ||
| ) | ||
|
|
||
| session := cliui.New( | ||
| cliui.StartSpinnerWithText(statusScaffolding), | ||
| cliui.WithoutUserInteraction(getYes(cmd)), | ||
| ) | ||
| defer session.End() | ||
|
|
||
| cfg, _, err := getChainConfig(cmd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| sc, err := scaffolder.New(cmd.Context(), appPath, cfg.Build.Proto.Path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := sc.CreateModuleMigration(moduleName, migrationName); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| sm, err := sc.ApplyModifications(xgenny.ApplyPreRun(scaffolder.AskOverwriteFiles(session))) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := sc.PostScaffold(cmd.Context(), cache.Storage{}, true); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| modificationsStr, err := sm.String() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| session.Println(modificationsStr) | ||
| session.Printf("\n🎉 Migration %s added to module %s.\n\n", migrationName, moduleName) | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package scaffolder | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/ignite/cli/v29/ignite/pkg/errors" | ||
| "github.com/ignite/cli/v29/ignite/pkg/multiformatname" | ||
| modulemigration "github.com/ignite/cli/v29/ignite/templates/module/migration" | ||
| ) | ||
|
|
||
| // CreateModuleMigration scaffolds a new module migration inside an existing module. | ||
| func (s Scaffolder) CreateModuleMigration(moduleName, migrationName string) error { | ||
| mfModuleName, err := multiformatname.NewName(moduleName, multiformatname.NoNumber) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| moduleName = mfModuleName.LowerCase | ||
|
|
||
| mfMigrationName, err := multiformatname.NewName(migrationName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := checkGoReservedWord(mfMigrationName.LowerCamel); err != nil { | ||
| return errors.Errorf("%s can't be used as a migration name: %w", migrationName, err) | ||
| } | ||
|
|
||
| ok, err := moduleExists(s.appPath, moduleName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if !ok { | ||
| return errors.Errorf("the module %s doesn't exist", moduleName) | ||
| } | ||
|
|
||
| moduleFilePath := filepath.Join(s.appPath, moduleDir, moduleName, modulePkg, "module.go") | ||
| content, err := os.ReadFile(moduleFilePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fromVersion, err := modulemigration.ConsensusVersion(string(content)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| opts := &modulemigration.Options{ | ||
| ModuleName: moduleName, | ||
| ModulePath: s.modpath.RawPath, | ||
| MigrationName: mfMigrationName, | ||
| FromVersion: fromVersion, | ||
| ToVersion: fromVersion + 1, | ||
| } | ||
|
|
||
| versionDir := filepath.Join(s.appPath, opts.MigrationDir()) | ||
| if _, err := os.Stat(versionDir); err == nil { | ||
| return errors.Errorf("migration version %s already exists for module %s", opts.MigrationVersion(), moduleName) | ||
| } else if !os.IsNotExist(err) { | ||
| return err | ||
| } | ||
|
|
||
| g, err := modulemigration.NewGenerator(opts) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return s.Run(g) | ||
| } |
8 changes: 8 additions & 0 deletions
8
...gration/files/x/{{moduleName}}/migrations/{{migrationVersion}}/{{migrationName}}.go.plush
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package <%= migrationVersion %> | ||
|
|
||
| import sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
|
||
| // <%= migrationFunc %> performs in-place store migrations from version <%= fromVersion %> to <%= toVersion %>. | ||
| func <%= migrationFunc %>(_ sdk.Context) error { | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package modulemigration | ||
|
|
||
| import ( | ||
| "io/fs" | ||
|
|
||
| "github.com/gobuffalo/genny/v2" | ||
| "github.com/gobuffalo/plush/v4" | ||
|
|
||
| "github.com/ignite/cli/v29/ignite/pkg/errors" | ||
| "github.com/ignite/cli/v29/ignite/pkg/xgenny" | ||
| ) | ||
|
|
||
| // NewGenerator returns the generator to scaffold a new module migration. | ||
| func NewGenerator(opts *Options) (*genny.Generator, error) { | ||
| subFS, err := fs.Sub(files, "files") | ||
| if err != nil { | ||
| return nil, errors.Errorf("fail to generate sub: %w", err) | ||
| } | ||
|
|
||
| g := genny.New() | ||
| if err := g.OnlyFS(subFS, nil, nil); err != nil { | ||
| return g, err | ||
| } | ||
|
|
||
| ctx := plush.NewContext() | ||
| ctx.Set("fromVersion", opts.FromVersion) | ||
| ctx.Set("migrationFunc", opts.MigrationFunc()) | ||
| ctx.Set("migrationName", opts.MigrationName) | ||
| ctx.Set("migrationVersion", opts.MigrationVersion()) | ||
| ctx.Set("moduleName", opts.ModuleName) | ||
| ctx.Set("toVersion", opts.ToVersion) | ||
|
|
||
| g.Transformer(xgenny.Transformer(ctx)) | ||
| g.Transformer(genny.Replace("{{migrationName}}", opts.MigrationName.Snake)) | ||
| g.Transformer(genny.Replace("{{migrationVersion}}", opts.MigrationVersion())) | ||
| g.Transformer(genny.Replace("{{moduleName}}", opts.ModuleName)) | ||
| g.RunFn(moduleModify(opts)) | ||
|
|
||
| return g, nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.