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

fix: don't erroneously stop with nil error when a down migration is found which was never applied #762

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions popx/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"database/sql"
"fmt"
"io"
"math"
"os"
"regexp"
"sort"
Expand Down Expand Up @@ -116,7 +117,7 @@ func (m *Migrator) UpTo(ctx context.Context, step int) (applied int, err error)
legacyVersion := mi.Version[:14]
exists, err = c.Where("version = ?", legacyVersion).Exists(mtn)
if err != nil {
return errors.Wrapf(err, "problem checking for migration version %s", mi.Version)
return errors.Wrapf(err, "problem checking for legacy migration version %s", legacyVersion)
}

if exists {
Expand Down Expand Up @@ -191,27 +192,37 @@ func (m *Migrator) UpTo(ctx context.Context, step int) (applied int, err error)

// Down runs pending "down" migrations and rolls back the
// database by the specified number of steps.
func (m *Migrator) Down(ctx context.Context, step int) error {
func (m *Migrator) Down(ctx context.Context, steps int) error {
span, ctx := m.startSpan(ctx, MigrationDownOpName)
defer span.End()

if steps <= 0 {
steps = math.MaxInt
}

c := m.Connection.WithContext(ctx)
return m.exec(ctx, func() error {
return m.exec(ctx, func() (err error) {
mtn := m.sanitizedMigrationTableName(c)
count, err := c.Count(mtn)
if err != nil {
return errors.Wrap(err, "migration down: unable count existing migration")
}
steps = min(steps, count)

mfs := m.Migrations["down"].SortAndFilter(c.Dialect.Name(), sort.Reverse)
// skip all ran migration
if len(mfs) > count {
mfs = mfs[len(mfs)-count:]
}
// run only required steps
if step > 0 && len(mfs) >= step {
mfs = mfs[:step]
}
for _, mi := range mfs {

reverted := 0
defer func() {
m.l.Debugf("Successfully reverted %d migrations.", reverted)
if err != nil {
m.l.WithError(err).Error("Problem reverting migrations.")
}
}()
for i, mi := range mfs {
if i >= steps {
break
}
l := m.l.WithField("version", mi.Version).WithField("migration_name", mi.Name).WithField("migration_file", mi.Path)
exists, err := c.Where("version = ?", mi.Version).Exists(mtn)
if err != nil {
return errors.Wrapf(err, "problem checking for migration version %s", mi.Version)
Expand All @@ -221,11 +232,11 @@ func (m *Migrator) Down(ctx context.Context, step int) error {
legacyVersion := mi.Version[:14]
legacyVersionExists, err := c.Where("version = ?", legacyVersion).Exists(mtn)
if err != nil {
return errors.Wrapf(err, "problem checking for migration version %s", mi.Version)
return errors.Wrapf(err, "problem checking for legacy migration version %s", legacyVersion)
}

if !legacyVersionExists {
return errors.Wrapf(err, "problem checking for migration version %s", legacyVersion)
return errors.Errorf("neither normal (%s) nor legacy migration (%s) exist", mi.Version, legacyVersion)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the problem. At this point, err is always nil. errors.Wrapf returns nil when its first argument is nil, so this error condition was swallowed and the down migrations silently stopped.

This revelead two incorrect migrations in Kratos. They are uncritical though.

}
} else if !exists {
return errors.Errorf("migration version %s does not exist", mi.Version)
Expand Down Expand Up @@ -264,7 +275,8 @@ func (m *Migrator) Down(ctx context.Context, step int) error {
}
}

m.l.Debugf("< %s", mi.Name)
l.Infof("< %s applied successfully", mi.Name)
reverted++
}
return nil
})
Expand Down
Loading