Merge pull request #231 from mattes/cli-updates

build cli with all source and database drivers
tree: fe595a09e75e5c6485f2a12c3205904ad7480e9f
  1. cli/
  2. database/
  3. source/
  4. testing/
  5. .gitignore
  6. .travis.yml
  7. CONTRIBUTING.md
  8. FAQ.md
  9. LICENSE
  10. log.go
  11. Makefile
  12. migrate.go
  13. migrate_test.go
  14. migration.go
  15. migration_test.go
  16. MIGRATIONS.md
  17. README.md
  18. util.go
  19. util_test.go
README.md

Build Status GoDoc Coverage Status packagecloud.io

migrate

Database migrations written in Go. Use as CLI or import as library.

  • Migrate reads migrations from sources and applies them in correct order to a database.
  • Drivers are “dumb”, migrate glues everything together and makes sure the logic is bulletproof.
    (Keeps the drivers lightweight, too.)
  • Database drivers don't assume things or try to correct user input. When in doubt, fail.

Looking for v1?

Databases

Database drivers run migrations. Add a new database?

Migration Sources

Source drivers read migrations from local or remote sources. Add a new source?

CLI usage

  • Simple wrapper around this library.
  • Handles ctrl+c (SIGINT) gracefully.
  • No config search paths, no config files, no magic ENV var injections.

CLI Documentation

(brew todo #156)

$ brew install migrate --with-postgres
$ migrate -database postgres://localhost:5432/database up 2

Use in your Go project

  • API is stable and frozen for this release (v3.x).
  • Package migrate has no external dependencies.
  • Only import the drivers you need. (check dependency_tree.txt for each driver)
  • To help prevent database corruptions, it supports graceful stops via GracefulStop chan bool.
  • Bring your own logger.
  • Uses io.Reader streams internally for low memory overhead.
  • Thread-safe and no goroutine leaks.

Go Documentation

import (
    "github.com/mattes/migrate"
    _ "github.com/mattes/migrate/database/postgres"
    _ "github.com/mattes/migrate/source/github"
)

func main() {
    m, err := migrate.New(
        "github://mattes:personal-access-token@mattes/migrate_test",
        "postgres://localhost:5432/database?sslmode=enable")
    m.Steps(2)
}

Want to use an existing database client?

import (
    "database/sql"
    _ "github.com/lib/pq"
    "github.com/mattes/migrate"
    "github.com/mattes/migrate/database/postgres"
    _ "github.com/mattes/migrate/source/file"
)

func main() {
    db, err := sql.Open("postgres", "postgres://localhost:5432/database?sslmode=enable")
    driver, err := postgres.WithInstance(db, &postgres.Config{})
    m, err := migrate.NewWithDatabaseInstance(
        "file:///migrations",
        "postgres", driver)
    m.Steps(2)
}

Migration files

Each migration has an up and down migration. Why?

1481574547_create_users_table.up.sql
1481574547_create_users_table.down.sql

Best practices: How to write migrations.

Development and Contributing

Yes, please! Makefile is your friend, read the development guide.

Also have a look at the FAQ.


Alternatives

https://bitbucket.org/liamstask/goose, https://github.com/tanel/dbmigrate,
https://github.com/BurntSushi/migration, https://github.com/DavidHuie/gomigrate,
https://github.com/rubenv/sql-migrate