Skip to content

Commit

Permalink
feat(server): add Cesium Ion and GSI terrain, transfer terrain proper…
Browse files Browse the repository at this point in the history
…ties (#369)
  • Loading branch information
rot1024 committed Oct 28, 2022
1 parent 387f1cc commit 104e590
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 199 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package migration

import (
"context"

"github.com/labstack/gommon/log"
"github.com/reearth/reearth/server/internal/infrastructure/mongo/mongodoc"
"github.com/reearth/reearth/server/pkg/property"
"github.com/reearth/reearthx/mongox"
"github.com/samber/lo"
"go.mongodb.org/mongo-driver/bson"
)

func MoveTerrainProperties(ctx context.Context, c DBClient) error {
col := c.WithCollection("property")
fields := []property.FieldID{
"terrain",
"terrainType",
"terrainExaggeration",
"terrainExaggerationRelativeHeight",
"depthTestAgainstTerrain",
}

diff := property.SchemaDiff{
Moved: lo.Map(fields, func(f property.FieldID, _ int) property.SchemaDiffMoved {
return property.SchemaDiffMoved{
From: property.SchemaFieldPointer{
SchemaGroup: "default",
Field: f,
},
To: property.SchemaFieldPointer{
SchemaGroup: "terrain",
Field: f,
},
}
}),
}

return col.Find(ctx, bson.M{
"$or": []bson.M{
{
"schema": "reearth/cesium",
},
{
"schemaplugin": "reearth",
"schemaname": "cesium",
},
},
}, &mongox.BatchConsumer{
Size: 1000,
Callback: func(rows []bson.Raw) error {
ids := make([]string, 0, len(rows))
newRows := make([]interface{}, 0, len(rows))

log.Infof("migration: MoveTerrainProperties: hit properties: %d\n", len(rows))

for _, row := range rows {
var doc mongodoc.PropertyDocument
if err := bson.Unmarshal(row, &doc); err != nil {
return err
}

p, err := doc.Model()
if err != nil {
return err
}

if diff.Migrate(p) {
ids = append(ids, doc.ID)
doc2, _ := mongodoc.NewProperty(p)
newRows = append(newRows, doc2)
}
}

return col.SaveAll(ctx, ids, newRows)
},
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
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/mongox"
"github.com/reearth/reearthx/mongox/mongotest"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
)

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

func TestMoveTerrainProperties(t *testing.T) {
db := mongotest.Connect(t)(t)
cl := mongox.NewClientWithDatabase(db)
ctx := context.Background()

sid := id.NewSceneID()
iid := id.NewPropertyItemID()
pid1, pid2, pid3 := id.NewPropertyID(), id.NewPropertyID(), id.NewPropertyID()

// insert seeds
_, _ = db.Collection("property").InsertMany(ctx, []any{
mongodoc.PropertyDocument{
ID: pid1.String(),
Scene: sid.String(),
SchemaPlugin: "reearth",
SchemaName: "cesium",
Items: []*mongodoc.PropertyItemDocument{
{
ID: iid.String(),
Type: "group",
SchemaGroup: "default",
Fields: []*mongodoc.PropertyFieldDocument{
{Field: "terrain", Type: "bool", Value: true},
{Field: "terrainType", Type: "string", Value: "cesium"},
{Field: "terrainExaggeration", Type: "number", Value: 2.0},
{Field: "terrainExaggerationRelativeHeight", Type: "number", Value: 1.0},
{Field: "depthTestAgainstTerrain", Type: "bool", Value: true},
},
},
},
},
mongodoc.PropertyDocument{
ID: pid2.String(),
Scene: sid.String(),
Schema: "reearth/cesium",
SchemaPlugin: "reearth",
SchemaName: "cesium",
Items: []*mongodoc.PropertyItemDocument{
{
ID: iid.String(),
Type: "group",
SchemaGroup: "default",
Fields: []*mongodoc.PropertyFieldDocument{
{Field: "aaa", Type: "bool", Value: true},
},
},
},
},
mongodoc.PropertyDocument{
ID: pid3.String(),
Scene: sid.String(),
SchemaPlugin: "reearth",
SchemaName: "cesium",
Items: []*mongodoc.PropertyItemDocument{
{
ID: iid.String(),
Type: "group",
SchemaGroup: "default",
Fields: []*mongodoc.PropertyFieldDocument{
{Field: "aaa", Type: "bool", Value: true},
{Field: "terrain", Type: "bool", Value: true},
},
},
},
},
})

// migrate
assert.NoError(t, MoveTerrainProperties(ctx, cl))

// assert migrated docs
cur, err := db.Collection("property").Find(ctx, bson.M{})
assert.NoError(t, err)
var res []mongodoc.PropertyDocument
assert.NoError(t, cur.All(ctx, &res))

assert.Equal(t, []mongodoc.PropertyDocument{
{
ID: pid1.String(),
Scene: sid.String(),
SchemaPlugin: "reearth",
SchemaName: "cesium",
Items: []*mongodoc.PropertyItemDocument{
{
ID: iid.String(),
Type: "group",
SchemaGroup: "default",
Fields: []*mongodoc.PropertyFieldDocument{},
},
{
ID: res[0].Items[1].ID,
Type: "group",
SchemaGroup: "terrain",
Fields: []*mongodoc.PropertyFieldDocument{
{Field: "terrain", Type: "bool", Value: true},
{Field: "terrainType", Type: "string", Value: "cesium"},
{Field: "terrainExaggeration", Type: "number", Value: 2.0},
{Field: "terrainExaggerationRelativeHeight", Type: "number", Value: 1.0},
{Field: "depthTestAgainstTerrain", Type: "bool", Value: true},
},
},
},
},
{
ID: pid2.String(),
Scene: sid.String(),
Schema: "reearth/cesium",
SchemaPlugin: "reearth",
SchemaName: "cesium",
Items: []*mongodoc.PropertyItemDocument{
{
ID: iid.String(),
Type: "group",
SchemaGroup: "default",
Fields: []*mongodoc.PropertyFieldDocument{
{Field: "aaa", Type: "bool", Value: true},
},
},
},
},
{
ID: pid3.String(),
Scene: sid.String(),
SchemaPlugin: "reearth",
SchemaName: "cesium",
Items: []*mongodoc.PropertyItemDocument{
{
ID: iid.String(),
Type: "group",
SchemaGroup: "default",
Fields: []*mongodoc.PropertyFieldDocument{
{Field: "aaa", Type: "bool", Value: true},
},
},
{
ID: res[2].Items[1].ID,
Type: "group",
SchemaGroup: "terrain",
Fields: []*mongodoc.PropertyFieldDocument{
{Field: "terrain", Type: "bool", Value: true},
},
},
},
},
}, res)
}

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

Loading

0 comments on commit 104e590

Please sign in to comment.