...

Package source

import "github.com/golang-migrate/migrate/v4/source"
Overview
Index
Examples
Subdirectories

Overview ▾

Package source provides the Source interface. All source drivers must implement this interface, register themselves, optionally provide a `WithInstance` function and pass the tests in package source/testing.

Variables

var (
    DefaultParse = Parse
    DefaultRegex = Regex
)
var (
    ErrParse = fmt.Errorf("no match")
)

Regex matches the following pattern:

123_name.up.ext
123_name.down.ext
var Regex = regexp.MustCompile(`^([0-9]+)_(.*)\.(` + string(Down) + `|` + string(Up) + `)\.(.*)$`)

func List

func List() []string

List lists the registered drivers

func Register

func Register(name string, driver Driver)

Register globally registers a driver.

type Direction

Direction is either up or down.

type Direction string
const (
    Down Direction = "down"
    Up   Direction = "up"
)

type Driver

Driver is the interface every source driver must implement.

How to implement a source driver?

  1. Implement this interface.
  2. Optionally, add a function named `WithInstance`. This function should accept an existing source instance and a Config{} struct and return a driver instance.
  3. Add a test that calls source/testing.go:Test()
  4. Add own tests for Open(), WithInstance() (when provided) and Close(). All other functions are tested by tests in source/testing. Saves you some time and makes sure all source drivers behave the same way.
  5. Call Register in init().

Guidelines:

type Driver interface {
    // Open returns a a new driver instance configured with parameters
    // coming from the URL string. Migrate will call this function
    // only once per instance.
    Open(url string) (Driver, error)

    // Close closes the underlying source instance managed by the driver.
    // Migrate will call this function only once per instance.
    Close() error

    // First returns the very first migration version available to the driver.
    // Migrate will call this function multiple times.
    // If there is no version available, it must return os.ErrNotExist.
    First() (version uint, err error)

    // Prev returns the previous version for a given version available to the driver.
    // Migrate will call this function multiple times.
    // If there is no previous version available, it must return os.ErrNotExist.
    Prev(version uint) (prevVersion uint, err error)

    // Next returns the next version for a given version available to the driver.
    // Migrate will call this function multiple times.
    // If there is no next version available, it must return os.ErrNotExist.
    Next(version uint) (nextVersion uint, err error)

    // ReadUp returns the UP migration body and an identifier that helps
    // finding this migration in the source for a given version.
    // If there is no up migration available for this version,
    // it must return os.ErrNotExist.
    // Do not start reading, just return the ReadCloser!
    ReadUp(version uint) (r io.ReadCloser, identifier string, err error)

    // ReadDown returns the DOWN migration body and an identifier that helps
    // finding this migration in the source for a given version.
    // If there is no down migration available for this version,
    // it must return os.ErrNotExist.
    // Do not start reading, just return the ReadCloser!
    ReadDown(version uint) (r io.ReadCloser, identifier string, err error)
}

Example

Code:

// see source/stub for an example

// source/stub/stub.go has the driver implementation
// source/stub/stub_test.go runs source/testing/test.go:Test

func Open

func Open(url string) (Driver, error)

Open returns a new driver instance.

type ErrDuplicateMigration

ErrDuplicateMigration is an error type for reporting duplicate migration files.

type ErrDuplicateMigration struct {
    Migration
    os.FileInfo
}

func (ErrDuplicateMigration) Error

func (e ErrDuplicateMigration) Error() string

Error implements error interface.

type Migration

Migration is a helper struct for source drivers that need to build the full directory tree in memory. Migration is fully independent from migrate.Migration.

type Migration struct {
    // Version is the version of this migration.
    Version uint

    // Identifier can be any string that helps identifying
    // this migration in the source.
    Identifier string

    // Direction is either Up or Down.
    Direction Direction

    // Raw holds the raw location path to this migration in source.
    // ReadUp and ReadDown will use this.
    Raw string
}

func Parse

func Parse(raw string) (*Migration, error)

Parse returns Migration for matching Regex pattern.

type Migrations

Migrations wraps Migration and has an internal index to keep track of Migration order.

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

func NewMigrations

func NewMigrations() *Migrations

func (*Migrations) Append

func (i *Migrations) Append(m *Migration) (ok bool)

func (*Migrations) Down

func (i *Migrations) Down(version uint) (m *Migration, ok bool)

func (*Migrations) First

func (i *Migrations) First() (version uint, ok bool)

func (*Migrations) Next

func (i *Migrations) Next(version uint) (nextVersion uint, ok bool)

func (*Migrations) Prev

func (i *Migrations) Prev(version uint) (prevVersion uint, ok bool)

func (*Migrations) Up

func (i *Migrations) Up(version uint) (m *Migration, ok bool)

Subdirectories

Name Synopsis
..
aws_s3
bitbucket
file
github
github_ee
gitlab
go_bindata
examples
migrations
godoc_vfs Package godoc_vfs contains a driver that reads migrations from a virtual file system.
google_cloud_storage
httpfs
iofs Package iofs provides the Go 1.16+ io/fs#FS driver.
pkger
stub
testing Package testing has the source tests.