steder/goose — Issue #3: Add reverse migrations
Author: steder
Created: 2012-03-04 17:47:04+00:00
State: open

Being able to undo or reverse a migration could be a useful feature for testing and rollback.

I think the configuration file could be updated from:

migrations:
- migration1.sql
- migration2.sql

To something more like:

migrations:
- migration1: # defines both forward and reverse
forward: migration1.sql
reverse: migration1_rollback.sql
- migration2.sql # no reverse migration/rollback

The actual logic for running migrations in reverse should be pretty straightforward, we'll just need a toggle to explicitly enable rollbacks.

However, how do we handle something like:

migrations:
- migration 0.sql
- migration1:
forward: migration1.sql
reverse: migration1.rollback.sql
- migration2.sql
- migration3:
forward: migration3.sql
reverse: migration3.rollback.sql

Can we rollback from 3 to 0? Or just from 3 to 2?

Must there be a reverse migration for every version?

Is it an error to try to rollback a migration that has no reverse or do you allow it?

Say we allow a rollback from 3 to 0. When rolling forward again to 3 or 4, do we rerun migration 2? I think we can reasonably be expected to remember that 2 was run and not run it again, but depending on the contents of 2 it may need to be rerun...

Ultimately this depends on having a workflow / migration policy for your specific project or schema so there's only so much we can do in the tool. We should be conservative in what we do and liberal in what we accept.