Repository: hasura/graphql-engine
File: cli/commands/migrate_apply.go (Go)

Code before:
	"os"
	"strconv"

	"github.com/hasura/graphql-engine/cli"
	migrate "github.com/hasura/graphql-engine/cli/migrate"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

func newMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
	v := viper.New()
	opts := &migrateApplyOptions{
		EC: ec,
	}
	migrateApplyCmd := &cobra.Command{
		Use:          "apply",
		Short:        "Apply migrations on the database",
		SilenceUsage: true,
		PreRunE: func(cmd *cobra.Command, args []string) error {
			ec.Viper = v
			return ec.Validate()
		},
		RunE: func(cmd *cobra.Command, args []string) error {
			if opts.dryRun && opts.skipExecution {
				return errors.New("both --skip-execution and --dry-run flag cannot be set to true")
			}
			if !opts.dryRun {
				opts.EC.Spin("Applying migrations...")
			}
			err := opts.run()
			opts.EC.Spinner.Stop()
			if err != nil {
				return err
			}
			if !opts.dryRun {
				opts.EC.Logger.Info("migrations applied")
			}
			return nil
		},
	}
	f := migrateApplyCmd.Flags()

	f.StringVar(&opts.upMigration, "up", "", "apply all or N up migration steps")
	f.StringVar(&opts.downMigration, "down", "", "apply all or N down migration steps")
	f.StringVar(&opts.versionMigration, "version", "", "only apply this particular migration")
	f.StringVar(&opts.migrationType, "type", "up", "type of migration (up, down) to be used with version flag")
	f.BoolVar(&opts.skipExecution, "skip-execution", false, "skip executing the migration action, but mark them as applied")
	f.BoolVar(&opts.dryRun, "dry-run", false, "print the names of migrations which are going to be applied")

	f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")

Review comment (refactor):
```suggestion
				return errors.New("both --skip-execution and --dry-run flag cannot be used together")
```

Code after:
	"os"
	"strconv"

	"github.com/hasura/graphql-engine/cli"
	migrate "github.com/hasura/graphql-engine/cli/migrate"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
)

func newMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
	opts := &MigrateApplyOptions{
		EC: ec,
	}
	migrateApplyCmd := &cobra.Command{
		Use:   "apply",
		Short: "Apply migrations on the database",
		Example: `  # Apply all migrations
  hasura migrate apply

  # Use with admin secret:
  hasura migrate apply --admin-secret "<admin-secret>"

  # Apply migrations on another Hasura instance:
  hasura migrate apply --endpoint "<endpoint>"

  # Mark migration as applied on the server and skip execution:
  hasura migrate apply --skip-execution

  # Apply a particular migration version only:
  hasura migrate apply --version "<version>"

  # Apply last 2 down migrations:
  hasura migrate apply --down 2

  # Apply only 2 up migrations:
  hasura migrate apply --up 2

  # Apply only a particular version
  hasura migrate apply --type up --version "<version>"
  
  # Apply all up migrations upto version 125, last applied is 100
  hasura migrate apply --goto 125
  
  # Apply all down migrations upto version 125, last applied is 150
  hasura migrate apply --goto 125

  # Rollback a particular version:
  hasura migrate apply --type down --version "<version>"

  # Rollback all migrations:
  hasura migrate apply --down all`,