Goose for Database Migrations

Mar 11 2014

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:

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.

Getting Started

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.

Now I have two databases configured. One for testing and one for development. By default, Goose assumes the target database is development.

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 sslmode=disable).

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 down:

-- +goose UpCREATETABLEfoo(-- ...);-- +goose DownDROPTABLEfoo;

With that done, I can now very easily create by development database:

$ goose up

If I want to setup test instead, I use the -env flag:

$ 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.)

Goose Pros

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.

This will cause Goose to check the environment for a variable named $MY_DB_PASSWORD.

Goose Cons

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.