-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
109 lines (94 loc) · 2.78 KB
/
db.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
package utils
import (
"context"
"fmt"
"reflect"
"regexp"
"strings"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Migrate function will create the collections if they do not exist.
// Along with creating the collections, it will also create indexes on the collections.
//
// TODO: Add support for composite keys
func Migrate(ctx context.Context, db *mongo.Database, models ...interface{}) (map[string]*mongo.Collection, error) {
collections := make(map[string]*mongo.Collection)
for _, model := range models {
modelType := reflect.TypeOf(model)
modelName := strings.ToLower(modelType.Name())
exists, err := collectionExists(ctx, db, modelName)
if err != nil {
return nil, err
}
// If the collection does not exist, create it.
if !exists {
err := db.CreateCollection(ctx, modelName)
if err != nil {
return nil, err
}
}
// Get the collection.
collection := db.Collection(modelName)
collections[modelName] = collection
loop:
for i := 0; i < modelType.NumField(); i++ {
field := modelType.Field(i)
fieldName := strings.ToLower(field.Name)
fieldTag := field.Tag.Get("mongo")
fieldOptions := strings.Split(fieldTag, ",")
if len(fieldOptions) == 0 {
continue loop
}
// Check if the index already exists
indexes, err := collection.Indexes().ListSpecifications(ctx)
if err != nil {
return nil, err
}
// regex to check if index such as "fieldName_1" exists
re := regexp.MustCompile(fmt.Sprintf(`^%s_\d+`, fieldName))
for _, index := range indexes {
if re.Match([]byte(index.Name)) {
continue loop
}
// Check if existing index is unique
if (index.Unique != nil) && (strings.Contains(fieldTag, "unique")) {
continue loop
}
}
indexOptions := options.Index()
for _, option := range fieldOptions {
switch option {
case "unique":
indexOptions = indexOptions.SetUnique(true)
case "sparse":
indexOptions = indexOptions.SetSparse(true)
// TODO: add more options for max,min,expire
}
}
// Create the index with the options
_, err = collection.Indexes().CreateOne(ctx, mongo.IndexModel{
Keys: bson.M{fieldName: 1},
Options: indexOptions,
})
if err != nil {
return nil, err
}
}
}
return collections, nil
}
// collectionExists checks if a collection exists in the database.
func collectionExists(ctx context.Context, db *mongo.Database, collection string) (bool, error) {
names, err := db.ListCollectionNames(ctx, bson.M{})
if err != nil {
return false, err
}
for _, name := range names {
if name == collection {
return true, nil
}
}
return false, nil
}