migrate

package
v0.0.0-...-50c9e83 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MigrateRootCmd = &cobra.Command{
	Use:   "migrate",
	Short: "postgresql migrate tools",
}
View Source
var MigrateUp = &cobra.Command{
	Use:   "up",
	Short: "apply new migrations",
	RunE: func(cmd *cobra.Command, args []string) error {
		log.Info().Msg("starting migrations")
		flags.BindEnv(cmd)
		p := pgx.Postgres{}
		db, err := p.Open(cfg.BuildDSN())
		if err != nil {
			return fmt.Errorf("migrations: new instance: %w", err)
		}
		m, err := migrate.NewWithDatabaseInstance("file://./migrations", "pgx", db)
		if err != nil {
			return fmt.Errorf("migrations: new instance: %w", err)
		}

		version, dirty, err := m.Version()
		if err != nil && err != migrate.ErrNilVersion {
			return fmt.Errorf("cannot get meta info from database. got err: %w", err)
		}

		log.Info().Uint("current_version", version).Bool("dirty", dirty).Msg("current migration status")

		err = m.Up()
		if err != nil && err != migrate.ErrNoChange {
			return fmt.Errorf("migrations: up: %w", err)
		} else if err == migrate.ErrNoChange {
			log.Info().Msg("no migration required. database is already on last schema revision")
			return nil
		}

		newversion, dirty, err := m.Version()
		if err != nil {
			return err
		}
		if dirty {
			return errors.New("database is dirty; manual fix is required")
		}

		log.Info().Uint("new_version", newversion).Msg("successfully migrated")
		return nil
	},
}

Functions

func Execute

func Execute()

Types

type Config

type Config struct {
	Addr     string
	User     string
	Password string
	Database string
}

func (*Config) BuildDSN

func (c *Config) BuildDSN() string

func (*Config) Flags

func (c *Config) Flags(prefix string) *pflag.FlagSet

func (*Config) Validate

func (c *Config) Validate() error

Jump to

Keyboard shortcuts

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