github.com/zepatrik/pop@v4.13.1+incompatible/soda/cmd/schema/load.go (about)

     1  package schema
     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 loadOptions = struct {
    12  	env   string
    13  	input string
    14  }{}
    15  
    16  // LoadCmd loads a schema.sql file into a database.
    17  var LoadCmd = &cobra.Command{
    18  	Use:   "load",
    19  	Short: "Load a schema.sql file into a database",
    20  	RunE: func(cmd *cobra.Command, args []string) error {
    21  		env := cmd.Flag("env")
    22  		if env == nil {
    23  			return errors.New("env is required")
    24  		}
    25  		loadOptions.env = env.Value.String()
    26  
    27  		f, err := os.Open(loadOptions.input)
    28  		if err != nil {
    29  			return errors.WithMessage(err, "unable to load schema file")
    30  		}
    31  		defer f.Close()
    32  
    33  		c, err := pop.Connect(loadOptions.env)
    34  		if err != nil {
    35  			return errors.WithMessage(err, "unable to connect to database")
    36  		}
    37  		defer c.Close()
    38  
    39  		return c.Dialect.LoadSchema(f)
    40  	},
    41  }
    42  
    43  func init() {
    44  	LoadCmd.Flags().StringVarP(&loadOptions.input, "input", "i", "./migrations/schema.sql", "The path to the schema file you want to load")
    45  }