-
Notifications
You must be signed in to change notification settings - Fork 0
/
collections.go
91 lines (80 loc) · 2.15 KB
/
collections.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
package functions
import (
"fmt"
"github.com/cygy/ginamite/common/log"
"github.com/cygy/ginamite/common/mongo/session"
"github.com/cygy/ginamite/plugins/mongodb/model"
"github.com/globalsign/mgo"
"github.com/sirupsen/logrus"
)
// CreateCollections : creates the collections.
func CreateCollections(collections []CollectionToCreate) {
session, db := session.Copy()
defer session.Close()
for _, collection := range collections {
err := db.C(collection.Name).Create(&collection.Info)
if err != nil {
log.WithFields(logrus.Fields{
"error": err.Error(),
"collection": collection.Name,
}).Error("create mongoDB collection")
}
}
}
// RemoveCollections : removes the collections.
func RemoveCollections(collections []string) {
session, db := session.Copy()
defer session.Close()
for _, collection := range collections {
db.C(collection).DropAllIndexes()
err := db.C(collection).DropCollection()
if err != nil && err.Error() != fmt.Sprintf("collection not found with name [%s]", collection) {
log.WithFields(logrus.Fields{
"error": err.Error(),
"collection": collection,
}).Error("delete mongoDB collection")
}
}
}
// RemoveUnusedCollections : removes the unused collections.
func RemoveUnusedCollections() {
collectionsToRemove := []string{}
RemoveCollections(collectionsToRemove)
}
// CreateAllCollections : creates the collections.
func CreateAllCollections() {
collectionInfo := mgo.CollectionInfo{
Collation: &mgo.Collation{
Locale: "fr",
Strength: 1,
NumericOrdering: false,
},
}
collections := []CollectionToCreate{
CollectionToCreate{
Name: model.AuthTokenCollection,
},
CollectionToCreate{
Name: model.DeletedUserCollection,
Info: collectionInfo,
},
CollectionToCreate{
Name: model.DisabledUserCollection,
Info: collectionInfo,
},
CollectionToCreate{
Name: model.ForgotPasswordProcessCollection,
},
CollectionToCreate{
Name: model.IPLocationCollection,
},
CollectionToCreate{
Name: model.UpdatePropertyProcessCollection,
},
CollectionToCreate{
Name: model.UserCollection,
Info: collectionInfo,
},
}
CreateCollections(collections)
}