// ShouldRun decides if the migration should be applied or removed, based on // the direction and desired version to reach. func ShouldRun(tx *sql.Tx, migration string, direction Direction, desiredVersion int) bool { version, err := Revision(migration) if err != nil { glog.Warningf("Unable to determine the revision of %s", migration) return false } switch direction { case Up: return IsUp(version, desiredVersion) && !IsMigrated(tx, migration) case Down: return IsDown(version, desiredVersion) && IsMigrated(tx, migration) } return false }
// LatestRevision returns the latest revision available from the SQL files in // the migrations directory. func LatestRevision(directory string) int { migrations, err := Available(directory, Down) if err != nil { println(err.Error()) return 0 } if len(migrations) == 0 { return 0 } // Find a valid filename for _, filename := range migrations { rev, err := Revision(filename) if err != nil { glog.Warningf("Invalid migration: %s", filename) continue } return rev } return 0 }