5
5
"fmt"
6
6
"time"
7
7
8
+ "github.com/sirupsen/logrus"
9
+
8
10
"gopkg.in/mgo.v2"
9
11
"gopkg.in/mgo.v2/bson"
10
12
@@ -22,14 +24,23 @@ type document interface {
22
24
Validable
23
25
}
24
26
27
+ var _ document = & Base {}
28
+ var _ document = & Paranoid {}
29
+
25
30
type scopable interface {
26
31
scope (bson.M ) bson.M
27
32
}
28
33
34
+ var _ scopable = & Base {}
35
+ var _ scopable = & Paranoid {}
36
+
29
37
type destroyable interface {
30
38
destroy (ctx context.Context , collectionName string ) error
31
39
}
32
40
41
+ var _ destroyable = & Base {}
42
+ var _ destroyable = & Paranoid {}
43
+
33
44
type Closer interface {
34
45
Close ()
35
46
}
@@ -38,6 +49,8 @@ type Validable interface {
38
49
Validate (ctx context.Context ) * ValidationErrors
39
50
}
40
51
52
+ var _ Validable = & Base {}
53
+
41
54
// Create inser the document in the database, returns an error if document already exists and set CreatedAt timestamp
42
55
func Create (ctx context.Context , collectionName string , doc document ) error {
43
56
log := logger .Get (ctx )
@@ -221,3 +234,26 @@ func Update(ctx context.Context, collectionName string, update bson.M, doc docum
221
234
log .WithField ("query" , update ).Debugf ("update %v" , collectionName )
222
235
return c .UpdateId (doc .getID (), update )
223
236
}
237
+
238
+ func EnsureParanoidIndices (ctx context.Context , collectionNames ... string ) {
239
+ go func () {
240
+ log := logger .Get (ctx )
241
+
242
+ for _ , collectionName := range collectionNames {
243
+ log := logger .Get (ctx ).WithFields (logrus.Fields {
244
+ "init" : "setup-indices" ,
245
+ "collection" : collectionName ,
246
+ })
247
+ ctx := logger .ToCtx (ctx , log )
248
+ log .Info ("Setup the MongoDB index" )
249
+
250
+ c := mongo .Session (log ).Clone ().DB ("" ).C (collectionName )
251
+ defer c .Database .Session .Close ()
252
+ err := c .EnsureIndexKey ("deleted_at" )
253
+ if err != nil {
254
+ log .WithError (err ).Error ("fail to setup the deleted_at index" )
255
+ return
256
+ }
257
+ }
258
+ }()
259
+ }
0 commit comments