@@ -35,10 +35,15 @@ func getVersionErrPath(version db.Migration) string {
35
35
36
36
// getVersionSQL takes a path to an SQL file and returns it from embed.FS
37
37
// a slice of strings separated by newlines
38
- func getVersionSQL (name string ) (queries []string ) {
38
+ func getVersionSQL (name string , ignoreErrors bool ) (queries []string ) {
39
39
sql , err := dbAssets .ReadFile (path .Join ("migrations" , name ))
40
40
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
+ }
42
47
}
43
48
queries = strings .Split (strings .ReplaceAll (string (sql ), ";\r \n " , ";\n " ), ";\n " )
44
49
for i := range queries {
@@ -152,7 +157,7 @@ func (d *SqlDb) ApplyMigration(migration db.Migration) error {
152
157
return err
153
158
}
154
159
155
- queries := getVersionSQL (getVersionPath (migration ))
160
+ queries := getVersionSQL (getVersionPath (migration ), false )
156
161
for i , query := range queries {
157
162
fmt .Printf ("\r [%d/%d]" , i + 1 , len (query ))
158
163
@@ -207,13 +212,21 @@ func (d *SqlDb) TryRollbackMigration(version db.Migration) {
207
212
}
208
213
209
214
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 {
211
224
_ = tx .Rollback ()
212
225
log .Error (err )
213
226
}
214
227
}()
215
228
216
- queries := getVersionSQL (getVersionErrPath (version ))
229
+ queries := getVersionSQL (getVersionErrPath (version ), true )
217
230
218
231
for _ , query := range queries {
219
232
fmt .Printf (" [ROLLBACK] > %v\n " , query )
0 commit comments