migrate

package
v0.0.0-...-f042570 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2018 License: BSD-2-Clause Imports: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cmd = &command.Command{
	Name: cmdName,
	Cobra: &cobra.Command{
		Use:          cmdName,
		Short:        "Migrate " + buildtime.PROGNAME + " schema",
		SilenceUsage: true,

		PreRunE: func(cmd *cobra.Command, args []string) error {
			return nil
		},

		RunE: func(cmd *cobra.Command, args []string) error {
			log.Info().Str("cmd", cmdName).Msg("")

			var config struct {
				DBConfig db.Config `mapstructure:"db"`
			}
			err := viper.Unmarshal(&config)
			if err != nil {
				log.Fatal().Err(err).Msg("unable to decode config into struct")
			}

			dbPool, err := db.New(config.DBConfig)
			if err != nil {
				log.Fatal().Err(err).Msg("unable to create database pool")
			}

			if err := dbPool.Ping(); err != nil {
				return errors.Wrap(err, "unable to ping database")
			}

			db, err := dbPool.STDDB()
			if err != nil {
				return errors.Wrap(err, "unable to conjur up sql.DB facade")
			}

			source, err := bindata.WithInstance(
				bindata.Resource(migrations.AssetNames(),
					func(name string) ([]byte, error) {
						return migrations.Asset(name)
					}))
			if err != nil {
				return errors.Wrap(err, "unable to create migration source")
			}

			if err := db.Ping(); err != nil {
				return errors.Wrap(err, "unable to ping with stdlib driver")
			}

			driver, err := postgres.WithInstance(db, &postgres.Config{})
			if err != nil {
				return errors.Wrap(err, "unable to create migration driver")
			}

			m, err := migrate.NewWithInstance("file:///migrations/crdb/", source,
				config.DBConfig.Database, driver)
			if err != nil {
				return errors.Wrap(err, "unable to create migration")
			}

			if err := m.Down(); err != nil && err != migrate.ErrNoChange {
				return errors.Wrap(err, "unable to downgrade schema")
			}

			if err := m.Up(); err != nil {
				return errors.Wrap(err, "unable to upgrade schema")
			}

			return nil
		},
	},

	Setup: func(self *command.Command) error {
		return db.SetDefaultViperOptions()
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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