mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 17:31:56 +03:00
a0995d1b30
GitOrigin-RevId: 5a1d9cb772ac62603f2543bfe6c01a95c0a035c6
113 lines
3.1 KiB
Go
113 lines
3.1 KiB
Go
package commands
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/hasura/graphql-engine/cli/internal/scripts"
|
|
|
|
"github.com/hasura/graphql-engine/cli/util"
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/hasura/graphql-engine/cli"
|
|
"github.com/hasura/graphql-engine/cli/migrate"
|
|
mig "github.com/hasura/graphql-engine/cli/migrate/cmd"
|
|
"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
|
|
}
|
|
if err := ec.Validate(); err != nil {
|
|
return err
|
|
}
|
|
if ec.Config.Version >= cli.V3 {
|
|
if !cmd.Flags().Changed("datasource") {
|
|
return errors.New("datasource flag is required")
|
|
}
|
|
} else {
|
|
if err := scripts.CheckIfUpdateToConfigV3IsRequired(ec); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
|
|
},
|
|
}
|
|
|
|
f := migrateCmd.PersistentFlags()
|
|
f.StringVar(&ec.Datasource, "datasource", "", "datasource name 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),
|
|
)
|
|
|
|
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
|
|
}
|