Returns nil if currentVersion corresponds to the latest available migration, otherwise an error explaining why not.
(sourceDriver source.Driver, currentVersion uint)
| 199 | // Returns nil if currentVersion corresponds to the latest available migration, |
| 200 | // otherwise an error explaining why not. |
| 201 | func CheckLatestVersion(sourceDriver source.Driver, currentVersion uint) error { |
| 202 | // This is ugly, but seems like the only way to do it with the public |
| 203 | // interfaces provided by golang-migrate. |
| 204 | |
| 205 | // Check that there is no later version |
| 206 | nextVersion, err := sourceDriver.Next(currentVersion) |
| 207 | if err == nil { |
| 208 | return xerrors.Errorf("current version is %d, but later version %d exists", currentVersion, nextVersion) |
| 209 | } |
| 210 | if !errors.Is(err, os.ErrNotExist) { |
| 211 | return xerrors.Errorf("get next migration after %d: %w", currentVersion, err) |
| 212 | } |
| 213 | |
| 214 | // Once we reach this point, we know that either currentVersion doesn't |
| 215 | // exist, or it has no successor (the return value from |
| 216 | // sourceDriver.Next() is the same in either case). So we need to check |
| 217 | // that either it's the first version, or it has a predecessor. |
| 218 | |
| 219 | firstVersion, err := sourceDriver.First() |
| 220 | if err != nil { |
| 221 | // the total number of migrations should be non-zero, so this must be |
| 222 | // an actual error, not just a missing file |
| 223 | return xerrors.Errorf("get first migration: %w", err) |
| 224 | } |
| 225 | if firstVersion == currentVersion { |
| 226 | return nil |
| 227 | } |
| 228 | |
| 229 | _, err = sourceDriver.Prev(currentVersion) |
| 230 | if err != nil { |
| 231 | return xerrors.Errorf("get previous migration: %w", err) |
| 232 | } |
| 233 | return nil |
| 234 | } |
| 235 | |
| 236 | // Stepper returns a function that runs SQL migrations one step at a time. |
| 237 | // |