Skip to content

Commit f77a5a0

Browse files
committed
fix(migration): commit undo transaction
1 parent 6296e0d commit f77a5a0

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

db/sql/migration.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,15 @@ func getVersionErrPath(version db.Migration) string {
3535

3636
// getVersionSQL takes a path to an SQL file and returns it from embed.FS
3737
// a slice of strings separated by newlines
38-
func getVersionSQL(name string) (queries []string) {
38+
func getVersionSQL(name string, ignoreErrors bool) (queries []string) {
3939
sql, err := dbAssets.ReadFile(path.Join("migrations", name))
4040
if err != nil {
41-
panic(err)
41+
if ignoreErrors {
42+
log.WithError(err).Warnf("migration %s not found", name)
43+
return nil
44+
} else {
45+
panic(err)
46+
}
4247
}
4348
queries = strings.Split(strings.ReplaceAll(string(sql), ";\r\n", ";\n"), ";\n")
4449
for i := range queries {
@@ -152,7 +157,7 @@ func (d *SqlDb) ApplyMigration(migration db.Migration) error {
152157
return err
153158
}
154159

155-
queries := getVersionSQL(getVersionPath(migration))
160+
queries := getVersionSQL(getVersionPath(migration), false)
156161
for i, query := range queries {
157162
fmt.Printf("\r [%d/%d]", i+1, len(query))
158163

@@ -207,13 +212,21 @@ func (d *SqlDb) TryRollbackMigration(version db.Migration) {
207212
}
208213

209214
defer func() {
210-
if err != nil {
215+
if err == nil {
216+
err = tx.Commit()
217+
if err != nil {
218+
log.WithError(err).WithFields(log.Fields{
219+
"context": "migration",
220+
"version": version.Version,
221+
}).Error("failed to commit undo migration transaction")
222+
}
223+
} else {
211224
_ = tx.Rollback()
212225
log.Error(err)
213226
}
214227
}()
215228

216-
queries := getVersionSQL(getVersionErrPath(version))
229+
queries := getVersionSQL(getVersionErrPath(version), true)
217230

218231
for _, query := range queries {
219232
fmt.Printf(" [ROLLBACK] > %v\n", query)

db/sql/migrations/v2.14.7.sql

Whitespace-only changes.

0 commit comments

Comments
 (0)