From 8ce33fbbac8e33077c20daf19a14c5ff2291bcae Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Sun, 19 Dec 2021 10:28:08 +0200 Subject: [PATCH] fix(migrate): mark migrations as applied on error so the migration can be rolled back --- migrate/migrator.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/migrate/migrator.go b/migrate/migrator.go index 7f39936f0..9c8d44173 100644 --- a/migrate/migrator.go +++ b/migrate/migrator.go @@ -149,15 +149,16 @@ func (m *Migrator) Migrate(ctx context.Context, opts ...MigrationOption) (*Migra migration := &group.Migrations[i] migration.GroupID = group.ID + // Always mark migration as applied so the rollback has a chance to fix the database. + if err := m.MarkApplied(ctx, migration); err != nil { + return nil, err + } + if !cfg.nop && migration.Up != nil { if err := migration.Up(ctx, m.db); err != nil { return group, err } } - - if err := m.MarkApplied(ctx, migration); err != nil { - return nil, err - } } return group, nil