Giter VIP home page Giter VIP logo

migrations's Introduction

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.

Installation

go get -u github.com/go-pg/migrations

Usage

To run migrations on your project you should fulfill the following steps:

  1. define the migration list;
  2. implement an executable app that calls migration tool;
  3. run migrations.

Define Migrations

Migration Files

You can save SQL migration files at the same directory as your main.go file, they should have proper file extensions (more about migration files).

Registered Migrations

Migrations can be registered in the code using migrations.RegisterTx and migrations.MustRegisterTx functions. More details about migration registering.

Implement app to run the tool

You can run migrations from any place of your app or ecosystem. It can be a standalone application of a part of a big program, or maybe an HTTP handler, etc. Check example for some helpful information about practical usage.

Run Migrations

Run migration tool by providing CLI arguments to the migrations.Run function.

Currently, the following arguments are supported:

  • up - runs all available migrations;
  • up [target] - runs available migrations up to the target one;
  • down - reverts last migration;
  • reset - reverts all migrations;
  • version - prints current db version;
  • set_version [version] - sets db version without running migrations.

Example

You need to create database pg_migrations_example before running the example.

> cd example

> psql -c "CREATE DATABASE pg_migrations_example"
CREATE DATABASE

> go run *.go init
version is 0

> 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.

SQL migrations

SQL migrations are automatically picked up if placed in the same folder with main.go or Go migrations. SQL migrations must have one of the following extensions:

  • .up.sql - up migration;
  • .down.sql - down migration;
  • .tx.up.sql - transactional up migration;
  • .tx.down.sql - transactional down migration.

By default SQL migrations are executed as single PostgreSQL statement. --gopg:split directive can be used to split migration into several statements:

SET statement_timeout = 60000;
SET lock_timeout = 60000;

--gopg:split

CREATE INDEX CONCURRENTLY ...;

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

migrations's People

Contributors

akaashanky avatar alecgorge avatar anmic avatar bentranter avatar betrok avatar bithavoc avatar firstrow avatar ilyakaznacheev avatar jgiles avatar owais avatar quentinvernot avatar smcdonald45 avatar vmihailenco avatar wkhere avatar zapic0 avatar zeroviscosity avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.