Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Wrap each migration file in an atomic transaction #196

Open
mcandre opened this issue Mar 29, 2019 · 6 comments
Open

Enhancement: Wrap each migration file in an atomic transaction #196

mcandre opened this issue Mar 29, 2019 · 6 comments
Labels
backwards incompatible Change is backwards incompatible

Comments

@mcandre
Copy link

mcandre commented Mar 29, 2019

Could migration scripts be wrapped in db.BeginTx() ... db.Rollback/db.Commit(), to make it easier to resolve any errors that occur during migrations?

This could help keep migrations in a clean state, even when a migration fails! Rather than having to clean up the innards of the golang-migrate state, the user can simply fix the migrations and run them again until they pass.

https://golang.org/pkg/database/sql/#Conn.BeginTx

@dhui
Copy link
Member

dhui commented Mar 29, 2019

This would be an interesting change to support in the driver. (See: #14)
However, not every RDBMS supports transactions for DDL/schema changes. e.g. MySQL I'm looking at you...
This is an issue since some migrations modify schema and others modify data.

For now, migrate errs on the side of flexibility.

@TVenuMadhav
Copy link

Golang Migrate Version 1 to Version 4

Are all migration files run in a transaction implicitly(from a black box perspective)?

if yes, then why do we have to explicitly mention (Begin/commit) in migration files with multiple sql statements?

Thanks in advance,

PS :- working with Postgres here.

@dhui dhui added the backwards incompatible Change is backwards incompatible label May 22, 2019
@dhui
Copy link
Member

dhui commented May 22, 2019

Are all migration files run in a transaction implicitly(from a black box perspective)?

No. That's what this issue/feature request is about. See: https://github.com/golang-migrate/migrate/blob/master/MIGRATIONS.md#migration-content-format

if yes, then why do we have to explicitly mention (Begin/commit) in migration files with multiple sql statements?

It's a best practice. If your migration contains multiple statements, a failure in a single statement will rollback the whole migration which makes it easier to re-apply the whole migration after the issue has been resolved.

An issue w/ this feature request is that it's not backwards compatible. e.g. existing migrations using with BEGIN/COMMIT will break

@tv42
Copy link

tv42 commented Jul 15, 2020

I find it really hard to believe this is something the user is supposed to remember every single time they write a migration. I'm personally going to look for an alternate solution now, I really don't trust myself to remember that.

@dhui
Copy link
Member

dhui commented Jul 15, 2020

Other options:

  1. Fork the DB driver and wrap your statement in a transaction in your fork
  2. Implement this as an option for each database. As this issue is currently written, it implies this change would take affect for all migrations instead of being DB specific

See also: #374

@u007
Copy link

u007 commented Oct 7, 2020

i think it could be roled out progressively one by one driver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backwards incompatible Change is backwards incompatible
Projects
None yet
Development

No branches or pull requests

5 participants