Giter VIP home page Giter VIP logo

migrate's Introduction

migrate

Build Status GoDoc Coverage Status

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

go get -u -tags 'postgres' -o migrate github.com/mattes/migrate/cli

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

Databases

Database drivers are responsible for applying migrations to databases. Implementing a new database driver is easy. Just implement database/driver interface

Migration Sources

Source Drivers read migrations from various locations. Implementing a new source driver is easy. Just implement the source/driver interface.

CLI usage

# dowload, build and install the CLI tool
# -tags takes database and source drivers and will only build those
$ go get -u -tags 'postgres' -o migrate github.com/mattes/migrate/cli

$ migrate -help
Usage: migrate OPTIONS COMMAND [arg...]
       migrate [ -version | -help ]

Options:
  -source      Location of the migrations (driver://url)
  -path        Shorthand for -source=file://path
  -database    Run migrations against this database (driver://url)
  -prefetch N  Number of migrations to load in advance before executing (default 10)
  -verbose     Print verbose logging
  -version     Print version
  -help        Print usage

Commands:
  goto V       Migrate to version V
  up [N]       Apply all or N up migrations
  down [N]     Apply all or N down migrations
  drop         Drop everyting inside database
  version      Print current migration version


# so let's say you want to run the first two migrations
migrate -database postgres://localhost:5432/database up 2

# if your migrations are hosted on github
migrate -source github://mattes:personal-access-token@mattes/migrate_test \
  -database postgres://localhost:5432/database down 2

Use in your Go project

import (
  "github.com/mattes/migrate/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)
}

Migration files

Each migration version has an up and down migration.

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

Development, Testing and Contributing

  1. Make sure you have a running Docker daemon (Install for MacOS)
  2. Fork this repo and git clone somewhere to $GOPATH/src/github.com/%you%/migrate
  3. make rewrite-import-paths to update imports to your local fork
  4. Confirm tests are working: make test-short
  5. Write awesome code ...
  6. make test to run all tests against all database versions
  7. make restore-import-paths to restore import paths
  8. Push code and open Pull Request

Some more notes:

  • You can specify which database/ source tests to run:
    make test-short SOURCE='file go-bindata' DATABASE='postgres cassandra'
  • After make test, run make html-coverage which opens a shiny test coverage overview.
  • Missing imports? make deps
  • make build-cli builds the CLI in directory cli/build/.
  • make list-external-deps lists all external dependencies for each package

Alternatives

migrate's People

Contributors

balboah avatar benma avatar bfosberry avatar buddhamagnet avatar c4milo avatar carlosforero avatar cgilling avatar cjwirth avatar dacamp avatar daddykotex avatar dereulenspiegel avatar dimag-jfrog avatar dinedal avatar divoxx avatar drboyer avatar goenning avatar jamiecuthill avatar jmhodges avatar johnweldon avatar justinas avatar lepoetemaudit avatar mattes avatar meehow avatar ngauthier avatar raizyr avatar raphaeljlps avatar skeswa avatar trietphm avatar uzimonkey avatar wlcx avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.