Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(web, server): add migration for ga support #981

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package migration

import (
"context"
"fmt"

"github.com/reearth/reearth/server/internal/infrastructure/mongo/mongodoc"
"github.com/reearth/reearth/server/pkg/id"
"github.com/reearth/reearthx/log"
"github.com/reearth/reearthx/mongox"
"go.mongodb.org/mongo-driver/bson"
)

type seed struct {
projectId id.ProjectID
sceneId id.SceneID
}

func CopySceneId(ctx context.Context, c DBClient) error {
col := c.WithCollection("scene")

var seeds []seed

col.Find(ctx, bson.M{}, &mongox.BatchConsumer{

Check failure on line 24 in server/internal/infrastructure/mongo/migration/240515221450_copy_scene_id.go

View workflow job for this annotation

GitHub Actions / ci-server / ci-server-lint

Error return value of `col.Find` is not checked (errcheck)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of just logging errors, consider returning them from the CopySceneId function. This will allow the caller to handle errors appropriately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 634a084

Size: 1000,
Callback: func(rows []bson.Raw) error {
log.Infofc(ctx, "log: migration: CopySceneId: hit scenes: %d\n", len(rows))

for _, row := range rows {
log.Debugfc(ctx, "log: migration: CopySceneId: row: %s\n", row)
var doc mongodoc.SceneDocument
if err := bson.Unmarshal(row, &doc); err != nil {
return err
}

log.Debugfc(ctx, "log: migration: CopySceneId: doc: %s\n", doc)

pid, err := id.ProjectIDFrom(doc.Project)
if err != nil {
log.Errorfc(ctx, "log: migration: CopySceneId: project id error: %s\n", err)
}
sid, err := id.SceneIDFrom(doc.ID)
if err != nil {
log.Errorfc(ctx, "log: migration: CopySceneId: scene id error: %s\n", err)
}

seeds = append(seeds, seed{pid, sid})
}

return nil
},
})

col = c.WithCollection("project")
ids := make([]string, 0, len(seeds))
newRows := make([]interface{}, 0, len(seeds))

for _, s := range seeds {
err := col.Find(ctx, bson.M{"id": s.projectId.String()}, &mongox.BatchConsumer{
Size: 1000,
Callback: func(rows []bson.Raw) error {
if len(rows) == 0 {
log.Debugfc(ctx, "log: migration: CopySceneId: project not found: %s\n", s.projectId)
return nil
}

if len(rows) > 1 {
return fmt.Errorf("project found multiple: %s", s.projectId)
}

log.Debugfc(ctx, "log: migration: CopySceneId: project found: %s\n", s.projectId)

p := rows[0]
var doc mongodoc.ProjectDocument
if err := bson.Unmarshal(p, &doc); err != nil {
return err
}

if doc.Scene != "" {
log.Debugfc(ctx, "log: migration: CopySceneId: project already has scene: %s\n", doc.Scene)
return nil
}

doc.Scene = s.sceneId.String()
ids = append(ids, doc.ID)
newRows = append(newRows, doc)
return nil
},
})

if err != nil {
log.Errorfc(ctx, "log: migration: CopySceneId: project find error: %s\n", err)
continue
}
}

return col.SaveAll(ctx, ids, newRows)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package migration

import (
"context"
"testing"

"github.com/reearth/reearth/server/internal/infrastructure/mongo/mongodoc"
"github.com/reearth/reearth/server/pkg/id"
"github.com/reearth/reearthx/log"
"github.com/reearth/reearthx/mongox"
"github.com/reearth/reearthx/mongox/mongotest"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
)

func init() {
mongotest.Env = "REEARTH_DB"
}

func TestCopySceneId(t *testing.T) {
ctx := context.Background()
log.Infofc(ctx, "migration: CopySceneId test")

db := mongotest.Connect(t)(t)
c := mongox.NewClientWithDatabase(db)

// arrange
type seed struct {
projectId id.ProjectID
sceneId id.SceneID
}
seed1, seed2, seed3 := seed{
projectId: id.NewProjectID(),
sceneId: id.NewSceneID(),
}, seed{
projectId: id.NewProjectID(),
sceneId: id.NewSceneID(),
}, seed{
projectId: id.NewProjectID(),
sceneId: id.NewSceneID(),
}

_, _ = db.Collection("project").InsertMany(ctx, []any{
mongodoc.ProjectDocument{
ID: seed1.projectId.String(),
},
mongodoc.ProjectDocument{
ID: seed2.projectId.String(),
},
mongodoc.ProjectDocument{
ID: seed3.projectId.String(),
},
})

_, _ = db.Collection("scene").InsertMany(ctx, []any{
mongodoc.SceneDocument{
ID: seed1.sceneId.String(),
Project: seed1.projectId.String(),
},
mongodoc.SceneDocument{
ID: seed2.sceneId.String(),
Project: seed2.projectId.String(),
},
mongodoc.SceneDocument{
ID: seed3.sceneId.String(),
Project: seed3.projectId.String(),
},
})

// act
assert.NoError(t, CopySceneId(ctx, c))

// assert
var projects []mongodoc.ProjectDocument
assert.NoError(t, c.WithCollection("project").Find(ctx, bson.M{}, &mongox.BatchConsumer{
Size: 1000,
Callback: func(rows []bson.Raw) error {
for _, row := range rows {
var doc mongodoc.ProjectDocument
if err := bson.Unmarshal(row, &doc); err != nil {
return err
}
projects = append(projects, doc)
}
return nil
},
}))
assert.Len(t, projects, 3)
for _, s := range []seed{seed1, seed2, seed3} {
p, ok := lo.Find(projects, func(doc mongodoc.ProjectDocument) bool {
return s.projectId.String() == doc.ID
})
assert.True(t, ok)
assert.Equal(t, s.sceneId.String(), p.Scene)
}
}
15 changes: 8 additions & 7 deletions server/internal/infrastructure/mongo/migration/migrations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/internal/infrastructure/mongo/mongodoc/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type ProjectDocument struct {
CoreSupport bool
EnableGA bool
TrackingID string
// Scene string
Scene string
}

type ProjectConsumer = Consumer[*ProjectDocument, *project.Project]
Expand Down
Loading