dbmate

package module
v0.0.0-...-ab00ef1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2019 License: MIT Imports: 14 Imported by: 0

README

Dbmate

This project was forked from amacneil/dbmate Note that this README still needs to be updated to reflect various changes (especially with regard to homebrew and releases).

Build Status Go Report Card GitHub Release Documentation

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

It is a standalone command line tool, which can be used with Go, Node.js, Python, Ruby, PHP, or any other language or framework you are using to write database-backed applications. This is especially helpful if you are writing many services in different languages, and want to maintain some sanity with consistent development tools.

For a comparison between dbmate and other popular database schema migration tools, please see the Alternatives table.

Features

  • Supports MySQL, PostgreSQL, and SQLite.
  • Powerful, purpose-built DSL for writing schema migrations.
  • Migrations are timestamp-versioned, to avoid version number conflicts with multiple developers.
  • Migrations are run atomically inside a transaction.
  • Supports creating and dropping databases (handy in development/test).
  • Database connection URL is definied using an environment variable (DATABASE_URL by default), or specified on the command line.
  • Built-in support for reading environment variables from your .env file.
  • Easy to distribute, single self-contained binary.

How to build this project

For local development you can build a docker image

$ git clone git@github.com:turnitin/dbmate
$ cd dbmate
$ make build
$ cp dist/dbmate-darwin-amd64-nocgo /usr/local/bin/dbmate

Once the dbmate distribution is available you can run migrations:

DATABASE_URL=postgres://somehost:someport/dbname?sslmode=disable dbmate migrate

or alternatively run them for a specific project:

DATABASE_URL=postgres://somehost:someport/dbname?sslmode=disable dbmate -p myproject migrate

Installation

If you have docker and docker-compose installed you can build your own artisanal binaries with:

$ make build
$ ls dist
dbmate-darwin-amd64-nocgo dbmate-linux-386          dbmate-linux-amd64        dbmate-linux-amd64-nocgo

Other

Dbmate can be installed directly using go get:

$ go get -u github.com/turnitin/dbmate

Commands

dbmate           # print help
dbmate new       # generate a new migration file
dbmate up        # create the database (if it does not already exist) and run any pending migrations
dbmate create    # create the database
dbmate drop      # drop the database
dbmate migrate   # run any pending migrations
dbmate rollback  # roll back the most recent migration
dbmate down      # alias for rollback

Usage

Dbmate locates your database using the DATABASE_URL environment variable by default. If you are writing a twelve-factor app, you should be storing all connection strings in environment variables.

To make this easy in development, dbmate looks for a .env file in the current directory, and treats any variables listed there as if they were specified in the current environment (existing environment variables take preference, however).

If you do not already have a .env file, create one and add your database connection URL:

$ cat .env
DATABASE_URL="postgres://postgres@127.0.0.1:5432/myapp_development?sslmode=disable"

DATABASE_URL should be specified in the following format:

protocol://username:password@host:port/database_name?options
  • protocol must be one of mysql, postgres, postgresql, sqlite, sqlite3
  • host can be either a hostname or IP address
  • options are driver-specific (refer to the underlying Go SQL drivers if you wish to use these)

MySQL

DATABASE_URL="mysql://username:password@127.0.0.1:3306/database_name"

PostgreSQL

When connecting to Postgres, you may need to add the sslmode=disable option to your connection string, as dbmate by default requires a TLS connection (some other frameworks/languages allow unencrypted connections by default).

DATABASE_URL="postgres://username:password@127.0.0.1:5432/database_name?sslmode=disable"

SQLite

SQLite databases are stored on the filesystem, so you do not need to specify a host. By default, files are relative to the current directory. For example, the following will create a database at ./db/database_name.sqlite3:

DATABASE_URL="sqlite:///db/database_name.sqlite3"

To specify an absolute path, add an additional forward slash to the path. The following will create a database at /tmp/database_name.sqlite3:

DATABASE_URL="sqlite:////tmp/database_name.sqlite3"
Creating Migrations

To create a new migration, run dbmate new create_users_table. You can name the migration anything you like. This will create a file db/migrations/20151127184807_create_users_table.sql in the current directory:

-- migrate:up

-- migrate:down

To write a migration, simply add your SQL to the migrate:up section:

-- migrate:up
create table users (
  id integer,
  name varchar(255),
  email varchar(255) not null
);

-- migrate:down

Note: Migration files are named in the format [version]_[description].sql. Only the version (defined as all leading numeric characters in the file name) is recorded in the database, so you can safely rename a migration file without having any effect on its current application state.

Running Migrations

Run dbmate up to run any pending migrations.

$ dbmate up
Creating: myapp_development
Applying: 20151127184807_create_users_table.sql

Note: dbmate up will create the database if it does not already exist (assuming the current user has permission to create databases). If you want to run migrations without creating the database, run dbmate migrate.

In Postgres, database locking will ensure that:

  • only one migration can run at a time, and
  • migrations are only run once

even for migrations kicked off concurrently.

(Locking is a no-op for both MySQL and SQLite.)

Rolling Back Migrations

By default, dbmate doesn't know how to roll back a migration. In development, it's often useful to be able to revert your database to a previous state. To accomplish this, implement the migrate:down section:

-- migrate:up
create table users (
  id integer,
  name varchar(255),
  email varchar(255) not null
);

-- migrate:down
drop table users;

Run dbmate rollback to roll back the most recent migration:

$ dbmate rollback
Rolling back: 20151127184807_create_users_table.sql
Options

The following command line options are available with all commands. You must use command line arguments in the order:

$ dbmate [global options] command [command options]
  • --migrations-dir, -d - where to keep the migration files, defaults to ./db/migrations
  • --project, -p "project-name" - a name under which to associate the set of migrations. defaults to default
  • --env, -e "DATABASE_URL" - specify an environment variable to read the database connection URL from, defaults to DATABASE_URL

For example, before running your test suite, you may wish to drop and recreate the test database. One easy way to do this is to store your test database connection URL in the TEST_DATABASE_URL environment variable:

$ cat .env
TEST_DATABASE_URL="postgres://postgres@127.0.0.1:5432/myapp_test?sslmode=disable"

You can then specify this environment variable in your test script (Makefile or similar):

$ dbmate -e TEST_DATABASE_URL drop
Dropping: myapp_test
$ dbmate -e TEST_DATABASE_URL up
Creating: myapp_test
Applying: 20151127184807_create_users_table.sql

Additional Features

This fork of dbmate has a few additional features that we use at Turnitin, particularly around:

  • using a single database by multiple microservices,
  • running multiple microservice instances at the same time.

Hopefully they're useful for you.

Tag migrations with a project

By default dbmate records migrations in a table schema_migrations with a single field to hold the migration version. However, if you have multiple microservices using the same database it's very useful to track which of them a migration came from.

So there's an additional field project in the table. By default it's default, but you can modify with a -p argument when applying migrations:

$ export DATABASE_URL=postgres://somehost:someport/dbname?sslmode=disable

$ dbmate migrate               # project "default"

$ dbmate -p myproject migrate  # project "myproject"

This is supported for all databases.

Prevent multiple migrations from running simultaneously

NOTE: this feature is only supported for Postgres. There are hooks if you'd like to implement it for MySQL and/or SQLite.

If you run multiple instances of a service you don't control which one runs first, so having deterministic schema migrations can be tricky. You can solve this with a separate service to run the migration, or by ensuring that only a single instance of the service is started first.

But these are weird special cases, and we'd rather have migrations run the same way everywhere -- they're executed when the service starts up, every single time.

And it turns out databases have a mechanism for this with advisory locks. These are application-defined locks that don't interfere with any database operation.

So if you're using Postgres we try to grab a lock with a predefined key, and if another process has the lock we'll block until that process unlocks it. This prevents reads and writes from being executed by dbmate effectively ensures that only one migration can be executed at a time.

It will show up like this -- say we're starting up three instances of a service simultaneously -- A, B, and C:

         A                           B                           C

   | Requests lock               Requests lock               Requests lock
   |                                 |                           |
T  | Acquires lock                   |                           |
   |                                 |                           |
   | Scans local + DB migrations     |                           |
I  |                                 |                           | 
   | Finds migration to apply        |                           |
M  |                                 |                           |  
   | Applies migration               |                           |
E  |                                 |                           | 
   | Releases lock               Acquires lock                   |
   |    DONE                                                     |
   |                             Scans local + DB migrations     |
   |                                                             |
   |                             Finds no migration to apply     |
   |                                                             |
   |                             Releases lock                Acquires lock
   |                                DONE
   |                                                          Scans local + DB migrations
   |
   |                                                          Finds no migration to apply
   |
   |                                                          Releases lock
   |                                                             DONE

The ordering, of course, is random -- it just looks nicer in a diagram if it cascades like this.

FAQ

How do I use dbmate under Alpine linux?

Alpine linux uses musl libc, which is incompatible with how we build SQLite support (using cgo). If you want Alpine linux support, and don't mind sacrificing SQLite support, please use the dbmate-linux-musl-amd64 build found on the releases page.

Alternatives

Why another database schema migration tool? Dbmate was inspired by many other tools, primarily Rails' ActiveRecord, with the goals of being trivial to configure, and language & framework independent. Here is a comparison between dbmate and other popular migration tools.

goose sql-migrate mattes/migrate activerecord sequelize dbmate
Features
Plain SQL migration files
Support for creating and dropping databases
Timestamp-versioned migration files
Database connection string loaded from environment variables
Automatically load .env file
No separate configuration file
Language/framework independent
Drivers
PostgreSQL
MySQL
SQLite

✴ In theory these tools could be used with other languages, but a Go development environment is required because binary builds are not provided.

If you notice any inaccuracies in this table, please propose a change.

Contributing

Dbmate is written in Go, pull requests are welcome.

Tests are run against a real database using docker-compose. First, install the Docker Toolbox.

Make sure you have docker running:

$ docker-machine start default && eval "$(docker-machine env default)"

To build a docker image and run the tests:

$ make

To run just the lint and tests (without completely rebuilding the docker image):

$ make lint test

Documentation

Index

Constants

View Source
const Version = "1.6.0"

Version of dbmate

Variables

View Source
var DefaultMigrationsDir = "./db/migrations"

DefaultMigrationsDir specifies default directory to find migration files

View Source
var DefaultProject = "default"

DefaultProject specifies the default name to associate with the migrations

View Source
var LockTimeout = "30s"

LockTimeout is the Postgres time specification for how long we should wait for a lock before dying

Functions

func GetDriverOpen

func GetDriverOpen(u *url.URL) (*sql.DB, error)

GetDriverOpen is a shortcut for GetDriver(u.Scheme).Open(u)

func RunInLock

func RunInLock(driver Driver, sqlDB *sql.DB, timeoutSecs int, lockFunc func(Driver, *sql.DB) error) error

RunInLock will execute a function in the context of the driver's advisory lock

Types

type DB

type DB struct {
	DatabaseURL   *url.URL
	MigrationsDir string
	Project       string
}

DB allows dbmate actions to be performed on a specified database

func NewDB

func NewDB(databaseURL *url.URL) *DB

NewDB initializes a new dbmate database

func (*DB) Create

func (db *DB) Create() error

Create creates the current database

func (*DB) Drop

func (db *DB) Drop() error

Drop drops the current database (if it exists)

func (*DB) GetDriver

func (db *DB) GetDriver() (Driver, error)

GetDriver loads the required database driver

func (*DB) Migrate

func (db *DB) Migrate(lockTimeoutSecs int) error

Migrate migrates database to the latest version

func (*DB) New

func (db *DB) New(name string) error

New creates a new migration file

func (*DB) RecordOnly

func (db *DB) RecordOnly() error

RecordOnly will record without applying all unapplied filesystem migrations

func (*DB) Rollback

func (db *DB) Rollback() error

Rollback rolls back the most recent migration

func (*DB) Up

func (db *DB) Up(lockTimeoutSecs int) error

Up creates the database (if necessary) and runs migrations

type Driver

type Driver interface {
	Open(*url.URL) (*sql.DB, error)
	DatabaseExists(*url.URL) (bool, error)
	CreateDatabase(*url.URL) error
	DropDatabase(*url.URL) error
	CreateMigrationsTable(*sql.DB) error
	SelectMigrations(*sql.DB, int, string) (map[string]bool, error)
	InsertMigration(Transaction, string, string) error
	DeleteMigration(Transaction, string) error
	Lock(*sql.DB) error
	Unlock(*sql.DB)
}

Driver provides top level database functions

func GetDriver

func GetDriver(name string) (Driver, error)

GetDriver loads a database driver by name

type MySQLDriver

type MySQLDriver struct {
}

MySQLDriver provides top level database functions

func (MySQLDriver) CreateDatabase

func (drv MySQLDriver) CreateDatabase(u *url.URL) error

CreateDatabase creates the specified database

func (MySQLDriver) CreateMigrationsTable

func (drv MySQLDriver) CreateMigrationsTable(db *sql.DB) error

CreateMigrationsTable creates the schema_migrations table

func (MySQLDriver) DatabaseExists

func (drv MySQLDriver) DatabaseExists(u *url.URL) (bool, error)

DatabaseExists determines whether the database exists

func (MySQLDriver) DeleteMigration

func (drv MySQLDriver) DeleteMigration(db Transaction, version string) error

DeleteMigration removes a migration record

func (MySQLDriver) DropDatabase

func (drv MySQLDriver) DropDatabase(u *url.URL) error

DropDatabase drops the specified database (if it exists)

func (MySQLDriver) InsertMigration

func (drv MySQLDriver) InsertMigration(db Transaction, version string, project string) error

InsertMigration adds a new migration record

func (MySQLDriver) Lock

func (drv MySQLDriver) Lock(db *sql.DB) error

Lock locks the database so no other migrations can be run, no-op in MySQL

func (MySQLDriver) Open

func (drv MySQLDriver) Open(u *url.URL) (*sql.DB, error)

Open creates a new database connection

func (MySQLDriver) SelectMigrations

func (drv MySQLDriver) SelectMigrations(db *sql.DB, limit int, project string) (map[string]bool, error)

SelectMigrations returns a list of applied migrations with an optional limit (in descending order)

func (MySQLDriver) Unlock

func (drv MySQLDriver) Unlock(db *sql.DB)

Unlock removes a database lock so other migrations can be run, no-op in MySQL

type PostgresDriver

type PostgresDriver struct {
}

PostgresDriver provides top level database functions

func (PostgresDriver) CreateDatabase

func (drv PostgresDriver) CreateDatabase(u *url.URL) error

CreateDatabase creates the specified database

func (PostgresDriver) CreateMigrationsTable

func (drv PostgresDriver) CreateMigrationsTable(db *sql.DB) error

CreateMigrationsTable creates the schema_migrations table

func (PostgresDriver) DatabaseExists

func (drv PostgresDriver) DatabaseExists(u *url.URL) (bool, error)

DatabaseExists determines whether the database exists

func (PostgresDriver) DeleteMigration

func (drv PostgresDriver) DeleteMigration(db Transaction, version string) error

DeleteMigration removes a migration record

func (PostgresDriver) DropDatabase

func (drv PostgresDriver) DropDatabase(u *url.URL) error

DropDatabase drops the specified database (if it exists)

func (PostgresDriver) InsertMigration

func (drv PostgresDriver) InsertMigration(db Transaction, version string, project string) error

InsertMigration adds a new migration record

func (PostgresDriver) Lock

func (drv PostgresDriver) Lock(db *sql.DB) error

Lock tries to acquire an advisory lock and waits for the configured LockTimeout seconds before giving up

func (PostgresDriver) Open

func (drv PostgresDriver) Open(u *url.URL) (*sql.DB, error)

Open creates a new database connection, also setting a lock timeout to 30 seconds for the whole session.

func (PostgresDriver) SelectMigrations

func (drv PostgresDriver) SelectMigrations(db *sql.DB, limit int, project string) (map[string]bool, error)

SelectMigrations returns a list of applied migrations with an optional limit (in descending order)

func (PostgresDriver) Unlock

func (drv PostgresDriver) Unlock(db *sql.DB)

Unlock releases an advisory lock

type SQLiteDriver

type SQLiteDriver struct {
}

SQLiteDriver provides top level database functions

func (SQLiteDriver) CreateDatabase

func (drv SQLiteDriver) CreateDatabase(u *url.URL) error

CreateDatabase creates the specified database

func (SQLiteDriver) CreateMigrationsTable

func (drv SQLiteDriver) CreateMigrationsTable(db *sql.DB) error

CreateMigrationsTable creates the schema_migrations table

func (SQLiteDriver) DatabaseExists

func (drv SQLiteDriver) DatabaseExists(u *url.URL) (bool, error)

DatabaseExists determines whether the database exists

func (SQLiteDriver) DeleteMigration

func (drv SQLiteDriver) DeleteMigration(db Transaction, version string) error

DeleteMigration removes a migration record

func (SQLiteDriver) DropDatabase

func (drv SQLiteDriver) DropDatabase(u *url.URL) error

DropDatabase drops the specified database (if it exists)

func (SQLiteDriver) InsertMigration

func (drv SQLiteDriver) InsertMigration(db Transaction, version string, project string) error

InsertMigration adds a new migration record

func (SQLiteDriver) Lock

func (drv SQLiteDriver) Lock(db *sql.DB) error

Lock locks the database so no other migrations can be run, no-op in SQLite

func (SQLiteDriver) Open

func (drv SQLiteDriver) Open(u *url.URL) (*sql.DB, error)

Open creates a new database connection

func (SQLiteDriver) SelectMigrations

func (drv SQLiteDriver) SelectMigrations(db *sql.DB, limit int, project string) (map[string]bool, error)

SelectMigrations returns a list of applied migrations with an optional limit (in descending order)

func (SQLiteDriver) Unlock

func (drv SQLiteDriver) Unlock(db *sql.DB)

Unlock removes a database lock so other migrations can be run, no-op in SQLite

type Transaction

type Transaction interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
}

Transaction can represent a database or open transaction

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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