Skip to content

Commit 7c31f39

Browse files
fix(server): replace accountmongo.New with wrapper to prevent index deletion [VIZ-2228] (#119)
1 parent 549a549 commit 7c31f39

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

server/internal/app/repo.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/reearth/reearth/server/internal/infrastructure/s3"
1414
"github.com/reearth/reearth/server/internal/usecase/gateway"
1515
"github.com/reearth/reearth/server/internal/usecase/repo"
16-
"github.com/reearth/reearthx/account/accountinfrastructure/accountmongo"
1716
"github.com/reearth/reearthx/account/accountusecase/accountgateway"
1817
"github.com/reearth/reearthx/account/accountusecase/accountrepo"
1918
"github.com/reearth/reearthx/log"
@@ -56,12 +55,12 @@ func initReposAndGateways(ctx context.Context, conf *config.Config, debug bool)
5655
if err != nil {
5756
log.Fatalf("mongo error: %+v\n", err)
5857
}
59-
accountUsers = append(accountUsers, accountmongo.NewUserWithHost(mongox.NewClient(accountDatabase, c), u.Name))
58+
accountUsers = append(accountUsers, mongorepo.NewUserWithHost(mongox.NewClient(accountDatabase, c), u.Name))
6059
}
6160

6261
txAvailable := mongox.IsTransactionAvailable(conf.DB)
6362

64-
accountRepos, err := accountmongo.New(ctx, client, accountDatabase, txAvailable, accountRepoCompat, accountUsers)
63+
accountRepos, err := mongorepo.NewAccountRepoContainer(ctx, client, accountDatabase, txAvailable, accountRepoCompat, accountUsers)
6564
if err != nil {
6665
log.Fatalf("Failed to init mongo: %+v\n", err)
6766
}

server/internal/infrastructure/mongo/container.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,51 @@ func createIndexesOnly(ctx context.Context, c *mongox.ClientCollection, keys, un
226226
log.Infofc(ctx, "mongo: %s: ensured indexes exist (without dropping any)\n", c.Client().Name())
227227
return nil
228228
}
229+
230+
// NewAccountRepoContainer creates account repositories using wrappers that prevent index dropping
231+
func NewAccountRepoContainer(ctx context.Context, client *mongo.Client, database string, txAvailable, accountRepoCompat bool, accountUsers []accountrepo.User) (*accountrepo.Container, error) {
232+
mongoxClient := mongox.NewClient(database, client)
233+
if txAvailable {
234+
mongoxClient = mongoxClient.WithTransaction()
235+
}
236+
237+
var ws accountrepo.Workspace
238+
if accountRepoCompat {
239+
ws = NewWorkspaceWrapper(mongoxClient)
240+
} else {
241+
ws = NewWorkspaceWrapper(mongoxClient)
242+
}
243+
244+
container := &accountrepo.Container{
245+
Workspace: ws,
246+
User: NewUserWrapper(mongoxClient),
247+
Transaction: mongoxClient.Transaction(),
248+
Users: accountUsers,
249+
Role: NewRoleWrapper(mongoxClient),
250+
Permittable: NewPermittableWrapper(mongoxClient),
251+
}
252+
253+
if err := initAccountWrappers(container); err != nil {
254+
return nil, err
255+
}
256+
257+
return container, nil
258+
}
259+
260+
func initAccountWrappers(r *accountrepo.Container) error {
261+
if r == nil {
262+
return nil
263+
}
264+
265+
ctx := context.Background()
266+
return util.Try(
267+
r.Workspace.(*WorkspaceWrapper).Init,
268+
r.User.(*UserWrapper).Init,
269+
func() error { return r.Role.(*RoleWrapper).Init(ctx) },
270+
func() error { return r.Permittable.(*PermittableWrapper).Init(ctx) },
271+
)
272+
}
273+
274+
func NewUserWithHost(client *mongox.Client, host string) accountrepo.User {
275+
return NewUserWrapper(client)
276+
}

0 commit comments

Comments
 (0)