forked from xakep666/mongo-migrate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
migration.go
43 lines (37 loc) · 1022 Bytes
/
migration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package migrate
import (
"context"
"sort"
"go.mongodb.org/mongo-driver/mongo"
)
// MigrationFunc used to define actions to be performed for a migration.
type MigrationFunc func(ctx context.Context, db *mongo.Database) error
// Migration represents single database migration.
// Migration contains:
//
// - version: migration version, must be unique in migration list
//
// - description: text description of migration
//
// - up: callback which will be called in "up" migration process
//
// - down: callback which will be called in "down" migration process for reverting changes
type Migration struct {
Version uint64
Description string
Up MigrationFunc
Down MigrationFunc
}
func migrationSort(migrations []Migration) {
sort.Slice(migrations, func(i, j int) bool {
return migrations[i].Version < migrations[j].Version
})
}
func hasVersion(migrations []Migration, version uint64) bool {
for _, m := range migrations {
if m.Version == version {
return true
}
}
return false
}