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
5 changes: 2 additions & 3 deletions server/internal/app/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/reearth/reearth/server/internal/infrastructure/s3"
"github.com/reearth/reearth/server/internal/usecase/gateway"
"github.com/reearth/reearth/server/internal/usecase/repo"
"github.com/reearth/reearthx/account/accountinfrastructure/accountmongo"
"github.com/reearth/reearthx/account/accountusecase/accountgateway"
"github.com/reearth/reearthx/account/accountusecase/accountrepo"
"github.com/reearth/reearthx/log"
Expand Down Expand Up @@ -56,12 +55,12 @@ func initReposAndGateways(ctx context.Context, conf *config.Config, debug bool)
if err != nil {
log.Fatalf("mongo error: %+v\n", err)
}
accountUsers = append(accountUsers, accountmongo.NewUserWithHost(mongox.NewClient(accountDatabase, c), u.Name))
accountUsers = append(accountUsers, mongorepo.NewUserWithHost(mongox.NewClient(accountDatabase, c), u.Name))
}

txAvailable := mongox.IsTransactionAvailable(conf.DB)

accountRepos, err := accountmongo.New(ctx, client, accountDatabase, txAvailable, accountRepoCompat, accountUsers)
accountRepos, err := mongorepo.NewAccountRepoContainer(ctx, client, accountDatabase, txAvailable, accountRepoCompat, accountUsers)
if err != nil {
log.Fatalf("Failed to init mongo: %+v\n", err)
}
Expand Down
48 changes: 48 additions & 0 deletions server/internal/infrastructure/mongo/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,51 @@ func createIndexesOnly(ctx context.Context, c *mongox.ClientCollection, keys, un
log.Infofc(ctx, "mongo: %s: ensured indexes exist (without dropping any)\n", c.Client().Name())
return nil
}

// NewAccountRepoContainer creates account repositories using wrappers that prevent index dropping
func NewAccountRepoContainer(ctx context.Context, client *mongo.Client, database string, txAvailable, accountRepoCompat bool, accountUsers []accountrepo.User) (*accountrepo.Container, error) {
mongoxClient := mongox.NewClient(database, client)
if txAvailable {
mongoxClient = mongoxClient.WithTransaction()
}

var ws accountrepo.Workspace
if accountRepoCompat {
ws = NewWorkspaceWrapper(mongoxClient)
} else {
ws = NewWorkspaceWrapper(mongoxClient)
}

container := &accountrepo.Container{
Workspace: ws,
User: NewUserWrapper(mongoxClient),
Transaction: mongoxClient.Transaction(),
Users: accountUsers,
Role: NewRoleWrapper(mongoxClient),
Permittable: NewPermittableWrapper(mongoxClient),
}

if err := initAccountWrappers(container); err != nil {
return nil, err
}

return container, nil
}

func initAccountWrappers(r *accountrepo.Container) error {
if r == nil {
return nil
}

ctx := context.Background()
return util.Try(
r.Workspace.(*WorkspaceWrapper).Init,
r.User.(*UserWrapper).Init,
func() error { return r.Role.(*RoleWrapper).Init(ctx) },
func() error { return r.Permittable.(*PermittableWrapper).Init(ctx) },
)
}

func NewUserWithHost(client *mongox.Client, host string) accountrepo.User {
return NewUserWrapper(client)
}
Loading