github.com/dhax/go-base@v0.0.0-20231004214136-8be7e5c1972b/cmd/migrate.go (about)

     1  package cmd
     2  
     3  import (
     4  	"github.com/spf13/cobra"
     5  
     6  	"github.com/dhax/go-base/database/migrate"
     7  )
     8  
     9  var reset bool
    10  
    11  // migrateCmd represents the migrate command
    12  var migrateCmd = &cobra.Command{
    13  	Use:   "migrate",
    14  	Short: "use go-pg migration tool",
    15  	Long:  `migrate uses go-pg migration tool under the hood supporting the same commands and an additional reset command`,
    16  	Run: func(cmd *cobra.Command, args []string) {
    17  		argsMig := args[:0]
    18  		for _, arg := range args {
    19  			switch arg {
    20  			case "migrate", "--db_debug", "--reset":
    21  			default:
    22  				argsMig = append(argsMig, arg)
    23  			}
    24  		}
    25  
    26  		if reset {
    27  			migrate.Reset()
    28  		}
    29  		migrate.Migrate(argsMig)
    30  	},
    31  }
    32  
    33  func init() {
    34  	RootCmd.AddCommand(migrateCmd)
    35  
    36  	// Here you will define your flags and configuration settings.
    37  
    38  	// Cobra supports Persistent Flags which will work for this command
    39  	// and all subcommands, e.g.:
    40  	// migrateCmd.PersistentFlags().String("foo", "", "A help for foo")
    41  
    42  	// Cobra supports local flags which will only run when this command
    43  	// is called directly, e.g.:
    44  	// migrateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
    45  	migrateCmd.Flags().BoolVar(&reset, "reset", false, "migrate down to version 0 then up to latest. WARNING: all data will be lost!")
    46  }