diff --git a/server/internal/infrastructure/mongo/container.go b/server/internal/infrastructure/mongo/container.go index 0780a5e91..4fbba864f 100644 --- a/server/internal/infrastructure/mongo/container.go +++ b/server/internal/infrastructure/mongo/container.go @@ -59,7 +59,7 @@ func New(ctx context.Context, db *mongo.Database, account *accountrepo.Container Storytelling: NewStorytelling(client), Lock: lock, Transaction: client.Transaction(), - Workspace: account.Workspace, + Workspace: NewWorkspaceWrapper(client), User: account.User, } @@ -136,7 +136,7 @@ func Init(r *repo.Container) error { func() error { return r.Scene.(*Scene).Init(ctx) }, func() error { return r.Tag.(*Tag).Init(ctx) }, func() error { return r.User.(*accountmongo.User).Init() }, - func() error { return r.Workspace.(*accountmongo.Workspace).Init() }, + func() error { return r.Workspace.(*WorkspaceWrapper).Init() }, ) } diff --git a/server/internal/infrastructure/mongo/workspace_wrapper.go b/server/internal/infrastructure/mongo/workspace_wrapper.go new file mode 100644 index 000000000..10c103de0 --- /dev/null +++ b/server/internal/infrastructure/mongo/workspace_wrapper.go @@ -0,0 +1,35 @@ +package mongo + +import ( + "context" + + "github.com/reearth/reearthx/account/accountinfrastructure/accountmongo" + "github.com/reearth/reearthx/account/accountusecase/accountrepo" + "github.com/reearth/reearthx/mongox" +) + +// WorkspaceWrapper wraps accountmongo.Workspace to prevent index dropping +type WorkspaceWrapper struct { + *accountmongo.Workspace + client *mongox.Collection +} + +// NewWorkspaceWrapper creates a workspace wrapper that doesn't drop indexes +func NewWorkspaceWrapper(client *mongox.Client) accountrepo.Workspace { + // Create the original workspace + original := accountmongo.NewWorkspace(client) + + return &WorkspaceWrapper{ + Workspace: original.(*accountmongo.Workspace), + client: client.WithCollection("workspace"), + } +} + +// Init override to prevent index dropping - just create indexes without dropping existing ones +func (w *WorkspaceWrapper) Init() error { + ctx := context.Background() + + // Only create the "id" unique index that the original workspace expects + // without calling the original Init() which would drop existing indexes + return createIndexesOnly(ctx, w.client, nil, []string{"id"}) +}