Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ require (
github.com/kennygrant/sanitize v1.2.4
github.com/labstack/echo/v4 v4.11.4
github.com/mitchellh/mapstructure v1.5.0
github.com/oklog/ulid v1.3.1
github.com/paulmach/go.geojson v1.4.0
github.com/pkg/errors v0.9.1
github.com/ravilushqa/otelgqlgen v0.15.0
Expand Down Expand Up @@ -117,6 +116,7 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/nicksnyder/go-i18n/v2 v2.4.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v1.10.1 // indirect
Expand Down
59 changes: 55 additions & 4 deletions server/internal/infrastructure/mongo/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mongo

import (
"context"
"strings"

"github.com/reearth/reearth/server/internal/infrastructure/adapter"
"github.com/reearth/reearth/server/internal/infrastructure/memory"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/samber/lo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

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

func createIndexes(ctx context.Context, c *mongox.ClientCollection, keys, uniqueKeys []string) error {
created, deleted, err := c.Indexes(ctx, keys, uniqueKeys)
if len(created) > 0 || len(deleted) > 0 {
log.Infofc(ctx, "mongo: %s: index deleted: %v, created: %v\n", c.Client().Name(), deleted, created)
return createIndexesOnly(ctx, c, keys, uniqueKeys)
}

// createIndexesOnly creates indexes without dropping any existing ones
func createIndexesOnly(ctx context.Context, c *mongox.ClientCollection, keys, uniqueKeys []string) error {
coll := c.Client()

// Create regular indexes
for _, key := range keys {
indexKeys := bson.D{}
for _, k := range strings.Split(key, ",") {
k = strings.TrimSpace(k)
if k != "" {
indexKeys = append(indexKeys, bson.E{Key: k, Value: 1})
}
}
if len(indexKeys) > 0 {
indexModel := mongo.IndexModel{
Keys: indexKeys,
}
if _, err := coll.Indexes().CreateOne(ctx, indexModel); err != nil {
// Ignore error if index already exists
if !strings.Contains(err.Error(), "already exists") && !strings.Contains(err.Error(), "IndexKeySpecsConflict") {
log.Errorfc(ctx, "mongo: %s: failed to create index %v: %v\n", c.Client().Name(), indexKeys, err)
}
}
}
}
return err

// Create unique indexes
for _, key := range uniqueKeys {
indexKeys := bson.D{}
for _, k := range strings.Split(key, ",") {
k = strings.TrimSpace(k)
if k != "" {
indexKeys = append(indexKeys, bson.E{Key: k, Value: 1})
}
}
if len(indexKeys) > 0 {
indexModel := mongo.IndexModel{
Keys: indexKeys,
Options: options.Index().SetUnique(true),
}
if _, err := coll.Indexes().CreateOne(ctx, indexModel); err != nil {
// Ignore error if index already exists
if !strings.Contains(err.Error(), "already exists") && !strings.Contains(err.Error(), "IndexKeySpecsConflict") {
log.Errorfc(ctx, "mongo: %s: failed to create unique index %v: %v\n", c.Client().Name(), indexKeys, err)
}
}
}
}

log.Infofc(ctx, "mongo: %s: ensured indexes exist (without dropping any)\n", c.Client().Name())
return nil
}
Loading