generate

package
v4.13.2-0...-426b2c9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2020 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigCmd = &cobra.Command{
	Use:              "config",
	Short:            "Generates a database.yml file for your project.",
	PersistentPreRun: func(c *cobra.Command, args []string) {},
	RunE: func(cmd *cobra.Command, args []string) error {
		cflag := cmd.Flag("config")
		cflagVal := ""
		if cflag != nil {
			cflagVal = cflag.Value.String()
		}
		cfgFile := defaults.String(cflagVal, "database.yml")

		run := genny.WetRunner(context.Background())

		pwd, _ := os.Getwd()
		g, err := config.New(&config.Options{
			Root:     pwd,
			Prefix:   filepath.Base(pwd),
			FileName: cfgFile,
			Dialect:  dialect,
		})
		if err != nil {
			return err
		}
		run.With(g)

		return run.Run()
	},
}

ConfigCmd is the command to generate pop config files

View Source
var FizzCmd = &cobra.Command{
	Use:     "fizz [name]",
	Aliases: []string{"migration"},
	Short:   "Generates Up/Down migrations for your database using fizz.",
	RunE: func(cmd *cobra.Command, args []string) error {
		name := ""
		if len(args) > 0 {
			name = args[0]
		}

		var (
			atts attrs.Attrs
			err  error
		)
		if len(args) > 1 {
			atts, err = attrs.ParseArgs(args[1:]...)
			if err != nil {
				return err
			}
		}

		run := genny.WetRunner(context.Background())

		lg := logger.New(logger.DebugLevel)
		run.Logger = lg

		p := cmd.Flag("path")
		path := ""
		if p != nil {
			path = p.Value.String()
		}

		if len(atts) == 0 {
			g, err := cempty.New(&cempty.Options{
				TableName: name,
				Path:      path,
				Type:      "fizz",
			})
			if err != nil {
				return err
			}
			run.With(g)
		} else {
			g, err := ctable.New(&ctable.Options{
				TableName: name,
				Path:      path,
				Type:      "fizz",
				Attrs:     atts,
			})
			if err != nil {
				return err
			}
			run.With(g)
		}

		return run.Run()
	},
}

FizzCmd generates a new fizz migration

View Source
var ModelCmd = &cobra.Command{
	Use:     "model [name]",
	Aliases: []string{"m"},
	Short:   "Generates a model for your database",
	RunE: func(cmd *cobra.Command, args []string) error {
		name := ""
		if len(args) > 0 {
			name = args[0]
		}

		var (
			atts attrs.Attrs
			err  error
		)
		if len(args) > 1 {
			atts, err = attrs.ParseArgs(args[1:]...)
			if err != nil {
				return err
			}
		}

		run := genny.WetRunner(context.Background())

		lg := logger.New(logger.DebugLevel)
		run.Logger = lg

		g, err := gmodel.New(&gmodel.Options{
			Name:                   name,
			Attrs:                  atts,
			Path:                   modelCmdConfig.ModelPath,
			Encoding:               modelCmdConfig.StructTag,
			ForceDefaultID:         true,
			ForceDefaultTimestamps: true,
		})
		if err != nil {
			return err
		}

		run.With(g)

		if !modelCmdConfig.SkipMigration {
			p := cmd.Flag("path")
			path := ""
			if p != nil {
				path = p.Value.String()
			}
			e := cmd.Flag("env")
			var translator fizz.Translator
			if modelCmdConfig.MigrationType == "sql" {
				db, err := pop.Connect(e.Value.String())
				if err != nil {
					return err
				}
				translator = db.Dialect.FizzTranslator()
			}

			g, err = ctable.New(&ctable.Options{
				TableName:              name,
				Attrs:                  atts,
				Path:                   path,
				Type:                   modelCmdConfig.MigrationType,
				Translator:             translator,
				ForceDefaultID:         true,
				ForceDefaultTimestamps: true,
			})
			if err != nil {
				return err
			}
			run.With(g)
		}

		return run.Run()
	},
}

ModelCmd is the cmd to generate a model

View Source
var SQLCmd = &cobra.Command{
	Use:   "sql [name]",
	Short: "Generates Up/Down migrations for your database using SQL.",
	RunE: func(cmd *cobra.Command, args []string) error {
		name := ""
		if len(args) > 0 {
			name = args[0]
		}

		var (
			atts attrs.Attrs
			err  error
		)
		if len(args) > 1 {
			atts, err = attrs.ParseArgs(args[1:]...)
			if err != nil {
				return err
			}
		}

		run := genny.WetRunner(context.Background())

		lg := logger.New(logger.DebugLevel)
		run.Logger = lg

		p := cmd.Flag("path")
		path := ""
		if p != nil {
			path = p.Value.String()
		}
		e := cmd.Flag("env")
		type nameable interface {
			Name() string
		}
		var translator nameable
		db, err := pop.Connect(e.Value.String())
		if err != nil {
			return err
		}
		t := db.Dialect.FizzTranslator()
		if tn, ok := t.(nameable); ok {
			translator = tn
		} else {
			return errors.New("invalid fizz translator")
		}

		if len(atts) == 0 {
			g, err := cempty.New(&cempty.Options{
				TableName:  name,
				Path:       path,
				Type:       "sql",
				Translator: translator,
			})
			if err != nil {
				return err
			}
			run.With(g)
		} else {
			g, err := ctable.New(&ctable.Options{
				TableName:  name,
				Path:       path,
				Type:       "sql",
				Attrs:      atts,
				Translator: t,
			})
			if err != nil {
				return err
			}
			run.With(g)
		}

		return run.Run()
	},
}

SQLCmd generates a SQL migration

Functions

func Config

func Config(cfgFile string, data map[string]interface{}) error

Config generates pop configuration files. Deprecated: use github.com/gobuffalo/pop/genny/config instead.

func GenerateConfig deprecated

func GenerateConfig(cfgFile string, data map[string]interface{}) error

GenerateConfig generates pop configuration files.

Deprecated: use github.com/gobuffalo/pop/genny/config instead.

func Model

func Model(name string, opts map[string]interface{}, attributes []string) error

Model generates new model files to work with pop.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL