-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.go
100 lines (89 loc) · 1.97 KB
/
database.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
package main
import (
"fmt"
"log"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
)
func newDatabaseConnection(hostname string) (session *mgo.Session) {
session, err := mgo.Dial(hostname)
if err != nil {
log.Fatal(err)
}
return
}
func setUpDatabase(session *mgo.Session) {
session.SetMode(mgo.Monotonic, true)
users := session.DB("guardian").C("users")
validations := session.DB("guardian").C("validations")
err := users.DropAllIndexes()
if err != nil {
log.Fatal(err)
}
index := mgo.Index{
Key: []string{"tokenHardExpire"},
Unique: true,
DropDups: true,
Background: true,
ExpireAfter: 1,
}
err = validations.EnsureIndex(index)
if err != nil {
fmt.Println(err)
}
index = mgo.Index{
Key: []string{"number"},
Unique: true,
DropDups: true,
Background: true,
PartialFilter: bson.M{"number": bson.M{"$type": "string"}},
}
err = users.EnsureIndex(index)
if err != nil {
fmt.Println(err)
}
index = mgo.Index{
Key: []string{"email"},
Unique: true,
DropDups: true,
Background: true,
PartialFilter: bson.M{"email": bson.M{"$type": "string"}},
}
err = users.EnsureIndex(index)
if err != nil {
fmt.Println(err)
}
index = mgo.Index{
Key: []string{"username"},
Unique: true,
DropDups: true,
Background: true,
PartialFilter: bson.M{"username": bson.M{"$type": "string"}},
}
err = users.EnsureIndex(index)
if err != nil {
fmt.Println(err)
}
index = mgo.Index{
Key: []string{"services.name", "number", "email"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err = users.EnsureIndex(index)
if err != nil {
fmt.Println(err)
}
index = mgo.Index{
Key: []string{"devices.token", "devices.name", "number", "email"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err = users.EnsureIndex(index)
if err != nil {
fmt.Println(err)
}
}