Skip to content

Commit 60514d0

Browse files
committed
Add EnsureParanoidIndices method
1 parent 7ac84bf commit 60514d0

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

mongo/document/base.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ type Base struct {
1313
UpdatedAt time.Time `bson:"updated_at" json:"updated_at"`
1414
}
1515

16-
func (d Base) IsPersisted() bool {
16+
func (d *Base) IsPersisted() bool {
1717
return !d.CreatedAt.IsZero()
1818
}
1919

20-
func (d Base) getID() bson.ObjectId {
20+
func (d *Base) getID() bson.ObjectId {
2121
return d.ID
2222
}
2323

mongo/document/document.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"time"
77

8+
"github.com/sirupsen/logrus"
9+
810
"gopkg.in/mgo.v2"
911
"gopkg.in/mgo.v2/bson"
1012

@@ -22,14 +24,23 @@ type document interface {
2224
Validable
2325
}
2426

27+
var _ document = &Base{}
28+
var _ document = &Paranoid{}
29+
2530
type scopable interface {
2631
scope(bson.M) bson.M
2732
}
2833

34+
var _ scopable = &Base{}
35+
var _ scopable = &Paranoid{}
36+
2937
type destroyable interface {
3038
destroy(ctx context.Context, collectionName string) error
3139
}
3240

41+
var _ destroyable = &Base{}
42+
var _ destroyable = &Paranoid{}
43+
3344
type Closer interface {
3445
Close()
3546
}
@@ -38,6 +49,8 @@ type Validable interface {
3849
Validate(ctx context.Context) *ValidationErrors
3950
}
4051

52+
var _ Validable = &Base{}
53+
4154
// Create inser the document in the database, returns an error if document already exists and set CreatedAt timestamp
4255
func Create(ctx context.Context, collectionName string, doc document) error {
4356
log := logger.Get(ctx)
@@ -221,3 +234,26 @@ func Update(ctx context.Context, collectionName string, update bson.M, doc docum
221234
log.WithField("query", update).Debugf("update %v", collectionName)
222235
return c.UpdateId(doc.getID(), update)
223236
}
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+
}

mongo/document/paranoid.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ type Paranoid struct {
1212
DeletedAt *time.Time `bson:"deleted_at,omitempty" json:"deleted_at,omitempty"`
1313
}
1414

15-
func (p Paranoid) IsDeleted() bool {
15+
func (p *Paranoid) IsDeleted() bool {
1616
return p.DeletedAt != nil && !p.DeletedAt.IsZero()
1717
}
1818

1919
func (d *Paranoid) setDeletedAt(t time.Time) {
2020
d.DeletedAt = &t
2121
}
2222

23-
func (d Paranoid) scope(query bson.M) bson.M {
23+
func (d *Paranoid) scope(query bson.M) bson.M {
2424
if _, ok := query["deleted_at"]; !ok {
2525
query["deleted_at"] = nil
2626
}

0 commit comments

Comments
 (0)