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,7 +49,10 @@ type Validable interface {
38
49
Validate (ctx context.Context ) * ValidationErrors
39
50
}
40
51
41
- // Create inser the document in the database, returns an error if document already exists and set CreatedAt timestamp
52
+ var _ Validable = & Base {}
53
+
54
+ // Create inserts the document in the database, returns an error if document
55
+ // already exists and set CreatedAt timestamp
42
56
func Create (ctx context.Context , collectionName string , doc document ) error {
43
57
log := logger .Get (ctx )
44
58
doc .ensureID ()
@@ -221,3 +235,24 @@ func Update(ctx context.Context, collectionName string, update bson.M, doc docum
221
235
log .WithField ("query" , update ).Debugf ("update %v" , collectionName )
222
236
return c .UpdateId (doc .getID (), update )
223
237
}
238
+
239
+ func EnsureParanoidIndices (ctx context.Context , collectionNames ... string ) {
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
+ continue
256
+ }
257
+ }
258
+ }
0 commit comments