migrator

package module
v0.0.0-...-933217b Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2018 License: MPL-2.0 Imports: 7 Imported by: 2

README

Migrator

GoDoc Build Status

Opinionated database migration library for Go applications.

Supported databases
  • Postgres

When building your project using this library, make sure you pass build tags to compile only the driver you want to use. Example: go build -tags postgres or go test -tags postgres

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBadFilenameFormat is returned if a migration file does not conform
	// with the expected name patter. Ex: 001_my-migration_up.sql
	ErrBadFilenameFormat = errors.New("bad-filename-format")
	// ErrInvalidDirection is returned if the direction of a migration is other
	// than 'up' or 'down'
	ErrInvalidDirection = errors.New("invalid-migration-direction")
	// ErrDBNotSupported is returned when trying to create a migrator instance
	// for an unsupported database.
	ErrDBNotSupported = errors.New("database-not-supported")
	// ErrInvalidDB is returned when a nil *sql.DB pointer is passed to NewMigrator
	ErrInvalidDB = errors.New("invalid-database-handle")
	// ErrMigrationFailed is returned when a migration failed to run
	ErrMigrationFailed = errors.New("migration-failed")
)

Functions

This section is empty.

Types

type AssetDirFunc

type AssetDirFunc func(path string) ([]string, error)

AssetDirFunc is the type that defines the function to access specific embedded dirs

type AssetFunc

type AssetFunc func(path string) ([]byte, error)

AssetFunc is the type that defines the function to access specific embedded files

type DBType

type DBType string

DBType defines a type for specifying the databasse to use during migration.

const (
	Postgres DBType = "postgres"
)

Supported databases.

type Migration

type Migration struct {
	ID        string
	Name      string
	Filename  string `db:"filename"`
	Up        string
	Down      string
	Status    string
	CreatedAt time.Time `db:"created_at"`
	UpdatedAt time.Time `db:"updated_at"`
}

Migration represents an actual migration file.

func DecodeFile

func DecodeFile(f string, assetFunc AssetFunc) (*Migration, error)

DecodeFile takes a sql file and returns a Migration instance

type Migrator

type Migrator interface {
	// Init initializes migrations table in the given database.
	Init() error
	// Migrate applies all migrations that hasn't been applied.
	Migrate() error
	// Redo undos specific migrations and applies them again. By default
	// if no parameter is specified, it will redo the latest migration.
	Redo(n ...uint) error
	// Rollback reverts the last migration if not parameter is specified.
	Rollback(n ...uint) error
	// Migrations returns the list of migrations currently applied to the database.
	Migrations(ids ...string) ([]*Migration, error)
	// Up applies a specific migration version.
	Up(version string) error
	// Down rolls back or takes down a specific migration version.
	Down(version string) error
}

Migrator defines the set of functions used by this package.

func NewMigrator

func NewMigrator(db *sql.DB, dbType DBType, assetFunc AssetFunc, assetDirFunc AssetDirFunc) (Migrator, error)

NewMigrator creates a new instance of the migration process, based on the database type provided.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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