migrations

package module
v6.5.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2018 License: BSD-2-Clause Imports: 18 Imported by: 0

README

SQL migrations for Golang and PostgreSQL

Build Status GoDoc

This package allows you to run migrations on your PostgreSQL database using Golang Postgres client. See example for details.

Example

You need to create database pg_migrations_example before running this example.

> psql -c "CREATE DATABASE pg_migrations_example"
CREATE DATABASE

> go run *.go version
version is 0

> go run *.go
creating table my_table...
adding id column...
seeding my_table...
migrated from version 0 to 4

> go run *.go version
version is 4

> go run *.go reset
truncating my_table...
dropping id column...
dropping table my_table...
migrated from version 4 to 0

> go run *.go up 2
creating table my_table...
adding id column...
migrated from version 0 to 2

> go run *.go
seeding my_table...
migrated from version 2 to 4

> go run *.go down
truncating my_table...
migrated from version 4 to 3

> go run *.go version
version is 3

> go run *.go set_version 1
migrated from version 3 to 1

> go run *.go create add email to users
created migration 5_add_email_to_users.go

Registering Migrations

migrations.RegisterTx and migrations.MustRegisterTx

Registers migrations to be executed inside transactions.

migrations.Register and migrations.MustRegister

Registers migrations to be executed without any transaction.

Transactions

By default, the migrations are executed outside without any transactions. Individual migrations can however be marked to be executed inside transactions by using the RegisterTx function instead of Register.

Global Transactions
var oldVersion, newVersion int64

err := db.RunInTransaction(func(tx *pg.Tx) (err error) {
    oldVersion, newVersion, err = migrations.Run(tx, flag.Args()...)
    return
})
if err != nil {
    exitf(err.Error())
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultCollection = NewCollection()

Functions

func MustRegister

func MustRegister(fns ...func(DB) error)

func MustRegisterTx

func MustRegisterTx(fns ...func(DB) error)

func Register

func Register(fns ...func(DB) error) error

Register registers new database migration. Must be called from file with name like "1_initialize_db.go", where:

  • 1 - migration version;
  • initialize_db - comment.

func RegisterTx

func RegisterTx(fns ...func(DB) error) error

RegisterTx is just like Register but marks the migration to be executed inside a transaction.

func Run

func Run(db DB, a ...string) (oldVersion, newVersion int64, err error)

Run runs command on the db. Supported commands are: - up [target] - runs all available migrations by default or up to target one if argument is provided. - down - reverts last migration. - reset - reverts all migrations. - version - prints current db version. - set_version - sets db version without running migrations.

func SetTableName

func SetTableName(name string)

func SetVersion

func SetVersion(db DB, version int64) error

func Version

func Version(db DB) (int64, error)

Types

type Collection

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

func NewCollection

func NewCollection(migrations ...*Migration) *Collection

func (*Collection) DisableSQLAutodiscover

func (c *Collection) DisableSQLAutodiscover(flag bool) *Collection

func (*Collection) Migrations

func (c *Collection) Migrations() []*Migration

func (*Collection) MustRegister

func (c *Collection) MustRegister(fns ...func(DB) error)

func (*Collection) MustRegisterTx

func (c *Collection) MustRegisterTx(fns ...func(DB) error)

func (*Collection) Register

func (c *Collection) Register(fns ...func(DB) error) error

Register registers new database migration. Must be called from file with name like "1_initialize_db.go", where:

  • 1 - migration version;
  • initialize_db - comment.

func (*Collection) RegisterTx

func (c *Collection) RegisterTx(fns ...func(DB) error) error

func (*Collection) Run

func (c *Collection) Run(db DB, a ...string) (oldVersion, newVersion int64, err error)

func (*Collection) SetTableName

func (c *Collection) SetTableName(tableName string) *Collection

func (*Collection) SetVersion

func (c *Collection) SetVersion(db DB, version int64) error

func (*Collection) Version

func (c *Collection) Version(db DB) (int64, error)

type DB

type DB interface {
	Model(model ...interface{}) *orm.Query
	Select(model interface{}) error
	Insert(model ...interface{}) error
	Update(model interface{}) error
	Delete(model interface{}) error
	ForceDelete(model interface{}) error

	Exec(query interface{}, params ...interface{}) (orm.Result, error)
	ExecOne(query interface{}, params ...interface{}) (orm.Result, error)
	Query(coll, query interface{}, params ...interface{}) (orm.Result, error)
	QueryOne(model, query interface{}, params ...interface{}) (orm.Result, error)

	Begin() (*pg.Tx, error)

	CopyFrom(r io.Reader, query interface{}, params ...interface{}) (orm.Result, error)
	CopyTo(w io.Writer, query interface{}, params ...interface{}) (orm.Result, error)

	Context() context.Context
	orm.QueryFormatter
}

DB is a common interface for pg.DB and pg.Tx types.

type Migration

type Migration struct {
	Version       int64
	Transactional bool
	Up            func(DB) error
	Down          func(DB) error
}

func RegisteredMigrations

func RegisteredMigrations() []*Migration

RegisteredMigrations returns currently registered Migrations.

func (*Migration) String

func (m *Migration) String() string

Jump to

Keyboard shortcuts

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