pgmigrate

package module
v0.0.0-...-6e93862 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2017 License: MIT Imports: 8 Imported by: 0

README

DEPRECATED - DOT NOT USE

I'm using https://flywaydb.org/ now. It does everything I'd ever want out of a migration tool, except that it's written in Java. Maybe one day I'll port it to Go. But probably not. There is more important stuff to worry about :)

pgmigrate

GoDoc Build Status

pgmigrate implements a minimalistic migration library for postgres.

Example Usage

db, _ := sql.Open("postgres", "postgres://localhost/mydb")
ms, _ := pgmigrate.LoadMigrations(http.Dir("path/to/migrations"))
pgmigrate.DefaultConfig.Migrate(db, ms)

Why this package exists

There are a number of decent database migration libraries available for Go, but I found them to be doing too much in some cases, and too little in others.

Specifically pgmigrate is different because:

  • Only works for postgres: This keeps the code base small and allows leveraging postgres specific features.
  • Executes all migrations in a single transaction: This avoids problems with partially applied migrations.
  • Verifies previously executed migrations have not been modified: This reduces the chance of different environments ending up with different schemas.
  • Does not support down migrations: This might be controversial, but I don't find them very useful. If a database change needs to be rolled back, this can be accomplished by pushing another up migration.
  • No external dependencies: Some other libs force you to transitively depend on client libraries for all the databases they support.
  • Configurable schema/table: Gives you control over where your migration data is stored.
  • Does not ship with a command line client: IMO there are just too many integration scenarios to make a CLI that works for everybody.
  • Supports loading migrations from a virtual http.FileSystem: This works well when using certain libraries that allow bundling static files into your Go binary.

If the tradeoffs above don't work for you, you're probably better off with one of the other libraries.

License

MIT

Documentation

Overview

Package pgmigrate implements a minimalistic migration library for postgres. See README for more information.

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	Schema: "migrations",
	Table:  "migrations",
}

DefaultConfig should be used by most users.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Schema is the name of the postgres schema the migrations table is stored in.
	Schema string
	// Table is the name of the migrations table.
	Table string
}

Config allows to customize pgmigrate. However, most users should use the DefaultConfig.

func (*Config) Migrate

func (c *Config) Migrate(db *sql.DB, ms Migrations) (Migrations, error)

Migrate validates ms, and on success applies any ms that has not already been executed. The return value is either an error, or a list of all migrations that were applied.

type Migration

type Migration struct {
	ID          int
	Description string
	SQL         string
}

Migration holds a migration

func (*Migration) Valid

func (m *Migration) Valid() error

Valid returns an error if the migration is invalid.

type Migrations

type Migrations []Migration

Migrations holds a list of migrations sorted by id. The first migration needs to have ID 1, and each following ID has to be incremented by 1.

func LoadMigrations

func LoadMigrations(dirFS http.FileSystem) (Migrations, error)

LoadMigrations loads all migration files named {{id}}_{{description}}.sql inside dirFS and returns them or an error. The returned Migrations are guaranteed to be sorted, but no validated.

func (Migrations) Len

func (m Migrations) Len() int

Len is part of the sort.Interface.

func (Migrations) Less

func (m Migrations) Less(i, j int) bool

Less is part of the sort.Interface.

func (Migrations) Swap

func (m Migrations) Swap(i, j int)

Swap is part of the sort.Interface.

func (Migrations) Valid

func (m Migrations) Valid() error

Valid returns an error if m holds an invalid migration list.

Jump to

Keyboard shortcuts

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