Skip to content

Latest commit

 

History

History
70 lines (53 loc) · 2.2 KB

README.md

File metadata and controls

70 lines (53 loc) · 2.2 KB

pgmigrate

Build Status GoDoc

Pgmigrate performs database migrations. It only supports Postgres and aims to be simple, robust, and verbose.

Why?

There are plenty of other database migration solutions for Go. Over the years they have seen a dramatic increase in the number of databases supported, features, etc. This results in tradeoffs and complexity. I wanted something that was easy to use and debug, built for the known tradeoffs associated with Postgres, so here we are.

Choices

  • It supports only Postgres.
  • It's modeled loosely after ActiveRecord migrations. Migrations are versioned and are applied or reverted in order.
  • Migrations are performed inside of transactions. A migration will either succeed or fail, but will not partially suceed or leave the database in a dirty state.
  • Migrations are defined in code, not files on disk. This is done to ease testing and deployment.

Usage

import "github.com/jcoene/pgmigrate"

// Create a new migrator with a Postgres URL
m := pgmigrate.NewMigrator("postgres://postgres:@127.0.0.1:5432/myapp_development?sslmode=disable")

// Add some migration definitions
m.Add(pgmigrate.Migration{
  Version: 1,
  Name:    "widgets_init",
  Up: `
    create table widgets (
      widget_id integer primary key,
      name text
    );
  `,
  Down: `
    drop table if exists widgets;
  `,
})
m.Add(pgmigrate.Migration{
  Version: 2,
  Name:    "users_init",
  Up: `
    create table users (
      user_id integer primary key,
      name text
    );

    alter table widgets add column user_id integer references widgets;
  `,
  Down: `
    alter table widgets drop column user_id;
    drop table if exists users;
  `,
})

// Run all migrations to reach the desired state. This only applies pending
// migrations and is idempotent (so long as your migrations are sensible).
if err := m.UpAll(); err != nil {
  log.Fatal(err)
}

See the tests or godoc for more details.

License

MIT License, see LICENSE