-
Notifications
You must be signed in to change notification settings - Fork 44
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
kawasaki124529
wants to merge
2
commits into
main
Choose a base branch
from
chore/migration-for-ga
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
server/internal/infrastructure/mongo/migration/240515221450_copy_scene_id.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{ | ||
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) | ||
} |
97 changes: 97 additions & 0 deletions
97
server/internal/infrastructure/mongo/migration/240515221450_copy_scene_id_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
server/internal/infrastructure/mongo/migration/migrations.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 634a084