-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Enhancement: Wrap each migration file in an atomic transaction #196
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
Comments
This would be an interesting change to support in the driver. (See: #14) For now, migrate errs on the side of flexibility. |
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. |
No. That's what this issue/feature request is about. See: https://github.com/golang-migrate/migrate/blob/master/MIGRATIONS.md#migration-content-format
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 |
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. |
Other options:
See also: #374 |
i think it could be roled out progressively one by one driver |
I see that this might not be possible for every DB that gomigrate supports, however I think most of the SQL DBs do. That said, if a DB supports transactions, having gomigrate use them automatically would be a great idea. I nearly submitted a bug report regarding this behavior but then realized that it simply doesn't put transactions there for you. So, why not just add a simple logic like this:
This way if a user DOES NOT use transactions in their migrations for the supported DBs gomigrate can wrap it for them thus preventing potential foot-shootings. |
might as well fork |
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
The text was updated successfully, but these errors were encountered: