From be1cdc3a1aa9034e84d311e54e4475e1cfec1dcb Mon Sep 17 00:00:00 2001 From: jprgic Date: Fri, 24 Feb 2023 08:52:46 +0100 Subject: [PATCH] add ensure custom index function Needed to create index for todo: https://3.basecamp.com/3077084/buckets/31145053/todos/5730629231 and for mongo driver upgrade on bonovi service: https://3.basecamp.com/3077084/buckets/10103289/messages/5732789325/\#__recording_5807541042 --- pkg/mdb2/mdb.go | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/pkg/mdb2/mdb.go b/pkg/mdb2/mdb.go index d6294dc..63e225a 100644 --- a/pkg/mdb2/mdb.go +++ b/pkg/mdb2/mdb.go @@ -366,17 +366,27 @@ func (mdb *Mdb) ResetIndexCache() { } // EnsureIndex creates sparse index if it doesn't already exist -// NOTE: the index created will be sparse. If you need basic index see EnsureNonSparseIndex +// The index will by default be sparse and built in background func (mdb *Mdb) EnsureIndex(col string, key []string, expireAfter time.Duration) error { - return mdb.ensureIndex(col, key, expireAfter, true) + opts := options.Index(). + SetBackground(true). + SetSparse(true) + if expireAfter > 0 { + opts.SetExpireAfterSeconds(int32(expireAfter / time.Second)) + } + + return mdb.ensureIndex(col, key, opts) } -// EnsureNonSparseIndex creates non-sparse index if it doesn't already exist -func (mdb *Mdb) EnsureNonSparseIndex(col string, key []string, expireAfter time.Duration) error { - return mdb.ensureIndex(col, key, expireAfter, false) +// EnsureCustomIndex creates index with the specified options if it doesn't already exist +// NOTE: IndexOptions#Name will always be overridden to obey legacy naming system derived from key values +func (mdb *Mdb) EnsureCustomIndex(col string, key []string, options *options.IndexOptions) error { + return mdb.ensureIndex(col, key, options) } -func (mdb *Mdb) ensureIndex(col string, key []string, expireAfter time.Duration, sparse bool) error { +// ensureIndex ensures index exist. Uses legacy key parsing for backward compatibility meaning that +// name is automatically set from the key values +func (mdb *Mdb) ensureIndex(col string, key []string, indexOptions *options.IndexOptions) error { c := mdb.db.Collection(col) parsedKeys, err := parseIndexKey(key) if err != nil { @@ -384,14 +394,8 @@ func (mdb *Mdb) ensureIndex(col string, key []string, expireAfter time.Duration, } index := mongo.IndexModel{} index.Keys = parsedKeys.key - options := options.Index(). - SetBackground(true). - SetSparse(sparse). - SetName(parsedKeys.name) - if expireAfter > 0 { - options.SetExpireAfterSeconds(int32(expireAfter / time.Second)) - } - index.Options = options + index.Options = indexOptions.SetName(parsedKeys.name) + _, err = c.Indexes().CreateOne(context.Background(), index) return err }