migration

package module
v0.0.0-...-a9f8cbb Latest Latest
Warning

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

Go to latest
Published: May 22, 2018 License: MIT Imports: 5 Imported by: 0

README

migration

A Go migration library.

Warning

This is a work in progress. As of now this library lacks:

  • Proper documentation
  • Tests
  • Support for anything other than PostgreSQL

Example:

var migrations = []migration.Migration{
	migration.Struct{
		NameString: "20161114105737_init",
		ApplyFunc: func(tx *sql.Tx) error {
			var err error
			var q string
			q = `CREATE TABLE IF NOT EXISTS users (` +
				`id BIGSERIAL PRIMARY KEY` +
				`, login TEXT NOT NULL UNIQUE` +
				`, properties JSONB NOT NULL` +
				`, created_at TIMESTAMP NOT NULL` +
				`, updated_at TIMESTAMP NOT NULL` +
				`)`
			_, err = tx.Exec(q)
			if err != nil {
				return err
			}

			return nil
		},
		RollbackFunc: func(tx *sql.Tx) error {
			var err error
			var q string
			q = `DROP TABLE rose.channels`
			_, err = tx.Exec(q)
			if err != nil {
				return err
			}

			return nil
		},
	},
}

func main() {
	db, err := sql.Open("postgres", "...")
	// ...
	sch := migration.NewSchema(db, migration.DefaultSchemaName, migration.DefaultMigrationTableName)
	err = sch.Init()
	if err != nil {
		log.Fatalf("can't init the db: %v", err)
	}

	migs, err := sch.FindUnapplied(migrations)
	if err != nil {
		log.Fatalf("can't find migrations: %v", err)
	}

	n, err := sch.Apply(migs)
	if err != nil {
		log.Fatalf("can't migrate the db: %v", err)
	}

	log.Printf("applied %d migrations", n)
}

Documentation

Index

Constants

View Source
const DefaultMigrationTableName = "schema_migrations"

DefaultMigrationTableName is the default migrations table name.

View Source
const DefaultSchemaName = "public"

DefaultSchemaName is the default schema name.

Variables

View Source
var ErrMigrationNotFound = errors.New("migration not found")

ErrMigrationNotFound is returned by FindOne when migration is not found by name.

Functions

This section is empty.

Types

type ErrNameNotUnique

type ErrNameNotUnique struct {
	Name string
}

ErrNameNotUnique is returned whenever a non-unique migration name is found.

func (ErrNameNotUnique) Error

func (err ErrNameNotUnique) Error() string

Error implements the error interface for ErrNameNotUnique.

type ErrorPair

type ErrorPair struct {
	Err1, Err2 error
}

ErrorPair is a pair of errors.

func (ErrorPair) Error

func (err ErrorPair) Error() string

Error implements the error interface for ErrorPair.

type Migration

type Migration interface {
	Apply(tx *sql.Tx) error
	Rollback(tx *sql.Tx) error

	Name() string
}

Migration is a migration interface. A migration can apply itself, rollback itself and has a unique name.

func FindByName

func FindByName(migrations []Migration, name string) Migration

FindByName finds a migration by name.

type Schema

type Schema struct {
	// contains filtered or unexported fields
}

Schema is the single database's schema representation.

func NewSchema

func NewSchema(db *sql.DB, schemaName, migTableName string) *Schema

NewSchema returns a new Schema.

func (*Schema) Apply

func (sch *Schema) Apply(migrations []Migration) (n int, err error)

Apply applies all migrations in a single transaction. It returns the number of applied migrations and error if any.

func (*Schema) ApplyEach

func (sch *Schema) ApplyEach(migrations []Migration) (n int, err error)

ApplyEach applies each migration in a separate transaction. It returns the number of applied migrations and error if any.

func (*Schema) FindOne

func (sch *Schema) FindOne(migrations []Migration, name string) (res []Migration, err error)

FindOne finds a migration by name

func (*Schema) FindUnapplied

func (sch *Schema) FindUnapplied(migrations []Migration) (res []Migration, err error)

FindUnapplied finds unapplied migrations.

func (*Schema) FindUnrolled

func (sch *Schema) FindUnrolled(migrations []Migration) (res []Migration, err error)

FindUnrolled finds migrations that were not rolled back.

func (*Schema) Init

func (sch *Schema) Init() error

Init creates a migrations table in the database.

func (*Schema) Rollback

func (sch *Schema) Rollback(migrations []Migration) (n int, err error)

Rollback rolls back all migrations in a single transaction. It returns the number of rolled back migrations and error if any.

func (*Schema) RollbackEach

func (sch *Schema) RollbackEach(migrations []Migration) (n int, err error)

RollbackEach rolls back each migration in a separate transaction. It returns the number of rolled back migrations and error if any.

type Struct

type Struct struct {
	NameString   string
	ApplyFunc    func(tx *sql.Tx) error
	RollbackFunc func(tx *sql.Tx) error
}

Struct is a simple implementation of the Migration interface.

func (Struct) Apply

func (s Struct) Apply(tx *sql.Tx) error

Apply implements Migration for Struct.

func (Struct) Name

func (s Struct) Name() string

Name implements Migration for Struct.

func (Struct) Rollback

func (s Struct) Rollback(tx *sql.Tx) error

Rollback implements Migration for Struct.

Jump to

Keyboard shortcuts

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