mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-14 17:02:49 +03:00
90 lines
2.3 KiB
Go
90 lines
2.3 KiB
Go
package commands
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/hasura/graphql-engine/cli"
|
|
"github.com/hasura/graphql-engine/cli/migrate"
|
|
mig "github.com/hasura/graphql-engine/cli/migrate/cmd"
|
|
"github.com/hasura/graphql-engine/cli/util"
|
|
"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"
|
|
)
|
|
|
|
// NewMigrateCmd returns the migrate command
|
|
func NewMigrateCmd(ec *cli.ExecutionContext) *cobra.Command {
|
|
v := viper.New()
|
|
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
|
|
}
|
|
return ec.Validate()
|
|
},
|
|
}
|
|
migrateCmd.AddCommand(
|
|
newMigrateApplyCmd(ec),
|
|
newMigrateStatusCmd(ec),
|
|
newMigrateCreateCmd(ec),
|
|
newMigrateSquashCmd(ec),
|
|
)
|
|
|
|
f := migrateCmd.PersistentFlags()
|
|
|
|
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")
|
|
|
|
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"))
|
|
|
|
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
|
|
}
|