migration

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2016 License: Apache-2.0 Imports: 9 Imported by: 21

README

Migration

GoDoc wercker status Coverage Status

Simple and pragmatic migrations for Go applications.

Features

  • Super simple driver interface to allow easy implementation for more database/migration drivers.
  • Embeddable migration files.
  • Support for up/down migrations.
  • Atomic migrations (where possible, depending on database support).

Drivers

  • Apache Phoenix
  • MySQL
  • PostgreSQL
  • SQLite

Quickstart

// Create migration source
assetMigration := &migration.AssetMigrationSource{
    Asset:    Asset,
    AssetDir: AssetDir,
    Dir:      "test-migrations",
}

// Create driver
driver, err := migration.NewPhoenix("http://localhost:8765")

// Run all up migrations
applied, err := Migrate(driver, assetMigration, migration.Up, 0)

// Remove the last 2 migrations
applied, err := Migrate(driver, assetMigration, migration.Down, 2)

Writing migrations

Migrations are extremely simple to write:

  • Separate your up and down migrations into different files. For example, 1_init.up.sql and 1_init.down.sql.
  • Prefix your migration with a number or timestamp for versioning: 1_init.up.sql or 1475813115_init.up.sql.
  • The file-extension can be anything you want, but must be present. For example, 1_init.up.sql is valid, but 1_init.up is not,

Let's say we want to write our first migration to initialize the database.

In that case, we would have a file called 1_init.up.sql containing SQL statements for the up migration:

CREATE TABLE test_data (
  id BIGINT NOT NULL PRIMARY KEY,
)

We also create a 1_init.down.sql file containing SQL statements for the down migration:

DROP TABLE IF EXISTS test_data

Embedding migration files

We use go-bindata to embed migration files. In the simpliest case, assuming your migration files are in migrations/, just run:

go-bindata -o bindata.go -pkg myapp migrations/

Then, use AssetMigrationSource to find the migrations:

assetMigration := &migration.AssetMigrationSource{
    Asset:    Asset,
    AssetDir: AssetDir,
    Dir:      "test-migrations",
}

The Asset and AssetDir functions are generated by go-bindata.

TODO (Pull requests welcomed!)

  • Command line program to run migrations
  • Support using .go migration files.
  • MigrationSource that uses migrations from the local file system
  • More drivers

Why yet another migration library?

We wanted a migration library with the following features:

  • Open to extension for all sorts of databases, not just database/sql drivers or an ORM.
  • Easily embeddable in a Go application.
  • Support for embedding migration files directly into the app.

We narrowed our focus down to 2 contenders: sql-migrate and migrate

sql-migrate leans heavily on the gorp ORM library to perform migrations. Unfortunately, this means that we were restricted to databases supported by gorp. It is easily embeddable in a Go app and supports embedding migration files directly into the Go binary. If database support was a bit more flexible, we would have gone with it.

migrate is highly extensible, and adding support for another database is extremely trivial. However, due to it using the scheme in the dsn to determine which database driver to use, it prevented us from easily implementing an Apache Phoenix driver, which uses the scheme to determine if we should connect over http or https. Due to the way the project is structured, it was also almost impossible to add support for embeddable migration files without major changes.

License

This library is licensed under the Apache 2 License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(driver Driver, migrations Source, direction Direction, max int) (int, error)

Migrate runs a migration using a given driver and MigrationSource. The direction defines whether the migration is up or down, and max is the maximum number of migrations to apply. If max is set to 0, then there is no limit on the number of migrations to apply.

Types

type AssetMigrationSource

type AssetMigrationSource struct {
	// Asset should return content of file in path if exists
	Asset func(path string) ([]byte, error)

	// AssetDir should return list of files in the path
	AssetDir func(path string) ([]string, error)

	// Path in the bindata to use.
	Dir string
}

AssetMigrationSource is a MigrationSource that uses migration files embedded in a Go application.

func (AssetMigrationSource) GetMigrationFile

func (a AssetMigrationSource) GetMigrationFile(name string) (io.Reader, error)

func (AssetMigrationSource) ListMigrationFiles

func (a AssetMigrationSource) ListMigrationFiles() ([]string, error)

type Direction added in v0.2.0

type Direction int
const (
	Up Direction = iota
	Down
)

type Driver

type Driver interface {

	// Close is the last function to be called.
	// Close any open connection here.
	Close() error

	// Migrate is the heart of the driver.
	// It will receive a PlannedMigration which the driver should apply
	// to its backend or whatever.
	Migrate(migration *PlannedMigration) error

	// Version returns all applied migration versions
	Versions() ([]string, error)
}

Driver is the interface type that needs to implemented by all drivers.

type MemoryMigrationSource

type MemoryMigrationSource struct {
	Files map[string]string
}

MemoryMigrationSource is a MigrationSource that uses migration sources in memory. It is mainly used for testing.

func (MemoryMigrationSource) GetMigrationFile

func (m MemoryMigrationSource) GetMigrationFile(name string) (io.Reader, error)

func (MemoryMigrationSource) ListMigrationFiles

func (m MemoryMigrationSource) ListMigrationFiles() ([]string, error)

type Migration

type Migration struct {
	ID   string
	Up   string
	Down string
}

Migration represents a migration, containing statements for migrating up and down.

func (Migration) Less

func (m Migration) Less(other *Migration) bool

Less compares two migrations to determine how they should be ordered.

func (Migration) NumberPrefixMatches

func (m Migration) NumberPrefixMatches() []string

func (Migration) VersionInt

func (m Migration) VersionInt() int64

VersionInt converts the migration version to an 64-bit integer.

type PlannedMigration

type PlannedMigration struct {
	*Migration
	Direction Direction
}

PlannedMigration is a migration with a direction defined. This allows the driver to work out how to apply the migration.

type Source added in v0.2.0

type Source interface {
	ListMigrationFiles() ([]string, error)
	GetMigrationFile(file string) (io.Reader, error)
}

Source is an interface that defines how a source can find and read migration files.

Directories

Path Synopsis
driver
golang Module

Jump to

Keyboard shortcuts

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