From a7a30c3bc6be1bb86be3412a9d75921aa4ac8407 Mon Sep 17 00:00:00 2001 From: wilfredmulenga Date: Wed, 24 Dec 2025 10:17:43 +0900 Subject: [PATCH] Introduces WorkspaceWrapper to prevent index dropping Replaces direct usage of accountmongo.Workspace with a WorkspaceWrapper implementation to prevent unintentional dropping of indexes during initialization. The WorkspaceWrapper overrides the Init method to create required indexes without removing existing ones, addressing potential data integrity issues. Includes the creation of a new workspace_wrapper.go file for this functionality. --- .../infrastructure/mongo/container.go | 4 +-- .../infrastructure/mongo/workspace_wrapper.go | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 server/internal/infrastructure/mongo/workspace_wrapper.go 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"}) +}