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.

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

Jan 4, 2023
Simple MySQL migrations in Go

gomigrator This package contains a very small MySQL migration solution for go written in 1 file. The aim of this package is to be very small but highl

Nov 29, 2021
[mirror] the database client and tools for the Go vulnerability database

The Go Vulnerability Database golang.org/x/vulndb This repository is a prototype of the Go Vulnerability Database. Read the Draft Design. Neither the

Dec 29, 2022
Database - Example project of database realization using drivers and models

database Golang based database realization Description Example project of databa

Feb 10, 2022
Interactive terminal user interface and CLI for database connections. MySQL, PostgreSQL. More to come.
Interactive terminal user interface and CLI for database connections. MySQL, PostgreSQL. More to come.

?? dbui dbui is the terminal user interface and CLI for database connections. It provides features like, Connect to multiple data sources and instance

Jan 5, 2023
sqlx is a library which provides a set of extensions on go's standard database/sql library

sqlx is a library which provides a set of extensions on go's standard database/sql library. The sqlx versions of sql.DB, sql.TX, sql.Stmt, et al. all leave the underlying interfaces untouched, so that their interfaces are a superset on the standard ones. This makes it relatively painless to integrate existing codebases using database/sql with sqlx.

Jan 7, 2023
Library for scanning data from a database into Go structs and more

scany Overview Go favors simplicity, and it's pretty common to work with a database via driver directly without any ORM. It provides great control and

Jan 9, 2023
A go Library for scan database/sql rows to struct、slice、other types. And it support multiple databases connection management

ploto A go Library for scan database/sql rows to struct、slice、other types. And it support multiple databases connection management It's not an ORM. wo

Nov 3, 2022
Database Access Layer for Golang - Testable, Extendable and Crafted Into a Clean and Elegant API

REL Modern Database Access Layer for Golang. REL is golang orm-ish database layer for layered architecture. It's testable and comes with its own test

Dec 29, 2022
A Go rest API project that is following solid and common principles and is connected to local MySQL database.
A Go rest API project that is following solid and common principles and is connected to local MySQL database.

This is an intermediate-level go project that running with a project structure optimized RESTful API service in Go. API's of that project is designed based on solid and common principles and connected to the local MySQL database.

Dec 25, 2022
An observability database aims to ingest, analyze and store Metrics, Tracing and Logging data.
An observability database aims to ingest, analyze and store Metrics, Tracing and Logging data.

BanyanDB BanyanDB, as an observability database, aims to ingest, analyze and store Metrics, Tracing and Logging data. It's designed to handle observab

Dec 31, 2022
database to golang struct
database to golang struct

中文文档 mysql database to golang struct conversion tools base on gorm(v1/v2),You can automatically generate golang sturct from mysql database. big Camel-

Jan 9, 2023
Constant Database native golang implementation

CDB golang implementation cdb is a fast, reliable, simple package for creating and reading constant databases see docs for more details Advantages Ite

Jul 15, 2022
Golang restAPI crud project with mySql database.
 Golang restAPI crud project with mySql database.

Golang RestAPI using gorilla/mux Golang restAPI crud project with mySql database. Test Api with Thunder Client vs code beautiful Extension. and use Be

Mar 26, 2022
A simple Golang-based application that queries a PostgreSQL database

Qwik-E-Mart Demo App A simple Golang-based application that queries a PostgreSQL database named qwikemart to read and return customer data stored in t

Nov 6, 2021
Implementasi database oracle kedalam golang

Go with Oracle database Implementasi database oracle kedalam golang How to using swagger Install generator swagger menggunakan perintah : go get -u gi

Nov 20, 2021
Golang database driver for SQLite

go-sqlite Golang database driver for SQLite. Does not use cgo. This driver is based on pure-Go SQLite implementation (https://gitlab.com/cznic/sqlite)

Dec 30, 2022
BigQuery database/sql golang driver

BigQuery SQL Driver This library is compatible with Go 1.17+ Please refer to CHA

Dec 7, 2022