forked from xakep666/mongo-migrate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
global_migrate.go
113 lines (100 loc) · 3.01 KB
/
global_migrate.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package migrate
import (
"context"
"fmt"
"runtime"
"go.mongodb.org/mongo-driver/mongo"
)
var globalMigrate = NewMigrate(nil)
func internalRegister(up, down MigrationFunc, skip int) error {
_, file, _, _ := runtime.Caller(skip)
version, description, err := extractVersionDescription(file)
if err != nil {
return err
}
if hasVersion(globalMigrate.migrations, version) {
return fmt.Errorf("migration with version %v already registered", version)
}
globalMigrate.migrations = append(globalMigrate.migrations, Migration{
Version: version,
Description: description,
Up: up,
Down: down,
})
return nil
}
// Register performs migration registration.
// Use case of this function:
//
// - Create a file called like "1_setup_indexes.go" ("<version>_<comment>.go").
//
// - Use the following template inside:
//
// package migrations
//
// import (
// "go.mongodb.org/mongo-driver/bson"
// "go.mongodb.org/mongo-driver/mongo"
// "go.mongodb.org/mongo-driver/mongo/options"
// migrate "github.com/xakep666/mongo-migrate"
// )
//
// func init() {
// migrate.Register(func(db *mongo.Database) error {
// opt := options.Index().SetName("my-index")
// keys := bson.D{{Key: "my-key", Value: 1}}
// model := mongo.IndexModel{Keys: keys, Options: opt}
// _, err := db.Collection("my-coll").Indexes().CreateOne(context.TODO(), model)
// if err != nil {
// return err
// }
// return nil
// }, func(db *mongo.Database) error {
// _, err := db.Collection("my-coll").Indexes().DropOne(context.TODO(), "my-index")
// if err != nil {
// return err
// }
// return nil
// })
// }
func Register(up, down MigrationFunc) error {
return internalRegister(up, down, 2)
}
// MustRegister acts like Register but panics on errors.
func MustRegister(up, down MigrationFunc) {
if err := internalRegister(up, down, 2); err != nil {
panic(err)
}
}
// RegisteredMigrations returns all registered migrations.
func RegisteredMigrations() []Migration {
ret := make([]Migration, len(globalMigrate.migrations))
copy(ret, globalMigrate.migrations)
return ret
}
// SetDatabase sets database for global migrate.
func SetDatabase(db *mongo.Database) {
globalMigrate.db = db
}
// SetMigrationsCollection changes default collection name for migrations history.
func SetMigrationsCollection(name string) {
globalMigrate.SetMigrationsCollection(name)
}
// Version returns current database version.
func Version(ctx context.Context) (uint64, string, error) {
return globalMigrate.Version(ctx)
}
// Up performs "up" migration using registered migrations.
// Detailed description available in Migrate.Up().
func Up(ctx context.Context, n int) error {
return globalMigrate.Up(ctx, n)
}
// Down performs "down" migration using registered migrations.
// Detailed description available in Migrate.Down().
func Down(ctx context.Context, n int) error {
return globalMigrate.Down(ctx, n)
}
// SetLogger sets a logger to print the migration process
func SetLogger(log Logger) {
globalMigrate.SetLogger(log)
}