@@ -366,9 +366,6 @@ func PlanMigration(db *sql.DB, dialect string, m MigrationSource, dir MigrationD
366366 }
367367 // Figure out which migrations to apply
368368 toApply := ToApply (migrations , existingMigrations , dir )
369- if err != nil {
370- return nil , nil , err
371- }
372369 toApplyCount := len (toApply )
373370 if max > 0 && max < toApplyCount {
374371 toApplyCount = max
@@ -397,7 +394,7 @@ func PlanMigration(db *sql.DB, dialect string, m MigrationSource, dir MigrationD
397394
398395// Searches the arrays for the latest elements with common ids.
399396// Returns the indexes of the common id.
400- func lastCommon (left []* Migration , right []* Migration ) (int , int ) {
397+ func lastCommonMigration (left []* Migration , right []* Migration ) (int , int ) {
401398 xIndexMatch := - 1
402399 yIndexMatch := 0
403400 for i := 0 ; i < len (left ); i ++ {
@@ -431,38 +428,48 @@ func toApplyUp(migrations, existingMigrations []*Migration) []*Migration {
431428 if len (existingMigrations ) == 0 {
432429 return migrations
433430 }
434- _ , index := lastCommon (existingMigrations , migrations )
431+ // All migrations after the last common migration need to be applied to the db.
432+ // It's possible that there are some existingMigrations unknown to us--
433+ // we'll just ignore them and hope they don't matter.
434+ _ , index := lastCommonMigration (existingMigrations , migrations )
435435 return migrations [index + 1 :]
436436}
437437
438438func toApplyDown (migrations , existingMigrations []* Migration ) []* Migration {
439- if len (existingMigrations ) == - 1 {
439+ if len (existingMigrations ) == 0 {
440440 return []* Migration {}
441441 }
442- // When a Migration appears in both existingMigrations and migrations,
443- // we want the Migration from migrations, since only that list has
444- // the Up and Down fields set.
442+ allMigrations := mergeMigrations (existingMigrations , migrations )
443+ _ , index := lastCommonMigration (existingMigrations , allMigrations )
444+ // Add in reverse order
445+ toApply := make ([]* Migration , index + 1 )
446+ for i := 0 ; i < index + 1 ; i ++ {
447+ toApply [index - i ] = allMigrations [i ]
448+ }
449+ return toApply
450+ }
451+
452+ // Returns a list of all migrations in either list, sorted by Id.
453+ // Migrations present only in existingMigrations will not have Up or Down set.
454+ func mergeMigrations (existingMigrations , migrations []* Migration ) []* Migration {
445455 migrationsMap := map [string ]* Migration {}
446456 for _ , m := range existingMigrations {
447- // existingMigrations will not have Up or Down set
448457 migrationsMap [m .Id ] = m
449458 }
459+ // When a Migration appears in both existingMigrations and migrations,
460+ // we want the Migration from migrations, since only that list has
461+ // the Up and Down fields set
450462 for _ , m := range migrations {
451463 migrationsMap [m .Id ] = m
452464 }
465+
466+ // Flatten our map back into a list that we can sort
453467 allMigrations := make ([]* Migration , 0 , len (migrationsMap ))
454468 for _ , m := range migrationsMap {
455469 allMigrations = append (allMigrations , m )
456470 }
457471 sort .Sort (byId (allMigrations ))
458472
459- _ , index := lastCommon (existingMigrations , allMigrations )
460- // Add in reverse order
461- toApply := make ([]* Migration , index + 1 )
462- for i := 0 ; i < index + 1 ; i ++ {
463- toApply [index - i ] = allMigrations [i ]
464- }
465- return toApply
466473}
467474
468475func ToCatchup (migrations , existingMigrations []* Migration , lastRun * Migration ) []* PlannedMigration {
0 commit comments