Skip to content

Commit f39a9a0

Browse files
kvchexekias
andauthored
Improve error message when start and rollack fails (#781)
When `pgroll` fails to execute `start` phase of the migration, it runs `rollback` immediately. If `rollback` fails as well, the errors are joined. This made the error message hard to understand. Previously, `pgroll` emitted this message: ``` Error: unable to execute start operation: pq: relation "qsdf" does not exist pq: current transaction is aborted, commands ignored until end of transaction block ``` From now on, the message is clearer. ``` Error: unable to execute start operation of 'my_migration_name': pq: relation "qsdf" does not exist unable to roll back failed operation: pq: current transaction is aborted, commands ignored until end of transaction block ``` If `start` fails but rollback succeeds, we emit a clearer error message: ``` Error: rolled back 'my_migration_name' because start command has failed: pq: relation "qsdf" does not exist ``` --------- Co-authored-by: Carlos Pérez-Aradros Herce <exekias@gmail.com>
1 parent 7cb59a8 commit f39a9a0

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

pkg/roll/execute.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ func (m *Roll) StartDDLOperations(ctx context.Context, migration *migrations.Mig
8989
table, err := op.Start(ctx, m.pgConn, latestSchema, newSchema)
9090
if err != nil {
9191
errRollback := m.Rollback(ctx)
92-
93-
return nil, errors.Join(
94-
fmt.Errorf("unable to execute start operation: %w", err),
95-
errRollback)
92+
if errRollback != nil {
93+
return nil, errors.Join(
94+
fmt.Errorf("unable to execute start operation of %q: %w", migration.Name, err),
95+
fmt.Errorf("unable to roll back failed operation: %w", errRollback))
96+
}
97+
return nil, fmt.Errorf("failed to start %q migration, changes rolled back: %w", migration.Name, err)
9698
}
9799
// refresh schema when the op is isolated and requires a refresh (for example raw sql)
98100
// we don't want to refresh the schema if the operation is not isolated as it would

0 commit comments

Comments
 (0)