github.com/duskeagle/pop@v4.10.1-0.20190417200916-92f2b794aab5+incompatible/soda/cmd/migrate.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/gobuffalo/pop"
     7  	"github.com/pkg/errors"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  var migrationPath string
    12  
    13  var migrateCmd = &cobra.Command{
    14  	Use:     "migrate",
    15  	Aliases: []string{"m"},
    16  	Short:   "Runs migrations against your database.",
    17  	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
    18  		RootCmd.PersistentPreRun(cmd, args)
    19  		return os.MkdirAll(migrationPath, 0766)
    20  	},
    21  	RunE: func(cmd *cobra.Command, args []string) error {
    22  		if len(args) > 0 {
    23  			return errors.New("migrate command does not accept any argument")
    24  		}
    25  		mig, err := pop.NewFileMigrator(migrationPath, getConn())
    26  		if err != nil {
    27  			return errors.WithStack(err)
    28  		}
    29  		return mig.Up()
    30  	},
    31  }
    32  
    33  func init() {
    34  	RootCmd.AddCommand(migrateCmd)
    35  	RootCmd.PersistentFlags().StringVarP(&migrationPath, "path", "p", "./migrations", "Path to the migrations folder")
    36  }