Skip to content

Postgres database migrations that are simple, robust, and verbose.

License

Notifications You must be signed in to change notification settings

jcoene/pgmigrate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Postgres database migrations that are simple, robust, and verbose.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages