A database migration tool. Supports SQL migrations and Go functions.
A database migration tool. Supports SQL migrations and Go functions.
Goose is a database migration tool. Both a CLI and a library.
Manage your database schema by creating incremental SQL changes or Go functions.
go install github.com/pressly/goose/v3/cmd/goose@latestThis will install the goose binary to your $GOPATH/bin directory.
Binary too big? Build a lite version by excluding the drivers you don't need:
go build -tags='no_postgres no_mysql no_sqlite3 no_ydb' -o goose ./cmd/goose
# Available build tags:
# no_azuresql no_clickhouse no_libsql no_mssql
# no_mysql no_postgres no_sqlite3 no_vertica
# no_ydb
#
# Note: no_mssql also excludes the azuresql driver.For macOS users goose is available as a Homebrew
Formulae:
brew install gooseSee installation documentation for more details.
goose help output…Commonly used commands:
create • up • up-to • down • down-to • status • version
Create a new SQL migration.
$ goose create add_some_column sql
$ Created new file: 20170506082420_add_some_column.sql
$ goose -s create add_some_column sql
$ Created new file: 00001_add_some_column.sqlEdit the newly created file to define the behavior of your migration.
You can also create a Go migration, if you then invoke it with your own goose binary:
$ goose create fetch_user_data go
$ Created new file: 20170506082421_fetch_user_data.goApply all available migrations.
$ goose up
$ OK 001_basics.sql
$ OK 002_next.sql
$ OK 003_and_again.goMigrate up to a specific version.
$ goose up-to 20170506082420
$ OK 20170506082420_create_table.sqlMigrate up a single migration from the current version
$ goose up-by-one
$ OK 20170614145246_change_type.sqlRoll back a single migration from the current version.
$ goose down
$ OK 003_and_again.goRoll back migrations to a specific version.
$ goose down-to 20170506082527
$ OK 20170506082527_alter_column.sqlOr, roll back all migrations (careful!):
$ goose down-to 0Print the status of all migrations:
$ goose status
$ Applied At Migration
$ =======================================
$ Sun Jan 6 11:25:03 2013 -- 001_basics.sql
$ Sun Jan 6 11:25:03 2013 -- 002_next.sql
$ Pending -- 003_and_again.goNote: for MySQL and MariaDB (use the mysql driver) parseTime flag must be enabled.
Note: for MySQL and MariaDB
multiStatements must
be enabled. This is required when writing multiple queries separated by ';' characters in a single
sql file.
Print the current version of the database:
$ goose version
$ goose: version 002If you prefer to use environment variables, instead of passing the driver and database string as arguments, you can set the following environment variables:
1. Via environment variables:
export GOOSE_DRIVER=DRIVER
export GOOSE_DBSTRING=DBSTRING
export GOOSE_MIGRATION_DIR=MIGRATION_DIR
export GOOSE_TABLE=TABLENAME2. Via .env files with corresponding variables. .env file example:
GOOSE_DRIVER=postgres
GOOSE_DBSTRING=postgres://admin:admin@localhost:5432/admin_db
GOOSE_MIGRATION_DIR=./migrations
GOOSE_TABLE=custom.goose_migrationsLoading from .env files is enabled by default. To disable this feature, set the -env=none flag.
If you want to load from a specific file, set the -env flag to the file path.
For more details about environment variables, see the official documentation on environment variables.
goose supports migrations written in SQL or in Go.
A sample SQL migration looks like:
-- +goose Up
CREATE TABLE post (
id int NOT NULL,
title text,
body text,
PRIMARY KEY(id)
);
-- +goose Down
DROP TABLE post;Each migration file must have exactly one -- +goose Up annotation. The -- +goose Down annotation
is optional. If the file has both annotations, then the -- +goose Up annotation must come
first.
Notice the annotations in the comments. Any statements following -- +goose Up will be executed as
part of a forward migration, and any statements following -- +goose Down will be executed as part
of a rollback.
By default, all migrations are run within a transaction. Some statements like CREATE DATABASE,
however, cannot be run within a transaction. You may optionally add -- +goose NO TRANSACTION to
the top of your migration file in order to skip transactions within that specific migration file.
Both Up and Down migrations within this file will be run without transactions.
By default, SQL statements are delimited by semicolons - in fact, query statements must end with a semicolon to be properly recognized by goose.
By default, all migrations are run on the public schema. If you want to use a different schema,
specify the schema name using the table option like -table='schemaname.goose_db_version.
More complex statements (PL/pgSQL) that have semicolons within them must be annotated with -- +goose StatementBegin and -- +goose StatementEnd to be properly recognized. For example:
…Goose supports environment variable substitution in SQL migrations through annotations. To enable
this feature, use the -- +goose ENVSUB ON annotation before the queries where you want
substitution applied. It stays active until the -- +goose ENVSUB OFF annotation is encountered.
You can use these annotations multiple times within a file.
This feature is disabled by default for backward compatibility with existing scripts.
For PL/pgSQL functions or other statements where substitution is not desired, wrap the annotations
explicitly around the relevant parts. For example, to exclude escaping the $$ characters:
-- +goose StatementBegin
CREATE OR REPLACE FUNCTION test_func()
RETURNS void AS $$
-- +goose ENVSUB ON
BEGIN
RAISE NOTICE '${SOME_ENV_VAR}';
END;
-- +goose ENVSUB OFF
$$ LANGUAGE plpgsql;
-- +goose StatementEnd${VAR} or $VAR - expands to the value of the environment variable VAR${VAR:-default} - expands to the value of the environment variable VAR, or default if VAR
is unset or null${VAR-default} - expands to the value of the environment variable VAR, or default if VAR
is unset${VAR?err_msg} - expands to the value of the environment variable VAR, or prints err_msg and
error if VAR unset${VAR:?err_msg} - expands to the value of the environment variable VAR, or prints err_msg
and error if VAR unset or null.See mfridman/interpolate for more details on supported expansions.
Go 1.16 introduced new feature: compile-time embedding files into binary and corresponding filesystem abstraction.
This feature can be used only for applying existing migrations. Modifying operations such as fix
and create will continue to operate on OS filesystem even if using embedded files. This is
expected behaviour because io/fs interfaces allows read-only access.
Make sure to configure the correct SQL dialect, see dialect.go for supported SQL dialects.
Example usage, assuming that SQL migrations are placed in the migrations directory:
package main
import (
"database/sql"
"embed"
"github.com/pressly/goose/v3"
)
//go:embed migrations/*.sql
var embedMigrations embed.FS
func main() {
var db *sql.DB
// setup database
goose.SetBaseFS(embedMigrations)
if err := goose.SetDialect("postgres"); err != nil {
panic(err)
}
if err := goose.Up(db, "migrations"); err != nil {
panic(err)
}
// run app
}Note that we pass "migrations" as directory argument in Up because embedding saves directory
structure.
github.com/pressly/goosemigrations package into Go build: in main.go, import _ "github.com/me/myapp/migrations"goose.Up(db *sql.DB, dir string)A sample Go migration 00002_users_add_email.go file looks like:
package migrations
import (
"database/sql"
"github.com/pressly/goose/v3"
)
func init() {
goose.AddMigration(Up, Down)
}
func Up(tx *sql.Tx) error {
_, err := tx.Exec("UPDATE users SET username='admin' WHERE username='root';")
if err != nil {
return err
}
return nil
}
func Down(tx *sql.Tx) error {
_, err := tx.Exec("UPDATE users SET username='root' WHERE username='admin';")
if err != nil {
return err
}
return nil
}Note that Go migration files must begin with a numeric value, followed by an underscore, and must
not end with *_test.go.
Please, read the versioning problem first.
By default, if you attempt to apply missing (out-of-order) migrations goose will raise an error.
However, If you want to apply these missing migrations pass goose the -allow-missing flag, or if
using as a library supply the functional option goose.WithAllowMissing() to Up, UpTo or UpByOne.
However, we strongly recommend adopting a hybrid versioning approach, using both timestamps and sequential numbers. Migrations created during the development process are timestamped and sequential versions are ran on production. We believe this method will prevent the problem of conflicting versions when writing software in a team environment.
To help you adopt this approach, create will use the current timestamp as the migration version.
When you're ready to deploy your migrations in a production environment, we also provide a helpful
fix command to convert your migrations into sequential order, while preserving the timestamp
ordering. We recommend running fix in the CI pipeline, and only when the migrations are ready for
production.
The gopher mascot was designed by Renée French / CC 3.0. For more info check out the Go Blog. Adapted by Ellen.
Licensed under MIT License
Pass a context and database handle to CreateTable
Provider does not log anything unless Verbose=true
`CREATE INDEX CONCURRENTLY` with `GoMigrationNoTx` failing
Add support to replicated clickhouse cluster
Goose: This command is not supported in the prepared statement protocol yet (starrocks)
Migrations table creation and insert fails on MySQL during transaction
"NO TRANSACTION" migrations should mark database as dirty if not successful
`-allow-missing` for per-migration opt-in
`goose up -fake` to mark migration as applied without running it
go tool support documentation