Goose for Database Migrations
Goose for Database Migrations
Join the DZone community and get the full member experience.Join For Free
Get the Edge with a Professional Java IDE. 30-day free trial.
I've been hunting for good database tools to perform that class of tasks that we all need, but that we end up re-implementing over and over again. One such task is database migrations. I've been experimenting with Goose to provide general-purpose database migration support.
What Is Goose?
Goose is a general purpose database migration manager. The idea is simple:
- You provide SQL schema files that follow a particular naming convention
- You provide a simple
dbconf.ymlfile that tells Goose how to connect to your various databases
- Goose provides you simple tools to upgrade (
goose up), check on (
goose status), and even revert (
goose down) schema changes.
Goose does this by adding one more table inside your database. This table tracks which schema changes it has made. Based on its history, it can tell which scheme updates need to be run and which have already been run.
While Goose is written in Go (golang), it is agnostic about what language your app is written in.
I got Goose up and running in less than 30 minutes, and you can probably do it faster.
I already have an empty Postgres database called
foo. But it has no tables. I have an existing codebase, too (
MyProject). Here is the process for configuring Goose to manage the database schema management.
First, I create the
db/ directory, which will house all of the Goose-specific files, including my schema.
$ cd MyProject $ mkdir db $ cd db $ vim dbconf.yml # Open with the editor of your choice.
dbconf.yml file contains a list of databases along with the relevant information for connecting to each. Mine looks something like this:
test: driver: postgres open: user=foo dbname=foo_test sslmode=disable development: driver: postgres open: user=foo dbname=foo_dev sslmode=disable
(Important: use spaces, not tabs, in YAML.)
Now I have two databases configured. One for testing and one for development. By default, Goose assumes the target database is
The above is just configured to connect to the PostgreSQL instance locally running. If I need support for a remote host, I can add
host=... password=... (and remove
At this point, I can generate a new migration.
$ cd .. # Back to MyProject/, not in db/ $ goose create NewSchema sql goose: created db/migrations/20140311133014_NewSchema.sql $ vim db/migrations/20140311133014_NewSchema.sql # Use whatever editor you like
Notice that the
goose create command will create a new SQL file that follows Goose's naming convention. (That trailing
sql on the command is important.
goose create can also generate
go migration files)
My new schema file has two sections: a section for
goose up and a section to rollback with
-- +goose Up CREATE TABLE foo ( -- ... ); -- +goose Down DROP TABLE foo;
With that done, I can now very easily create by
$ goose up
If I want to setup
test instead, I use the
$ goose -env=test up
And that's it! In subsequent schema files, I may
ALTER existing tables or
CREATE new ones, and so on. Just about anything that your SQL engine can execute can be passed through Goose. (Though there are some formatting annotations you need to use for things like stored procedures.)
In addition to the general ease of use of Goose, here are some additional features that I really like:
- You do not need your entire codebase to execute Goose. Our deployment box, for example, only has the Goose
db/directory, not the rest of the code.
- It is largely language neutral if you're just migrating SQL.
- It works with PostgreSQL, MySQL, and SQLite.
- The history table that it creates is human-readable, which makes it easy for me to see what's been going on.
- It supports environment variable interpolation. Don't want your password inside the
dbconf.ymlfile? Just do something like this:
development: driver: postgres open: user=foo dbname=foo_dev sslmode=disable password=$MY_DB_PASSWORD
This will cause Goose to check the environment for a variable named
Honestly, I have very few.
- Right now, you need the Go runtime to install and build Goose. Of course, you can compile Goose once, and then use it wherever.
- While it has support for Go language migrations, it would be nice to be able to write migration scripts that are executed via the shell. That way, one could use Bash, Python, Perl, or whatever else to trigger migrations. But, hey... this is a pretty minor complaint.
Overall, though, Goose is a fantastic tool for handling migrations with ease.
Published at DZone with permission of Matt Butcher , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.