pgmigrate

package module
v0.0.0-...-175f806 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2019 License: MIT Imports: 7 Imported by: 0

README

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

Documentation

Overview

Package pgmigrate performs Postgres database migrations. It aims to be simple, robust, and verbose.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Migration

type Migration struct {
	Version int64
	Name    string
	Up      string
	Down    string
	// contains filtered or unexported fields
}

Migration is an individual database migration to be performed.

func (*Migration) String

func (g *Migration) String() string

String returns a string that describes the Migration

type Migrator

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

Migrator contains a database connection and required state to perform migrations.

func NewMigrator

func NewMigrator(url string) *Migrator

NewMigrator creates a new Migrator for the given postgres url.

func (*Migrator) Add

func (m *Migrator) Add(gs ...Migration) *Migrator

Add adds Migrations to the Migrator. This method can be called repeatedly any time before an Up or Down method is called.

func (*Migrator) DownAll

func (m *Migrator) DownAll() error

DownAll applies all applied migrations, if any.

func (*Migrator) DownOne

func (m *Migrator) DownOne() error

DownOne reverts the most recently applied migration, if any.

func (*Migrator) UpAll

func (m *Migrator) UpAll() error

UpAll applies all pending migrations, if any.

func (*Migrator) UpOne

func (m *Migrator) UpOne() error

UpOne applies the next pending migration, if any.

Jump to

Keyboard shortcuts

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