graphql-engine/cli/commands/migrate.go

113 lines
3.1 KiB
Go
Raw Normal View History

2018-06-24 16:40:48 +03:00
package commands
import (
"fmt"
"github.com/hasura/graphql-engine/cli/internal/scripts"
"github.com/hasura/graphql-engine/cli/util"
"github.com/pkg/errors"
2018-06-24 16:40:48 +03:00
"github.com/hasura/graphql-engine/cli"
"github.com/hasura/graphql-engine/cli/migrate"
mig "github.com/hasura/graphql-engine/cli/migrate/cmd"
2018-06-24 16:40:48 +03:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
// Initialize migration drivers
_ "github.com/hasura/graphql-engine/cli/migrate/database/hasuradb"
_ "github.com/hasura/graphql-engine/cli/migrate/source/file"
2018-06-24 16:40:48 +03:00
)
// NewMigrateCmd returns the migrate command
2018-06-24 16:40:48 +03:00
func NewMigrateCmd(ec *cli.ExecutionContext) *cobra.Command {
v := viper.New()
2018-06-24 16:40:48 +03:00
migrateCmd := &cobra.Command{
Use: "migrate",
Short: "Manage migrations on the database",
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
cmd.Root().PersistentPreRun(cmd, args)
ec.Viper = v
err := ec.Prepare()
if err != nil {
return err
}
if err := ec.Validate(); err != nil {
return err
}
if ec.Config.Version >= cli.V3 {
if !cmd.Flags().Changed("database") {
return errors.New("database flag is required")
}
} else {
if err := scripts.CheckIfUpdateToConfigV3IsRequired(ec); err != nil {
return err
}
}
return nil
},
2018-06-24 16:40:48 +03:00
}
f := migrateCmd.PersistentFlags()
f.StringVar(&ec.Database, "database", "", "database on which operation should be applied")
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
f.String("admin-secret", "", "admin secret for Hasura GraphQL Engine")
f.String("access-key", "", "access key for Hasura GraphQL Engine")
f.MarkDeprecated("access-key", "use --admin-secret instead")
f.Bool("insecure-skip-tls-verify", false, "skip TLS verification and disable cert checking (default: false)")
f.String("certificate-authority", "", "path to a cert file for the certificate authority")
util.BindPFlag(v, "endpoint", f.Lookup("endpoint"))
util.BindPFlag(v, "admin_secret", f.Lookup("admin-secret"))
util.BindPFlag(v, "access_key", f.Lookup("access-key"))
util.BindPFlag(v, "insecure_skip_tls_verify", f.Lookup("insecure-skip-tls-verify"))
util.BindPFlag(v, "certificate_authority", f.Lookup("certificate-authority"))
migrateCmd.AddCommand(
newMigrateApplyCmd(ec),
newMigrateStatusCmd(ec),
newMigrateCreateCmd(ec),
newMigrateSquashCmd(ec),
)
2018-06-24 16:40:48 +03:00
return migrateCmd
}
// ExecuteMigration runs the actual migration
func ExecuteMigration(cmd string, t *migrate.Migrate, stepOrVersion int64) error {
var err error
switch cmd {
case "up":
err = mig.UpCmd(t, stepOrVersion)
case "down":
err = mig.DownCmd(t, stepOrVersion)
case "gotoVersion":
err = mig.GotoVersionCmd(t, stepOrVersion)
case "version":
var direction string
if stepOrVersion >= 0 {
direction = "up"
} else {
direction = "down"
stepOrVersion = -(stepOrVersion)
}
err = mig.GotoCmd(t, uint64(stepOrVersion), direction)
default:
err = fmt.Errorf("invalid command")
}
return err
}
func executeStatus(t *migrate.Migrate) (*migrate.Status, error) {
status, err := t.GetStatus()
if err != nil {
return nil, err
}
return status, nil
}