migrate

package module
v0.0.0-...-368d26c Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2020 License: MIT Imports: 11 Imported by: 0

README

Go migrations

A very simple migration library for your Go projects.

Features

  • Any database/sql drivers should work. See https://github.com/golang/go/wiki/SQLDrivers

  • Each migration is run in a transaction so there should be no 'dirty' state.

  • Only migrates "up". I have never used a "down" migration.

  • Enables a callback function to suit whatever logging implementation you choose.

Usage

import "src.userspace.com.au/go-migrate"
// or import "github.com/felix/go-migrate"

db, err := sql.Open("pgx", uri)
//db, err := sql.Open("sqlite3", uri)
if err != nil {
    return err
}
defer db.Close()

// Relative path to migration files
migrator, err := migrate.NewFileMigrator(db, "file://migrations/")
if err != nil {
    return fmt.Errorf("failed to create migrator: %s", err)
}

// Migrate all the way
err = migrator.Migrate()
if err != nil {
    return fmt.Errorf("failed to migrate: %s", err)
}

v, err := migrator.Version()
fmt.Printf("database at version %d\n", v)

Documentation

Index

Constants

View Source
const (
	// NilVersion is a Claytons version
	// "the version you are at when you are not at a version"
	NilVersion = -1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Migration

type Migration interface {
	// The version of this migration
	Version() int64
	// Run the migration
	Run(*sql.Tx) error
}

Migration interface

type Migrator

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

A Migrator collates and runs migrations

func NewFileMigrator

func NewFileMigrator(db *sql.DB, path string, opts ...Option) (*Migrator, error)

NewFileMigrator creates a new set of migrations from a path Each one is run in a transaction.

func NewStringMigrator

func NewStringMigrator(db *sql.DB, src []string, opts ...Option) (*Migrator, error)

NewStringMigrator creates a new set of migrations from a slice of strings Each one is run in a transaction.

func (*Migrator) Migrate

func (m *Migrator) Migrate() error

Migrate migrates the database to the highest possible version

func (*Migrator) MigrateTo

func (m *Migrator) MigrateTo(toVersion int64) error

MigrateTo migrates the database to the specified version

func (*Migrator) Version

func (m *Migrator) Version() (int64, error)

Version reports the current version of the database

type Option

type Option func(*Migrator) error

An Option configures a migrator

func SetCallback

func SetCallback(cb ResultFunc) Option

SetCallback configures the table used for recording the schema version

func SetVersionTable

func SetVersionTable(vt string) Option

SetVersionTable configures the table used for recording the schema version

type ResultFunc

type ResultFunc func(int64, int64, error)

ResultFunc is the callback signature

Source Files

  • file.go
  • migrate.go
  • options.go
  • string.go

Jump to

Keyboard shortcuts

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