graphql-engine/cli/commands/migrate_apply.go

103 lines
2.8 KiB
Go
Raw Normal View History

2018-06-24 16:40:48 +03:00
package commands
import (
"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 {
2018-06-24 16:40:48 +03:00
opts := &migrateApplyOptions{
EC: ec,
}
migrateApplyCmd := &cobra.Command{
Use: "apply",
Short: "Apply migrations on the database",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return opts.run()
2018-06-24 16:40:48 +03:00
},
}
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", "", "migrate the database to a specific version")
f.StringVar(&opts.migrationType, "type", "up", "type of migration (up, down) to be used with version flag")
2018-06-24 16:40:48 +03:00
return migrateApplyCmd
}
type migrateApplyOptions struct {
EC *cli.ExecutionContext
upMigration string
downMigration string
versionMigration string
migrationType string
2018-06-24 16:40:48 +03:00
}
func (o *migrateApplyOptions) run() error {
migrationType, step, err := getMigrationTypeAndStep(o.upMigration, o.downMigration, o.versionMigration, o.migrationType)
2018-06-24 16:40:48 +03:00
if err != nil {
return errors.Wrap(err, "error validating flags")
}
migrateDrv, err := newMigrate(o.EC.MigrationDir, o.EC.Config.ParsedEndpoint, o.EC.Config.AccessKey, o.EC.Logger)
2018-06-24 16:40:48 +03:00
if err != nil {
return err
2018-06-24 16:40:48 +03:00
}
err = ExecuteMigration(migrationType, migrateDrv, step)
2018-06-24 16:40:48 +03:00
if err != nil {
if err == migrate.ErrNoChange {
o.EC.Logger.Info("nothing to apply")
return nil
}
return errors.Wrap(err, "apply failed")
}
o.EC.Logger.Info("migrations applied")
return nil
}
// 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
func getMigrationTypeAndStep(upMigration, downMigration, versionMigration, migrationType string) (string, int64, error) {
2018-06-24 16:40:48 +03:00
var flagCount = 0
var stepString = "all"
var migrationName = "up"
if upMigration != "" {
2018-06-24 16:40:48 +03:00
stepString = upMigration
flagCount++
}
if downMigration != "" {
2018-06-24 16:40:48 +03:00
migrationName = "down"
stepString = downMigration
flagCount++
}
if versionMigration != "" {
2018-06-24 16:40:48 +03:00
migrationName = "version"
stepString = versionMigration
if migrationType == "down" {
2018-06-24 16:40:48 +03:00
stepString = "-" + stepString
}
flagCount++
}
if flagCount > 1 {
return "", 0, errors.New("Only one migration type can be applied at a time (--up, --down or --goto)")
}
if stepString == "all" && migrationName != "version" {
return migrationName, -1, nil
}
step, err := strconv.ParseInt(stepString, 10, 64)
if err != nil {
return "", 0, errors.Wrap(err, "not a valid input for steps")
}
return migrationName, step, nil
}