Skip to content

Commit d79b3a7

Browse files
fix(server): update Mongo index creation logic (#115)
1 parent 703e022 commit d79b3a7

2 files changed

Lines changed: 56 additions & 5 deletions

File tree

server/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ require (
2323
github.com/kennygrant/sanitize v1.2.4
2424
github.com/labstack/echo/v4 v4.11.4
2525
github.com/mitchellh/mapstructure v1.5.0
26-
github.com/oklog/ulid v1.3.1
2726
github.com/paulmach/go.geojson v1.4.0
2827
github.com/pkg/errors v0.9.1
2928
github.com/ravilushqa/otelgqlgen v0.15.0
@@ -117,6 +116,7 @@ require (
117116
github.com/mattn/go-isatty v0.0.20 // indirect
118117
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
119118
github.com/nicksnyder/go-i18n/v2 v2.4.0 // indirect
119+
github.com/oklog/ulid v1.3.1 // indirect
120120
github.com/opentracing/opentracing-go v1.2.0 // indirect
121121
github.com/pmezard/go-difflib v1.0.0 // indirect
122122
github.com/rs/cors v1.10.1 // indirect

server/internal/infrastructure/mongo/container.go

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mongo
22

33
import (
44
"context"
5+
"strings"
56

67
"github.com/reearth/reearth/server/internal/infrastructure/adapter"
78
"github.com/reearth/reearth/server/internal/infrastructure/memory"
@@ -22,6 +23,7 @@ import (
2223
"github.com/samber/lo"
2324
"go.mongodb.org/mongo-driver/bson"
2425
"go.mongodb.org/mongo-driver/mongo"
26+
"go.mongodb.org/mongo-driver/mongo/options"
2527
)
2628

2729
func New(ctx context.Context, db *mongo.Database, account *accountrepo.Container, useTransaction bool) (*repo.Container, error) {
@@ -170,9 +172,58 @@ func applyOptionalSceneFilter(filter interface{}, ids scene.IDList) interface{}
170172
}
171173

172174
func createIndexes(ctx context.Context, c *mongox.ClientCollection, keys, uniqueKeys []string) error {
173-
created, deleted, err := c.Indexes(ctx, keys, uniqueKeys)
174-
if len(created) > 0 || len(deleted) > 0 {
175-
log.Infofc(ctx, "mongo: %s: index deleted: %v, created: %v\n", c.Client().Name(), deleted, created)
175+
return createIndexesOnly(ctx, c, keys, uniqueKeys)
176+
}
177+
178+
// createIndexesOnly creates indexes without dropping any existing ones
179+
func createIndexesOnly(ctx context.Context, c *mongox.ClientCollection, keys, uniqueKeys []string) error {
180+
coll := c.Client()
181+
182+
// Create regular indexes
183+
for _, key := range keys {
184+
indexKeys := bson.D{}
185+
for _, k := range strings.Split(key, ",") {
186+
k = strings.TrimSpace(k)
187+
if k != "" {
188+
indexKeys = append(indexKeys, bson.E{Key: k, Value: 1})
189+
}
190+
}
191+
if len(indexKeys) > 0 {
192+
indexModel := mongo.IndexModel{
193+
Keys: indexKeys,
194+
}
195+
if _, err := coll.Indexes().CreateOne(ctx, indexModel); err != nil {
196+
// Ignore error if index already exists
197+
if !strings.Contains(err.Error(), "already exists") && !strings.Contains(err.Error(), "IndexKeySpecsConflict") {
198+
log.Errorfc(ctx, "mongo: %s: failed to create index %v: %v\n", c.Client().Name(), indexKeys, err)
199+
}
200+
}
201+
}
176202
}
177-
return err
203+
204+
// Create unique indexes
205+
for _, key := range uniqueKeys {
206+
indexKeys := bson.D{}
207+
for _, k := range strings.Split(key, ",") {
208+
k = strings.TrimSpace(k)
209+
if k != "" {
210+
indexKeys = append(indexKeys, bson.E{Key: k, Value: 1})
211+
}
212+
}
213+
if len(indexKeys) > 0 {
214+
indexModel := mongo.IndexModel{
215+
Keys: indexKeys,
216+
Options: options.Index().SetUnique(true),
217+
}
218+
if _, err := coll.Indexes().CreateOne(ctx, indexModel); err != nil {
219+
// Ignore error if index already exists
220+
if !strings.Contains(err.Error(), "already exists") && !strings.Contains(err.Error(), "IndexKeySpecsConflict") {
221+
log.Errorfc(ctx, "mongo: %s: failed to create unique index %v: %v\n", c.Client().Name(), indexKeys, err)
222+
}
223+
}
224+
}
225+
}
226+
227+
log.Infofc(ctx, "mongo: %s: ensured indexes exist (without dropping any)\n", c.Client().Name())
228+
return nil
178229
}

0 commit comments

Comments
 (0)