@@ -2,6 +2,7 @@ package mongo
22
33import (
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
2729func 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
172174func 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