Skip to content

Add EnsureParanoidIndices method #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 29, 2019
Merged

Add EnsureParanoidIndices method #49

merged 6 commits into from
Aug 29, 2019

Conversation

EtienneM
Copy link
Member

@EtienneM EtienneM commented Aug 13, 2018

Is it what you meant @johnsudaar ?

Fix #12

Copy link
Member

@Soulou Soulou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't switch everything to pointer, there is no need

@@ -13,11 +13,11 @@ type Base struct {
UpdatedAt time.Time `bson:"updated_at" json:"updated_at"`
}

func (d Base) IsPersisted() bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, no need of pointer, nothing is modified

return !d.CreatedAt.IsZero()
}

func (d Base) getID() bson.ObjectId {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need of pointer, nothing is modified

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then document Base does not satisfy the document interface as getID is implemented for Base but the ensure* methods are for pointer of Base.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go is smarter than that :P If the struct is passed as a pointer it can access non-pointer methods: https://play.golang.com/p/NicC6AlMbd7

(Not the over way around though: https://play.golang.com/p/o-eDYPih2V4)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep but interface does not work that way. If you have the following struct and interface:

type T struct {}

type I interface {
  A()
  B()
}

And you want the struct T to implement the interface I, you can NOT do the following:

func (t T) A() {}
func (t *T) B() {}

With this example, the struct T does not satisfy the interface I

@@ -12,15 +12,15 @@ type Paranoid struct {
DeletedAt *time.Time `bson:"deleted_at,omitempty" json:"deleted_at,omitempty"`
}

func (p Paranoid) IsDeleted() bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No pointer

return p.DeletedAt != nil
}

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

func (d Paranoid) scope(query bson.M) bson.M {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No pointer

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

var _ document = &Base{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No pointer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Base implementation of the document interface is on pointer. Cf. https://github.com/Scalingo/go-utils/pull/49/files#diff-9732770faadda6716a617078c80ccbe9L24

@@ -221,3 +234,26 @@ func Update(ctx context.Context, collectionName string, update bson.M, doc docum
log.WithField("query", update).Debugf("update %v", collectionName)
return c.UpdateId(doc.getID(), update)
}

func EnsureParanoidIndices(ctx context.Context, collectionNames ...string) {
go func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why in a goroutine?

@EtienneM
Copy link
Member Author

EtienneM commented Sep 28, 2018

I think it's mandatory to have everything as a pointer. For instance, the document interface requires, among other, the following two methods:

setUpdatedAt(time.Time)
getID() bson.ObjectId

The Base structure implements this interface. setUpdatedAt requires to modify the Base instance. getID does not. But if I the receiver of setUpdatedAt is a pointer and getID is not, Base does not implement the document interface (https://play.golang.org/p/CqjoJPV3b8d).

@EtienneM EtienneM force-pushed the fix/12/missing_index branch from d8b7c4b to cb0a6aa Compare August 29, 2019 06:41
@EtienneM EtienneM requested a review from johnsudaar August 29, 2019 06:46
@johnsudaar johnsudaar merged commit 3501a14 into master Aug 29, 2019
@EtienneM EtienneM deleted the fix/12/missing_index branch June 2, 2021 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing index on the deleted_at field
3 participants