2018-06-24 16:40:48 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
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-09-27 16:57:17 +03:00
|
|
|
"github.com/spf13/viper"
|
2018-06-24 16:40:48 +03:00
|
|
|
)
|
|
|
|
|
2018-06-28 11:36:25 +03:00
|
|
|
func newMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
|
2018-09-27 16:57:17 +03:00
|
|
|
v := viper.New()
|
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,
|
2018-09-27 16:57:17 +03:00
|
|
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
ec.Viper = v
|
|
|
|
return ec.Validate()
|
|
|
|
},
|
2018-06-24 16:40:48 +03:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2018-06-28 11:36:25 +03:00
|
|
|
return opts.run()
|
2018-06-24 16:40:48 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
f := migrateApplyCmd.Flags()
|
|
|
|
|
2018-06-28 11:36:25 +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.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")
|
2019-03-18 19:40:04 +03:00
|
|
|
f.BoolVar(&opts.skipExecution, "skip-execution", false, "skip executing the migration action, but mark them as applied")
|
2018-09-27 16:57:17 +03:00
|
|
|
|
|
|
|
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
|
2019-02-14 12:37:47 +03:00
|
|
|
f.String("admin-secret", "", "admin secret for Hasura GraphQL Engine")
|
2018-09-27 16:57:17 +03:00
|
|
|
f.String("access-key", "", "access key for Hasura GraphQL Engine")
|
2019-02-14 12:37:47 +03:00
|
|
|
f.MarkDeprecated("access-key", "use --admin-secret instead")
|
2018-09-27 16:57:17 +03:00
|
|
|
|
|
|
|
// need to create a new viper because https://github.com/spf13/viper/issues/233
|
|
|
|
v.BindPFlag("endpoint", f.Lookup("endpoint"))
|
2019-02-14 12:37:47 +03:00
|
|
|
v.BindPFlag("admin_secret", f.Lookup("admin-secret"))
|
2018-09-27 16:57:17 +03:00
|
|
|
v.BindPFlag("access_key", f.Lookup("access-key"))
|
2018-06-24 16:40:48 +03:00
|
|
|
return migrateApplyCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
type migrateApplyOptions struct {
|
|
|
|
EC *cli.ExecutionContext
|
|
|
|
|
|
|
|
upMigration string
|
|
|
|
downMigration string
|
|
|
|
versionMigration string
|
2018-06-28 11:36:25 +03:00
|
|
|
migrationType string
|
2019-03-18 19:40:04 +03:00
|
|
|
skipExecution bool
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2018-06-28 11:36:25 +03:00
|
|
|
func (o *migrateApplyOptions) run() error {
|
2019-03-18 19:40:04 +03:00
|
|
|
migrationType, step, err := getMigrationTypeAndStep(o.upMigration, o.downMigration, o.versionMigration, o.migrationType, o.skipExecution)
|
2018-06-24 16:40:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error validating flags")
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:37:47 +03:00
|
|
|
migrateDrv, err := newMigrate(o.EC.MigrationDir, o.EC.ServerConfig.ParsedEndpoint, o.EC.ServerConfig.AdminSecret, o.EC.Logger, o.EC.Version)
|
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
|
|
|
}
|
2019-03-18 19:40:04 +03:00
|
|
|
migrateDrv.SkipExecution = o.skipExecution
|
2018-06-24 16:40:48 +03:00
|
|
|
|
2018-08-21 21:37:47 +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
|
|
|
|
}
|
2018-09-10 16:21:30 +03:00
|
|
|
if e, ok := err.(*os.PathError); ok {
|
|
|
|
// If Op is first, then log No migrations to apply
|
|
|
|
if e.Op == "first" {
|
|
|
|
o.EC.Logger.Info("No migrations to apply")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 16:40:48 +03:00
|
|
|
return errors.Wrap(err, "apply failed")
|
|
|
|
}
|
|
|
|
o.EC.Logger.Info("migrations applied")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
2019-03-18 19:40:04 +03:00
|
|
|
func getMigrationTypeAndStep(upMigration, downMigration, versionMigration, migrationType 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++
|
|
|
|
}
|
|
|
|
|
|
|
|
if flagCount > 1 {
|
|
|
|
return "", 0, errors.New("Only one migration type can be applied at a time (--up, --down or --goto)")
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return "", 0, errors.Wrap(err, "not a valid input for steps")
|
|
|
|
}
|
|
|
|
return migrationName, step, nil
|
|
|
|
}
|