-
Notifications
You must be signed in to change notification settings - Fork 0
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
[postgres] Issue rollback after migration error #1
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -285,7 +285,7 @@ func (p *Postgres) runStatement(statement []byte) error { | |
return nil | ||
} | ||
if _, err := p.conn.ExecContext(ctx, query); err != nil { | ||
|
||
migrationErr := database.Error{OrigErr: err, Err: "migration failed", Query: statement} | ||
if pgErr, ok := err.(*pgconn.PgError); ok { | ||
var line uint | ||
var col uint | ||
|
@@ -298,9 +298,19 @@ func (p *Postgres) runStatement(statement []byte) error { | |
if pgErr.Detail != "" { | ||
message = fmt.Sprintf("%s, %s", message, pgErr.Detail) | ||
} | ||
return database.Error{OrigErr: err, Err: message, Query: statement, Line: line} | ||
migrationErr = database.Error{OrigErr: err, Err: message, Query: statement, Line: line} | ||
} | ||
|
||
// For safety, always issue a rollback on error. In multi-statement | ||
// mode, this is necessary to make sure that the connection is not left | ||
// in an aborted state. In single-statement mode, this will be a no-op | ||
// outside of the implicit transaction block that was already rolled | ||
// back. | ||
if _, rollbackErr := p.conn.ExecContext(ctx, "ROLLBACK"); rollbackErr != nil { | ||
rollbackErr = fmt.Errorf("rolling back migration tx: %w", rollbackErr) | ||
return multierror.Append(migrationErr, rollbackErr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can do enough CFA-by-eye to know that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing of note is that hashicorp (in their infinite wisdom) has made à la https://go.dev/play/p/HOmHw7-3Dbf That being said, defining Switched over accordingly in 4813cdb There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing of note is that hashicorp (in their infinite wisdom) has made à la https://go.dev/play/p/HOmHw7-3Dbf That being said, defining Switched over accordingly in 4813cdb |
||
} | ||
return database.Error{OrigErr: err, Err: "migration failed", Query: statement} | ||
return migrationErr | ||
} | ||
return nil | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a simple example of what this output might look like from a basic console logger in the case where both the migration statement and this explicit rollback generated an error:
I aimed to make this message clear that what failed wasn't a "rollback of the migrate schema version itself" , but just a rollback of the transaction being used in the up migration that was being attempted. Happy to take feedback on this error to make that clearer, though!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My only nit on the wrapped error message is that
failed to
is kinda noise, as suggested by https://github.com/uber-go/guide/blob/master/style.md#error-wrapping, but it looks like this repo usesFailed to...
already https://github.com/search?q=repo%3Anicheinc%2Fmigrate+Errorf+%25w&type=codeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine by me! In bfdd2f0 I changed the wording from "failed to rollback migration tx: " to "rolling back migration tx: ".