DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Why Database Migrations Take Months and How to Speed Them Up
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • How Trustworthy Is Big Data?
  • Fixing Common Oracle Database Problems

Trending

  • Transforming AI-Driven Data Analytics with DeepSeek: A New Era of Intelligent Insights
  • AI-Based Threat Detection in Cloud Security
  • How to Practice TDD With Kotlin
  • Memory Leak Due to Time-Taking finalize() Method
  1. DZone
  2. Data Engineering
  3. Databases
  4. Goose for Database Migrations

Goose for Database Migrations

By 
Matt Butcher user avatar
Matt Butcher
·
Mar. 27, 14 · Interview
Likes (0)
Comment
Save
Tweet
Share
16.4K Views

Join the DZone community and get the full member experience.

Join For Free

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.yml file 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.

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.

$ cd MyProject
$ mkdir db
$ cd db
$ vim dbconf.yml # Open with the editor of your choice.

The 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 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 Up
CREATE TABLE foo (
  -- ...
);

-- +goose Down
DROP TABLE foo;

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.
  • It supports environment variable interpolation. Don't want your password inside the dbconf.yml file? 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 $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.

Database

Published at DZone with permission of Matt Butcher, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Why Database Migrations Take Months and How to Speed Them Up
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • How Trustworthy Is Big Data?
  • Fixing Common Oracle Database Problems

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: