package commands import ( "fmt" "os" "path/filepath" "strconv" "github.com/hasura/graphql-engine/cli/v2/internal/hasura" "github.com/hasura/graphql-engine/cli/v2/internal/metadatautil" "github.com/hasura/graphql-engine/cli/v2" migrate "github.com/hasura/graphql-engine/cli/v2/migrate" "github.com/pkg/errors" "github.com/spf13/cobra" ) func newMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command { opts := &MigrateApplyOptions{ EC: ec, } migrateApplyCmd := &cobra.Command{ Use: "apply", Short: "Apply migrations on the database", Example: ` # Apply all migrations hasura migrate apply # Use with admin secret: hasura migrate apply --admin-secret "" # Apply migrations on another Hasura instance: hasura migrate apply --endpoint "" # Mark migration as applied on the server and skip execution: hasura migrate apply --skip-execution --version "" # Mark migrations as applied on the server and skip execution: hasura migrate apply --skip-execution --up all # Mark migrations as rollbacked on the server and skip execution: hasura migrate apply --skip-execution --down all # Apply a particular migration version only: hasura migrate apply --version "" # Apply last 2 down migrations: hasura migrate apply --down 2 # Apply only 2 up migrations: hasura migrate apply --up 2 # Apply only a particular version hasura migrate apply --type up --version "" # Apply all up migrations upto version 125, last applied is 100 hasura migrate apply --goto 125 # Apply all down migrations upto version 125, last applied is 150 hasura migrate apply --goto 125 # Rollback a particular version: hasura migrate apply --type down --version "" # Rollback all migrations: hasura migrate apply --down all`, SilenceUsage: true, PreRunE: func(cmd *cobra.Command, args []string) error { return validateConfigV3Flags(cmd, ec) }, RunE: func(cmd *cobra.Command, args []string) error { return opts.Run() }, } f := migrateApplyCmd.Flags() f.SortFlags = false 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.GotoVersion, "goto", "", "apply migration chain up to to the version specified") f.StringVar(&opts.VersionMigration, "version", "", "only apply this particular migration") f.BoolVar(&opts.SkipExecution, "skip-execution", false, "skip executing the migration action, but mark them as applied") f.StringVar(&opts.MigrationType, "type", "up", "type of migration (up, down) to be used with version flag") f.BoolVar(&opts.DryRun, "dry-run", false, "print the names of migrations which are going to be applied") f.BoolVar(&opts.AllDatabases, "all-databases", false, "set this flag to attempt to apply migrations on all databases present on server") f.BoolVar(&opts.ProgressBarLogs, "progressbar-logs", false, "print the logs of progressbar") f.MarkHidden("progressbar-logs") return migrateApplyCmd } type MigrateApplyOptions struct { EC *cli.ExecutionContext UpMigration string DownMigration string VersionMigration string MigrationType string // version up to which migration chain has to be applied GotoVersion string SkipExecution bool DryRun bool Source cli.Source AllDatabases bool ProgressBarLogs bool } func (o *MigrateApplyOptions) Validate() error { if o.EC.Config.Version == cli.V2 { o.Source.Kind = hasura.SourceKindPG o.Source.Name = "" } if o.DryRun && o.SkipExecution { return errors.New("both --skip-execution and --dry-run flags cannot be used together") } if o.DryRun && o.AllDatabases { return errors.New("both --all-databases and --dry-run flags cannot be used together") } if o.EC.Config.Version >= cli.V3 { if !o.AllDatabases && len(o.Source.Name) == 0 { return fmt.Errorf("unable to determine database on which migration should be applied") } if !o.AllDatabases { if len(o.Source.Name) == 0 { return fmt.Errorf("empty database name") } if len(o.Source.Kind) == 0 { // find out the database kind by making a API call to server // and update ec to include the database name and kind sourceKind, err := metadatautil.GetSourceKind(o.EC.APIClient.V1Metadata.ExportMetadata, o.Source.Name) if err != nil { return fmt.Errorf("determining database kind of %s: %w", o.Source.Name, err) } if sourceKind == nil { return fmt.Errorf("error determining database kind for %s, check if database exists on hasura", o.Source.Name) } o.Source.Kind = *sourceKind } } } return nil } type errDatabaseMigrationDirectoryNotFound struct { message string } func (e *errDatabaseMigrationDirectoryNotFound) Error() string { return e.message } func (o *MigrateApplyOptions) Run() error { results, err := o.Apply() if err != nil { return err } for result := range results { if result.Error != nil { o.EC.Logger.Errorf("%v", result.Error) } else if len(result.Message) > 0 { o.EC.Logger.Infof(result.Message) } } return nil } type MigrateApplyResult struct { DatabaseName string Message string Error error } func (o *MigrateApplyOptions) Apply() (chan MigrateApplyResult, error) { resultChan := make(chan MigrateApplyResult) handleError := func(err error) (string, error) { if err == migrate.ErrNoChange { return fmt.Sprintf("nothing to apply on database %s", o.Source.Name), nil } else if e, ok := err.(*os.PathError); ok { // If Op is first, then log No migrations to apply if e.Op == "first" { return fmt.Sprintf("nothing to apply on database %s", o.Source.Name), nil } } else if e, ok := err.(*errDatabaseMigrationDirectoryNotFound); ok { // check if the returned error is a directory not found error // ie might be because a migrations/ directory is not found // if so skip this return "", fmt.Errorf("skipping applying migrations on database %s, encountered: \n%s", o.Source.Name, e.Error()) } else if err != nil { return "", fmt.Errorf("skipping applying migrations on database %s, encountered: \n%v", o.Source.Name, err) } return "", nil } if len(o.Source.Name) == 0 { o.Source = o.EC.Source } if err := o.Validate(); err != nil { return nil, err } if o.AllDatabases && o.EC.Config.Version >= cli.V3 { o.EC.Spin("getting lists of databases from server ") sourcesAndKind, err := metadatautil.GetSourcesAndKind(o.EC.APIClient.V1Metadata.ExportMetadata) o.EC.Spinner.Stop() if err != nil { return nil, err } go func() { defer close(resultChan) for _, source := range sourcesAndKind { result := MigrateApplyResult{ DatabaseName: source.Name, Message: "", Error: nil, } o.Source.Kind = source.Kind o.Source.Name = source.Name err := o.Exec() if err != nil { result.Message, result.Error = handleError(err) } else { result.Message = fmt.Sprintf("migrations applied on database: %s", o.Source.Name) } resultChan <- result } }() } else { go func() { defer close(resultChan) result := MigrateApplyResult{ DatabaseName: o.Source.Name, Message: "", Error: nil, } err := o.Exec() if err != nil { result.Message, result.Error = handleError(err) } else { result.Message = "migrations applied" } resultChan <- result }() } return resultChan, nil } func (o *MigrateApplyOptions) Exec() error { if o.EC.Config.Version >= cli.V3 { // check if a migrations directory exists for source in project migrationDirectory := filepath.Join(o.EC.MigrationDir, o.Source.Name) if f, err := os.Stat(migrationDirectory); err != nil || f == nil { return &errDatabaseMigrationDirectoryNotFound{fmt.Sprintf("expected to find a migrations directory for database %s in %s, but encountered error: %s", o.Source.Name, o.EC.MigrationDir, err.Error())} } } if o.AllDatabases && (len(o.GotoVersion) > 0 || len(o.VersionMigration) > 0) { return fmt.Errorf("cannot use --goto or --version in conjunction with --all-databases") } migrationType, step, err := getMigrationTypeAndStep(o.UpMigration, o.DownMigration, o.VersionMigration, o.MigrationType, o.GotoVersion, o.SkipExecution) if err != nil { return errors.Wrap(err, "error validating flags") } migrateDrv, err := migrate.NewMigrate(o.EC, true, o.Source.Name, o.Source.Kind) if err != nil { return err } migrateDrv.SkipExecution = o.SkipExecution migrateDrv.DryRun = o.DryRun migrateDrv.ProgressBarLogs = o.ProgressBarLogs return ExecuteMigration(migrationType, migrateDrv, step) } // 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, gotoVersion string, skipExecution bool) (string, int64, error) { var flagCount = 0 var stepString = "all" var migrationName = "up" if upMigration != "" { stepString = upMigration flagCount++ } if downMigration != "" { migrationName = "down" stepString = downMigration flagCount++ } if versionMigration != "" { migrationName = "version" stepString = versionMigration if migrationType == "down" { stepString = "-" + stepString } flagCount++ } if gotoVersion != "" { migrationName = "gotoVersion" stepString = gotoVersion flagCount++ } if flagCount > 1 { return "", 0, errors.New("only one migration type can be applied at a time (--up, --down, --type or --goto)") } skipExecutionValid := migrationName == "version" || migrationName == "up" || migrationName == "down" if !skipExecutionValid && skipExecution { return "", 0, errors.New("--skip-execution flag can be set only with --version, --up, --down flags") } 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/version") } return migrationName, step, nil }