MCPcopy Index your code
hub / github.com/coder/coder / CheckLatestVersion

Function CheckLatestVersion

coderd/database/migrations/migrate.go:201–234  ·  view source on GitHub ↗

Returns nil if currentVersion corresponds to the latest available migration, otherwise an error explaining why not.

(sourceDriver source.Driver, currentVersion uint)

Source from the content-addressed store, hash-verified

199// Returns nil if currentVersion corresponds to the latest available migration,
200// otherwise an error explaining why not.
201func 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//

Callers 2

TestCheckLatestVersionFunction · 0.92
EnsureCleanFunction · 0.85

Calls 3

NextMethod · 0.65
ErrorfMethod · 0.45
IsMethod · 0.45

Tested by 1

TestCheckLatestVersionFunction · 0.74