yam

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2018 License: MIT Imports: 2 Imported by: 0

README

CircleCI

YAM (yet another migrator)

There are plenty of migrators out there most of which deal with db schema migrations in some form or another. YAM is another migrator that aims to be light weight in capabilities but heavy in usefulness.

Example:

package main

import (
	"database/sql"

	"github.com/cbelsole/yam"
	_ "github.com/lib/pq"
)

func main() {
	db, err := sql.Open("postgres", "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	migrations := []yam.Migration{
		{
			Version: 0,
			Up: func() error {
				_, err := db.Exec(`CREATE TABLE users(
					id SERIAL PRIMARY KEY,
					name text not null
				);`)
				return err
			},
			Down: func() error {
				_, err := db.Exec("DROP TABLE USERS")
				return err
			},
		},
		{
			Version: 1,
			Up: func() error {
				_, err := db.Exec("INSERT INTO users (name) VALUES ($1);", "gopher")
				return err
			},
			Down: func() error {
				_, err := db.Exec("DELETE FROM users WHERE name = $1;", "gopher")
				return err
			},
		},
	}

	// Running migrations without migrator skips version checks
	if err := yam.Migrate(nil, migrations, 0); err != nil {
		panic(err)
	}
	if err := yam.Rollback(nil, migrations, 0); err != nil {
		panic(err)
	}

	pg, err := yam.NewPostgres("postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable")
	if err != nil {
		panic(err)
	}

	if err = yam.Migrate(pg, migrations, 0); err != nil {
		panic(err)
	}

	pg, err = yam.NewPostgres("postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable")
	if err != nil {
		panic(err)
	}

	if err = yam.Rollback(pg, migrations, 0); err != nil {
		panic(err)
	}

	// NewPostgresFromDB does not clean up the connection. So you can reuse the
	// migrator.
	pg = yam.NewPostgresFromDB(db)
	if err = yam.Migrate(pg, migrations, 0); err != nil {
		panic(err)
	}

	if err = yam.Rollback(pg, migrations, 0); err != nil {
		panic(err)
	}
}

Use cases

  • Seeding data for different environments.
  • Bootstrapping environments with data
  • Running onetime scripts for data migrations

Running tests

# testing without integrations
go test ./... -short

# testing with integrations
docker-compose up
go test ./...

Integrations

  • postgres

Pull requests welcome

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(m Migrator, migrations migrationSlice, offset int64) error

Migrate runs migrations after validating that they have not been run

func NewPostgres

func NewPostgres(url string) (*postgres, error)

NewPostgres creates a new postgres migrator from a new connection which cleans itself up after migrations are complete.

func NewPostgresFromDB

func NewPostgresFromDB(db *sql.DB) *postgres

NewPostgresFromDB creates a new postgres migrator from an existing connection which does not clean itself up after migrations are complete.

func Rollback

func Rollback(m Migrator, migrations migrationSlice, offset int64) error

Rollback runs migrations after validating that they have not been run

Types

type Migration

type Migration struct {
	Version int64
	Up      func() error
	Down    func() error
}

Migration describes how to migrate and roll back changes with a version to keep track of changes

type Migrator

type Migrator interface {
	// contains filtered or unexported methods
}

Jump to

Keyboard shortcuts

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