Skip to content

Commit 549a549

Browse files
fix(server): add wrappers to prevent index-dropping initialization code [VIZ-2228] (#118)
1 parent 6cd8b6c commit 549a549

4 files changed

Lines changed: 92 additions & 7 deletions

File tree

server/internal/infrastructure/mongo/container.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/reearth/reearth/server/pkg/property"
1515
"github.com/reearth/reearth/server/pkg/scene"
1616
"github.com/reearth/reearthx/account/accountdomain/user"
17-
"github.com/reearth/reearthx/account/accountinfrastructure/accountmongo"
1817
"github.com/reearth/reearthx/account/accountusecase/accountrepo"
1918
"github.com/reearth/reearthx/authserver"
2019
"github.com/reearth/reearthx/log"
@@ -53,14 +52,14 @@ func New(ctx context.Context, db *mongo.Database, account *accountrepo.Container
5352
Scene: NewScene(client),
5453
Tag: NewTag(client),
5554
SceneLock: NewSceneLock(client),
56-
Permittable: accountmongo.NewPermittable(client), // TODO: Delete this once the permission check migration is complete.
55+
Permittable: NewPermittableWrapper(client), // TODO: Delete this once the permission check migration is complete.
5756
Policy: NewPolicy(client),
58-
Role: accountmongo.NewRole(client), // TODO: Delete this once the permission check migration is complete.
57+
Role: NewRoleWrapper(client), // TODO: Delete this once the permission check migration is complete.
5958
Storytelling: NewStorytelling(client),
6059
Lock: lock,
6160
Transaction: client.Transaction(),
6261
Workspace: NewWorkspaceWrapper(client),
63-
User: account.User,
62+
User: NewUserWrapper(client),
6463
}
6564

6665
// init
@@ -126,16 +125,16 @@ func Init(r *repo.Container) error {
126125
func() error { return r.Dataset.(*Dataset).Init(ctx) },
127126
func() error { return r.DatasetSchema.(*DatasetSchema).Init(ctx) },
128127
func() error { return r.Layer.(*Layer).Init(ctx) },
129-
func() error { return r.Permittable.(*accountmongo.Permittable).Init(ctx) }, // TODO: Delete this once the permission check migration is complete.
128+
func() error { return r.Permittable.(*PermittableWrapper).Init(ctx) }, // TODO: Delete this once the permission check migration is complete.
130129
func() error { return r.Plugin.(*Plugin).Init(ctx) },
131130
func() error { return r.Policy.(*Policy).Init(ctx) },
132131
func() error { return r.Project.(*Project).Init(ctx) },
133132
func() error { return r.Property.(*Property).Init(ctx) },
134133
func() error { return r.PropertySchema.(*PropertySchema).Init(ctx) },
135-
func() error { return r.Role.(*accountmongo.Role).Init(ctx) }, // TODO: Delete this once the permission check migration is complete.
134+
func() error { return r.Role.(*RoleWrapper).Init(ctx) }, // TODO: Delete this once the permission check migration is complete.
136135
func() error { return r.Scene.(*Scene).Init(ctx) },
137136
func() error { return r.Tag.(*Tag).Init(ctx) },
138-
func() error { return r.User.(*accountmongo.User).Init() },
137+
func() error { return r.User.(*UserWrapper).Init() },
139138
func() error { return r.Workspace.(*WorkspaceWrapper).Init() },
140139
)
141140
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package mongo
2+
3+
import (
4+
"context"
5+
6+
"github.com/reearth/reearthx/account/accountinfrastructure/accountmongo"
7+
"github.com/reearth/reearthx/account/accountusecase/accountrepo"
8+
"github.com/reearth/reearthx/mongox"
9+
)
10+
11+
// PermittableWrapper wraps accountmongo.Permittable to prevent index dropping
12+
type PermittableWrapper struct {
13+
*accountmongo.Permittable
14+
client *mongox.Collection
15+
}
16+
17+
func NewPermittableWrapper(client *mongox.Client) accountrepo.Permittable {
18+
original := accountmongo.NewPermittable(client)
19+
20+
return &PermittableWrapper{
21+
Permittable: original.(*accountmongo.Permittable),
22+
client: client.WithCollection("permittable"),
23+
}
24+
}
25+
26+
func (p *PermittableWrapper) Init(ctx context.Context) error {
27+
return createIndexesOnly(ctx, p.client, nil, []string{"id"})
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package mongo
2+
3+
import (
4+
"context"
5+
6+
"github.com/reearth/reearthx/account/accountinfrastructure/accountmongo"
7+
"github.com/reearth/reearthx/account/accountusecase/accountrepo"
8+
"github.com/reearth/reearthx/mongox"
9+
)
10+
11+
// RoleWrapper wraps accountmongo.Role to prevent index dropping
12+
type RoleWrapper struct {
13+
*accountmongo.Role
14+
client *mongox.Collection
15+
}
16+
17+
func NewRoleWrapper(client *mongox.Client) accountrepo.Role {
18+
original := accountmongo.NewRole(client)
19+
20+
return &RoleWrapper{
21+
Role: original.(*accountmongo.Role),
22+
client: client.WithCollection("role"),
23+
}
24+
}
25+
26+
func (r *RoleWrapper) Init(ctx context.Context) error {
27+
return createIndexesOnly(ctx, r.client, nil, []string{"id"})
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package mongo
2+
3+
import (
4+
"context"
5+
6+
"github.com/reearth/reearthx/account/accountinfrastructure/accountmongo"
7+
"github.com/reearth/reearthx/account/accountusecase/accountrepo"
8+
"github.com/reearth/reearthx/mongox"
9+
)
10+
11+
// UserWrapper wraps accountmongo.User to prevent index dropping
12+
type UserWrapper struct {
13+
*accountmongo.User
14+
client *mongox.Collection
15+
}
16+
17+
func NewUserWrapper(client *mongox.Client) accountrepo.User {
18+
original := accountmongo.NewUser(client)
19+
20+
return &UserWrapper{
21+
User: original.(*accountmongo.User),
22+
client: client.WithCollection("user"),
23+
}
24+
}
25+
26+
func (u *UserWrapper) Init() error {
27+
ctx := context.Background()
28+
29+
return createIndexesOnly(ctx, u.client, nil, []string{"id"})
30+
}

0 commit comments

Comments
 (0)