2018-06-24 16:40:48 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2021-03-08 14:59:35 +03:00
|
|
|
"fmt"
|
2018-09-10 16:21:30 +03:00
|
|
|
"os"
|
2018-06-24 16:40:48 +03:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/hasura/graphql-engine/cli"
|
|
|
|
migrate "github.com/hasura/graphql-engine/cli/migrate"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-06-28 11:36:25 +03:00
|
|
|
func newMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
|
2020-02-24 19:14:46 +03:00
|
|
|
opts := &MigrateApplyOptions{
|
2018-06-24 16:40:48 +03:00
|
|
|
EC: ec,
|
|
|
|
}
|
|
|
|
migrateApplyCmd := &cobra.Command{
|
2020-02-03 10:03:32 +03:00
|
|
|
Use: "apply",
|
|
|
|
Short: "Apply migrations on the database",
|
2019-12-12 08:16:36 +03:00
|
|
|
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>"
|
2020-02-03 10:03:32 +03:00
|
|
|
|
|
|
|
# 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
|
2019-12-12 08:16:36 +03:00
|
|
|
|
|
|
|
# Rollback a particular version:
|
|
|
|
hasura migrate apply --type down --version "<version>"
|
|
|
|
|
|
|
|
# Rollback all migrations:
|
|
|
|
hasura migrate apply --down all`,
|
2018-06-24 16:40:48 +03:00
|
|
|
SilenceUsage: true,
|
2018-09-27 16:57:17 +03:00
|
|
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
2020-02-24 19:14:46 +03:00
|
|
|
err := ec.Prepare()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-27 16:57:17 +03:00
|
|
|
return ec.Validate()
|
|
|
|
},
|
2018-06-24 16:40:48 +03:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2021-03-08 14:59:35 +03:00
|
|
|
opts.Source = ec.Source
|
2020-06-03 14:19:36 +03:00
|
|
|
if opts.dryRun && opts.SkipExecution {
|
|
|
|
return errors.New("both --skip-execution and --dry-run flags cannot be used together")
|
|
|
|
}
|
|
|
|
if !opts.dryRun {
|
|
|
|
opts.EC.Spin("Applying migrations...")
|
|
|
|
}
|
2020-02-24 19:14:46 +03:00
|
|
|
err := opts.Run()
|
2019-04-03 14:29:58 +03:00
|
|
|
opts.EC.Spinner.Stop()
|
|
|
|
if err != nil {
|
2020-02-03 10:03:32 +03:00
|
|
|
if err == migrate.ErrNoChange {
|
|
|
|
opts.EC.Logger.Info("nothing to apply")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if e, ok := err.(*os.PathError); ok {
|
|
|
|
// If Op is first, then log No migrations to apply
|
|
|
|
if e.Op == "first" {
|
|
|
|
opts.EC.Logger.Info("nothing to apply")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-03-08 14:59:35 +03:00
|
|
|
return fmt.Errorf("apply failed\n%w", err)
|
2019-04-03 14:29:58 +03:00
|
|
|
}
|
2020-06-03 14:19:36 +03:00
|
|
|
if !opts.dryRun {
|
|
|
|
opts.EC.Logger.Info("migrations applied")
|
|
|
|
}
|
2019-04-03 14:29:58 +03:00
|
|
|
return nil
|
2018-06-24 16:40:48 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
f := migrateApplyCmd.Flags()
|
2020-02-03 10:03:32 +03:00
|
|
|
f.SortFlags = false
|
2018-06-24 16:40:48 +03:00
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
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.GotoVersion, "goto", "", "apply migration chain up to to the version specified")
|
2018-09-27 16:57:17 +03:00
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
f.StringVar(&opts.VersionMigration, "version", "", "only apply this particular migration")
|
|
|
|
f.BoolVar(&opts.SkipExecution, "skip-execution", false, "skip executing the migration action, but mark them as applied")
|
|
|
|
f.StringVar(&opts.MigrationType, "type", "up", "type of migration (up, down) to be used with version flag")
|
2018-09-27 16:57:17 +03:00
|
|
|
|
2020-06-03 14:19:36 +03:00
|
|
|
f.BoolVar(&opts.dryRun, "dry-run", false, "print the names of migrations which are going to be applied")
|
2018-06-24 16:40:48 +03:00
|
|
|
return migrateApplyCmd
|
|
|
|
}
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
type MigrateApplyOptions struct {
|
2018-06-24 16:40:48 +03:00
|
|
|
EC *cli.ExecutionContext
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
UpMigration string
|
|
|
|
DownMigration string
|
|
|
|
VersionMigration string
|
|
|
|
MigrationType string
|
2020-02-03 10:03:32 +03:00
|
|
|
// version up to which migration chain has to be applied
|
2020-02-24 19:14:46 +03:00
|
|
|
GotoVersion string
|
|
|
|
SkipExecution bool
|
2020-06-03 14:19:36 +03:00
|
|
|
dryRun bool
|
2021-03-08 14:59:35 +03:00
|
|
|
Source cli.Source
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
func (o *MigrateApplyOptions) Run() error {
|
|
|
|
migrationType, step, err := getMigrationTypeAndStep(o.UpMigration, o.DownMigration, o.VersionMigration, o.MigrationType, o.GotoVersion, o.SkipExecution)
|
2018-06-24 16:40:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error validating flags")
|
|
|
|
}
|
|
|
|
|
2021-03-08 14:59:35 +03:00
|
|
|
migrateDrv, err := migrate.NewMigrate(o.EC, true, o.Source.Name, o.Source.Kind)
|
2018-06-24 16:40:48 +03:00
|
|
|
if err != nil {
|
2018-07-09 16:47:38 +03:00
|
|
|
return err
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
2020-02-24 19:14:46 +03:00
|
|
|
migrateDrv.SkipExecution = o.SkipExecution
|
2020-06-03 14:19:36 +03:00
|
|
|
migrateDrv.DryRun = o.dryRun
|
2018-06-24 16:40:48 +03:00
|
|
|
|
2020-02-03 10:03:32 +03:00
|
|
|
return ExecuteMigration(migrationType, migrateDrv, step)
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2018-06-28 11:36:25 +03:00
|
|
|
// Only one flag out of up, down and version can be set at a time. This function
|
|
|
|
// checks whether that is the case and returns an error is not
|
2020-02-03 10:03:32 +03:00
|
|
|
func getMigrationTypeAndStep(upMigration, downMigration, versionMigration, migrationType, gotoVersion string, skipExecution bool) (string, int64, error) {
|
2018-06-24 16:40:48 +03:00
|
|
|
var flagCount = 0
|
|
|
|
var stepString = "all"
|
|
|
|
var migrationName = "up"
|
2018-06-28 11:36:25 +03:00
|
|
|
if upMigration != "" {
|
2018-06-24 16:40:48 +03:00
|
|
|
stepString = upMigration
|
|
|
|
flagCount++
|
|
|
|
}
|
2018-06-28 11:36:25 +03:00
|
|
|
if downMigration != "" {
|
2018-06-24 16:40:48 +03:00
|
|
|
migrationName = "down"
|
|
|
|
stepString = downMigration
|
|
|
|
flagCount++
|
|
|
|
}
|
2018-06-28 11:36:25 +03:00
|
|
|
if versionMigration != "" {
|
2018-06-24 16:40:48 +03:00
|
|
|
migrationName = "version"
|
|
|
|
stepString = versionMigration
|
2018-06-28 11:36:25 +03:00
|
|
|
if migrationType == "down" {
|
2018-06-24 16:40:48 +03:00
|
|
|
stepString = "-" + stepString
|
|
|
|
}
|
|
|
|
flagCount++
|
|
|
|
}
|
2020-02-03 10:03:32 +03:00
|
|
|
if gotoVersion != "" {
|
|
|
|
migrationName = "gotoVersion"
|
|
|
|
stepString = gotoVersion
|
|
|
|
flagCount++
|
|
|
|
}
|
2018-06-24 16:40:48 +03:00
|
|
|
|
|
|
|
if flagCount > 1 {
|
2020-02-24 19:14:46 +03:00
|
|
|
return "", 0, errors.New("only one migration type can be applied at a time (--up, --down or --goto)")
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2019-03-18 19:40:04 +03:00
|
|
|
if migrationName != "version" && skipExecution {
|
|
|
|
return "", 0, errors.New("--skip-execution flag can be set only with --version flag")
|
|
|
|
}
|
|
|
|
|
2018-06-24 16:40:48 +03:00
|
|
|
if stepString == "all" && migrationName != "version" {
|
|
|
|
return migrationName, -1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
step, err := strconv.ParseInt(stepString, 10, 64)
|
|
|
|
if err != nil {
|
2020-02-03 10:03:32 +03:00
|
|
|
return "", 0, errors.Wrap(err, "not a valid input for steps/version")
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
return migrationName, step, nil
|
|
|
|
}
|