Database migrations. CLI and Golang library.

CircleCI - Build Status GoDoc Coverage Status packagecloud.io Docker Pulls Supported Go Versions GitHub Release Go Report Card

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.

Forked from mattes/migrate

Databases

Database drivers run migrations. Add a new database?

Database URLs

Database connection strings are specified via URLs. The URL format is driver dependent but generally has the form: dbdriver://username:password@host:port/dbname?param1=true&param2=false

Any reserved URL characters need to be escaped. Note, the % character also needs to be escaped

Explicitly, the following characters need to be escaped: !, #, $, %, &, ', (, ), *, +, ,, /, :, ;, =, ?, @, [, ]

It's easiest to always run the URL parts of your DB connection URL (e.g. username, password, etc) through an URL encoder. See the example Python snippets below:

$ python3 -c 'import urllib.parse; print(urllib.parse.quote(input("String to encode: "), ""))'
String to encode: FAKEpassword!#$%&'()*+,/:;=?@[]
FAKEpassword%21%23%24%25%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D
$ python2 -c 'import urllib; print urllib.quote(raw_input("String to encode: "), "")'
String to encode: FAKEpassword!#$%&'()*+,/:;=?@[]
FAKEpassword%21%23%24%25%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D
$

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

Basic usage

$ migrate -source file://path/to/migrations -database postgres://localhost:5432/database up 2

Docker usage

$ docker run -v {{ migration dir }}:/migrations --network host migrate/migrate
    -path=/migrations/ -database postgres://localhost:5432/database up 2

Use in your Go project

  • API is stable and frozen for this release (v3 & v4).
  • Uses Go modules to manage dependencies.
  • 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/golang-migrate/migrate/v4"
    _ "github.com/golang-migrate/migrate/v4/database/postgres"
    _ "github.com/golang-migrate/migrate/v4/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/golang-migrate/migrate/v4"
    "github.com/golang-migrate/migrate/v4/database/postgres"
    _ "github.com/golang-migrate/migrate/v4/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)
}

Getting started

Go to getting started

Tutorials

(more tutorials to come)

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.

Versions

Version Supported? Import Notes
master import "github.com/golang-migrate/migrate/v4" New features and bug fixes arrive here first
v4 import "github.com/golang-migrate/migrate/v4" Used for stable releases
v3 import "github.com/golang-migrate/migrate" (with package manager) or import "gopkg.in/golang-migrate/migrate.v3" (not recommended) DO NOT USE - No longer supported

Development and Contributing

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

Also have a look at the FAQ.


Looking for alternatives? https://awesome-go.com/#database.

