akaashanky / migrations

SQL migrations for Golang go-pg and PostgreSQL

Home Page:https://godoc.org/github.com/go-pg/migrations

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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())
}

About

SQL migrations for Golang go-pg and PostgreSQL

https://godoc.org/github.com/go-pg/migrations

License:BSD 2-Clause "Simplified" License


Languages

Language:Go 96.7%Language:AMPL 2.7%Language:Makefile 0.6%