Comments
  • Getting

    Getting "error: file does not exist"

    We have run the migration successfully, and now i need to run it again in case there are any new migrations.

    This is he command we are using:

    migrate -database 'mysql://xxx:yyyy@/zzzz' -source file://go/src/github.com/me/myproj.org/db/schema up

    I am in the root directory of the user (i.e. /home/myuser), and underneath is the structure: go/src/github.com/me/myproj.org/db/schema which contains files like:

    201802221525_init_schema.down.sql 201805211743_us_states.down.sql 201805291420_countries.down.sql 201806051200_currencies.down.sql 201802221525_init_schema.up.sql 201805211743_us_states.up.sql 201805291420_countries.up.sql 201806051200_currencies.up.sql etc.

    We see that with mattes migrate, there were many similar issues with no obvious resolution.

    in the db in schema_migrations table, I see it says version 201807191200, although i am not sure this has any bearing on the issue.

    If I change the file path to something which doesn't exist, e.g.

    migrate -database 'mysql://xxx:yyyy@/zzzz' -source file://go/src/github.com/me/myproj.org/notexist up

    I get a different error message:

    error: open /home/muyuser/go/src/github.com/me/myproj.org/notexist : no such file or directory

    This is how we installed migrate:

    wget https://github.com/golang-migrate/migrate/releases/download/v3.2.0/migrate.linux-amd64.tar.gz gunzip migrate.linux-amd64.tar.gz tar xvf migrate.linux-amd64.tar mv migrate.linux-amd64 /usr/bin/migrate

  • Dirty database version 1. Fix and force version

    Dirty database version 1. Fix and force version

    the error appears as below when migrate timeout:

    Dirty database version 1. Fix and force version
    

    Steps to Reproduce Steps to reproduce the behavior:

    1. do migrate timeout
    2. migrate again

    Expected Behavior migrate again could automatically solve this problem and do migrate normally.

  • Postgres - Add schema name support in x-migrations-table value (#95)

    Postgres - Add schema name support in x-migrations-table value (#95)

    @dhui what do you think about this commit which add x-schema-name URL Query support as suggest by this comment?

    Is anything missing to approve this Pull Request? Some documention, unit test or other?

    Best regards,
    Stéphane


    Edit:

    The second version of this patch add schema name support in x-migrations-table value.

    Now, you can use:

    migrate ... -database ...?x-migrations-table=gomigrate.schema_migrations
    

    Edit: alternative implementation: https://github.com/golang-migrate/migrate/pull/533

  • feat: embed, io/fs support

    feat: embed, io/fs support

    Is your feature request related to a problem? Please describe.

    The next Go 1.16 will add official file system interface and file embedding.

    https://github.com/golang/go/issues/41190 https://github.com/golang/go/tree/master/src/io/fs

    https://github.com/golang/go/issues/41191 https://github.com/golang/go/tree/master/src/embed

    It would be great if golang-migrate supports these as source.Driver.

    It replaces statik, go-bindata, and pkger. Of course, this does not mean that they cannot be used.

    Describe the solution you'd like

    Describe alternatives you've considered

    Additional context

    We will also need to use the build tag properly.

  • fix example of install by go toolchain at README

    fix example of install by go toolchain at README

    Currently, result of install by go toolchain as follows

    > go get -tags 'postgres' -u github.com/golang-migrate/migrate/cmd/migrate
    go: finding github.com/golang-migrate/migrate/cmd/migrate latest
    go: finding github.com/golang-migrate/migrate/cmd latest
    go get github.com/golang-migrate/migrate/cmd/migrate: no matching versions for query "latest"
    

    So, I fixed Installation->With Go toolchain.

  • postgres: Move lock out of ensureVersionTable, for consistency with other SQL operations

    postgres: Move lock out of ensureVersionTable, for consistency with other SQL operations

    Fixes #164 .

    ensureVersionTable seems to be the only SQL operation in the postgres driver which handles locking of database itself, meaning it seems to break the convention of having the caller responsible for locking and not the callee.

    This removes the locking logic from ensureVersionTable and makes sure to properly lock the database before calling ensureVersionTable from the WithInstance method.

  • adding /v3/ to urls is a breaking change when building.

    adding /v3/ to urls is a breaking change when building.

    When building the project there is no such place as

    ```https://github.com/golang-migrate/migrate/v3/``

    so the build cannot import its dependencies and the build fails.

    Steps to Reproduce

    Build the project as below

    go build -tags 'postgres' -o ~/go/bin/migrate github.com/golang-migrate/migrate/cli

    Expected Behavior I expect the binary to be built and deployed to $GOPATH/bin/migrate

    Migrate Version v3.5.2

  • Postgres - add x-migrations-table-quoted url query option (#95)

    Postgres - add x-migrations-table-quoted url query option (#95)

    Following this comment, this is a new Pull Request.

    By default, gomigrate quote migrations table name, if x-migrations-table-quoted is enabled, then you must to quote migrations table name manually, for instance "gomigrate"."schema_migrations".

    I have used this playground to test this branch.

  • Packr Box as migration source.

    Packr Box as migration source.

    Is your feature request related to a problem? Please describe. go-bindata has been left unmaintained, unwillingly or otherwise. See https://github.com/jteeuwen/go-bindata/issues/5

    This presents a problem as we're using go-bindata to embed our migration files into our Go binaries. The next best alternative is https://github.com/gobuffalo/packr

    Describe the solution you'd like Migration source support for packr boxes.

    Describe alternatives you've considered N/A

    Additional context N/A

  • mysql database registered twice

    mysql database registered twice

    Step to reproduce Execute the sample code of this page: https://github.com/golang-migrate/migrate/tree/master/database/mysql

    Output It gives the following error:

    panic: sql: Register called twice for driver mysql
    
    goroutine 1 [running]:
    database/sql.Register(0x127dc81, 0x5, 0x12a7a20, 0x13d2508)
    	/usr/local/Cellar/go/1.10.3/libexec/src/database/sql/sql.go:50 +0x1a4
    github.com/golang-migrate/migrate/vendor/github.com/go-sql-driver/mysql.init.0()
    	/Users/martin/go/src/github.com/golang-migrate/migrate/vendor/github.com/go-sql-driver/mysql/driver.go:168 +0x5c
    exit status 2
    

    Root cause The migrate/database/mysql package uses github.com/go-sql-driver/mysql which already registers the "mysql" driver. So when migrate/database/mysql tries to register it as well, it creates this error.

  • goreleaser configuration and automatic release workflow

    goreleaser configuration and automatic release workflow

    close #74

    Required settings

    GitHub Actions secrets

    • DOCKERHUB_USERNAME
    • DOCKERHUB_PASSWORD
    • PACKAGECLOUD_TOKEN

    How to release

    I imagined as much as possible what the existing release workflow would look like, and followed the steps below. If you don't like it, please send comments. We can also automatically generate a change log for the release notes.

    1. push a new tag (manual)
    2. publish draft release (automatic)
    3. edit draft release and publish release (manual)
  • Problematic alter column migration script fails with crdb 22.2

    Problematic alter column migration script fails with crdb 22.2

    Is your feature request related to a problem? Please describe. Altering column type seems cannot run within a transaction in crd, doing so results following error: [0A000] ERROR: unimplemented: ALTER COLUMN TYPE is not supported inside a transaction Hint: You have attempted to use a feature that is not yet implemented. See: https://go.crdb.dev/issue-v/49351/v22.2

    Describe the solution you'd like Would be good to tell migration tool do not run this file within a tx

    Describe alternatives you've considered No solution yet

    Additional context n/a

  • Bump github.com/aws/aws-sdk-go from 1.17.7 to 1.33.0

    Bump github.com/aws/aws-sdk-go from 1.17.7 to 1.33.0

    Bumps github.com/aws/aws-sdk-go from 1.17.7 to 1.33.0.

    Changelog

    Sourced from github.com/aws/aws-sdk-go's changelog.

    Release v1.33.0 (2020-07-01)

    Service Client Updates

    • service/appsync: Updates service API and documentation
    • service/chime: Updates service API and documentation
      • This release supports third party emergency call routing configuration for Amazon Chime Voice Connectors.
    • service/codebuild: Updates service API and documentation
      • Support build status config in project source
    • service/imagebuilder: Updates service API and documentation
    • service/rds: Updates service API
      • This release adds the exceptions KMSKeyNotAccessibleFault and InvalidDBClusterStateFault to the Amazon RDS ModifyDBInstance API.
    • service/securityhub: Updates service API and documentation

    SDK Features

    • service/s3/s3crypto: Introduces EncryptionClientV2 and DecryptionClientV2 encryption and decryption clients which support a new key wrapping algorithm kms+context. (#3403)
      • DecryptionClientV2 maintains the ability to decrypt objects encrypted using the EncryptionClient.
      • Please see s3crypto documentation for migration details.

    Release v1.32.13 (2020-06-30)

    Service Client Updates

    • service/codeguru-reviewer: Updates service API and documentation
    • service/comprehendmedical: Updates service API
    • service/ec2: Updates service API and documentation
      • Added support for tag-on-create for CreateVpc, CreateEgressOnlyInternetGateway, CreateSecurityGroup, CreateSubnet, CreateNetworkInterface, CreateNetworkAcl, CreateDhcpOptions and CreateInternetGateway. You can now specify tags when creating any of these resources. For more information about tagging, see AWS Tagging Strategies.
    • service/ecr: Updates service API and documentation
      • Add a new parameter (ImageDigest) and a new exception (ImageDigestDoesNotMatchException) to PutImage API to support pushing image by digest.
    • service/rds: Updates service documentation
      • Documentation updates for rds

    Release v1.32.12 (2020-06-29)

    Service Client Updates

    • service/autoscaling: Updates service documentation and examples
      • Documentation updates for Amazon EC2 Auto Scaling.
    • service/codeguruprofiler: Updates service API, documentation, and paginators
    • service/codestar-connections: Updates service API, documentation, and paginators
    • service/ec2: Updates service API, documentation, and paginators
      • Virtual Private Cloud (VPC) customers can now create and manage their own Prefix Lists to simplify VPC configurations.

    Release v1.32.11 (2020-06-26)

    Service Client Updates

    • service/cloudformation: Updates service API and documentation
      • ListStackInstances and DescribeStackInstance now return a new StackInstanceStatus object that contains DetailedStatus values: a disambiguation of the more generic Status value. ListStackInstances output can now be filtered on DetailedStatus using the new Filters parameter.
    • service/cognito-idp: Updates service API

    ... (truncated)

    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

  • [BUG] MYSQL driver no longer respects safe updates

    [BUG] MYSQL driver no longer respects safe updates

    Describe the Bug

    golang-migrate no longer respects safe updates within MySQL.

    Steps to Reproduce

    1. Create a database that has SQL_SAFE_UPDATES enabled.
    2. Run any migration which causes the migrations table to be updated.
    3. query := "DELETE FROM `" + m.config.MigrationsTable + "`" will be executed and golang-migrate will error out:

    Error 1175: You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column in line 0: DELETE FROM schema_migrations

    Expected Behavior

    I would expect golang-migrate to respect this flag by using an additional WHERE-statement (or by using TRUNCATE).

    Migrate Version

    github.com/golang-migrate/migrate/v4 v4.15.2

    Loaded Source Drivers

    iofs (so I can use go:embed)

    Loaded Database Drivers

    mysql

    Go Version

    go version go1.19.3 darwin/amd64

    Additional context

    This does not happen in v4.15.1 as it used TRUNCATE (changed with https://github.com/golang-migrate/migrate/pull/656), which is acceptable for safe updating. Might be nice if I, as a user, could select what I want to use?

  • Use DELETE FROM instead of TRUNCATE for pgx

    Use DELETE FROM instead of TRUNCATE for pgx

    Problem

    In PostgreSQL, TRUNCATE statements cannot run during a db backup.

    Deployment would be blocked by db_dump if there would be migration to run.

    db_dump requires ACCESS SHARE LOCK

    Reference: https://www.postgresql.org/docs/current/app-pgdump.html

    And will conflict with TRUNCATE which requires ACCESS EXCLUSIVE LOCK

    Solution

    Use DELETE FROM instead of TRUNCATE for clearing the table since there is only one row of record

    Reference

    Postgres

    https://www.postgresql.org/docs/current/sql-truncate.html

    Article about this scenario

    https://www.dbi-services.com/blog/when-we-do-a-pg_dump-and-right-afterwards-truncate-a-table-which-is-in-the-dump-what-happens/

  • I don't get support sqlserver with the library

    I don't get support sqlserver with the library

    Good afternoon. I'm trying to use your code as a library in the code. In file go.mod I write the dependency github.com/golang-migrate/migrate/v4 v4.15.2. Everything is regular, but only the dependency for postgres arrives, while others do not. I, in particular, need to work with sqlserver. How can I normally achieve this? Maybe I missed something in the documentation?

  • clickhouse: Quote db name in ensureVersionTable

    clickhouse: Quote db name in ensureVersionTable

    Hi all!

    Today I wanted to use migrate on a clickhouse cloud cluster where we were setting up databases with dashes in their name, e.g. database-name. This broke migrate since the SHOW TABLES query for clickhouse did not quote the database name, hence this PR.

Opinionated tool for database structure management and migrations

trek Requirements At least version 13 of postgres is needed. Installation go install . Setup Create config.yaml: model_name: <model_name> db_name: <db

Dec 14, 2022
A Go package to help write migrations with go-pg/pg.

go-pg-migrations A Go package to help write migrations with go-pg/pg. Usage Installation Because go-pg now has Go modules support, go-pg-migrations al

Oct 16, 2022
Tool to handle versioned migrations with gorm

GORM Migrations About Gorm Migrations Gorm Migrations is a tool designed for go-gorm. Gorm Migration takes the pain out of development of migrations v

Mar 14, 2022
GitHub's Online Schema Migrations for MySQL
GitHub's Online Schema Migrations for MySQL

gh-ost GitHub's online schema migration for MySQL gh-ost is a triggerless online schema migration solution for MySQL. It is testable and provides paus

Apr 3, 2020
Dbmate is a database migration tool, to keep your database schema in sync across multiple developers and your production servers.

Dbmate is a database migration tool, to keep your database schema in sync across multiple developers and your production servers. It is a stand

Jan 1, 2023
mini tools handling migrasion database from cli

mini tools handling migrasion database from cli

Dec 13, 2021
Django style fixtures for Golang's excellent built-in database/sql library.

go-fixtures Django style fixtures for Golang's excellent built-in database/sql library. Currently only YAML fixtures are supported. There are two rese

Sep 26, 2022
Database schema evolution library for Go

Try browsing the code on Sourcegraph! Darwin Database schema evolution library for Go Example package main import ( "database/sql" "log" "github.

Dec 5, 2022
Dead simple Go database migration library.
Dead simple Go database migration library.

migrator Dead simple Go database migration library. Features Simple code Usage as a library, embeddable and extensible on your behalf Support of any d

Nov 9, 2022
A migration engine to deploy database changes in your golang + mongodb app.

bisonmigration A migration engine to deploy database changes in your golang + mongodb app. Migration files register their UP and DOWN functions in the

Jan 30, 2022
A simple database migration tool using an sql.DB connection and fs.FS for the migration source

A simple database migration tool using an sql.DB connection and fs.FS for the migration source. It has no non-test dependencies.

Dec 7, 2022
Goose database migration tool - fork of https://bitbucket.org/liamstask/goose

goose Goose is a database migration tool. Manage your database schema by creating incremental SQL changes or Go functions. Goals of this fork github.c

Dec 30, 2022
Minimalistic database migration helper for Gorm ORM

Gormigrate Gormigrate is a minimalistic migration helper for Gorm. Gorm already has useful migrate functions, just misses proper schema versioning and

Dec 25, 2022
goydb, a couchdb compatible embeddable database written in go
goydb, a couchdb compatible embeddable database written in go

goydb, a couchdb compatible embeddable database written in go Getting started (not embedded) Using docker mkdir data docker run -e GOYDB_ADMINS=admin:

Sep 14, 2022
Database migration through structures - development

goMigration 基于 Golang 的数据库迁移工具,目前仍在开发中,有兴趣的小伙伴可以联系我一起~ 食用方法 go get https://github.com/DGuang21/goMigration 手动将其安装 可通过 gom gen create_c_user_table 方法生

Dec 2, 2021
A database migration tool written in Go.

dbmagritte created by Austin Poor A database migration tool written in Go. Usage Commands: init: Set up the repo by creating a .dbmagritte.yaml file a

Jan 29, 2022
Schema management CLI for MySQL
Schema management CLI for MySQL

Skeema is a tool for managing MySQL tables and schema changes in a declarative fashion using pure SQL. It provides a CLI tool allowing you to: Export

Dec 27, 2022
Open Sound Control (OSC) library for Golang. Implemented in pure Go.

GoOSC Open Sound Control (OSC) library for Golang. Implemented in pure Go. This repository is a heavily modified fork of the original go-osc. Please c

Dec 22, 2021
Database migrations. CLI and Golang library.

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

May 30, 2021
Database migrations. CLI and Golang library.

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

Jan 9, 2023