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

fix(server): plugin update cause layer corruption #431

Merged
merged 15 commits into from
Jun 1, 2023
Merged
52 changes: 40 additions & 12 deletions server/internal/usecase/interactor/scene_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,24 @@ func TestScene_UpgradePlugin(t *testing.T) {
pl2ps := property.NewSchema().ID(id.NewPropertySchemaID(pid2, "@")).MustBuild()
psr := memory.NewPropertySchemaWith(pl1ps, pl2ps)

pl1 := plugin.New().ID(pid1).Schema(pl1ps.ID().Ref()).MustBuild()
pl2 := plugin.New().ID(pid2).Schema(pl2ps.ID().Ref()).MustBuild()
pl1 := plugin.New().ID(pid1).Schema(pl1ps.ID().Ref()).Extensions([]*plugin.Extension{
plugin.NewExtension().ID("a").Type(plugin.ExtensionTypeBlock).Schema(pl1ps.ID()).MustBuild(),
}).MustBuild()
pl2 := plugin.New().ID(pid2).Schema(pl2ps.ID().Ref()).Extensions([]*plugin.Extension{
plugin.NewExtension().ID("a").Type(plugin.ExtensionTypeBlock).Schema(pl2ps.ID()).MustBuild(),
}).MustBuild()
pr := memory.NewPluginWith(pl1, pl2)

pl1p := property.New().NewID().Scene(sid).Schema(*pl1.Schema()).MustBuild()
prr := memory.NewPropertyWith(pl1p)

lr := memory.NewLayerWith()
pl2p := property.New().NewID().Scene(sid).Schema(*pl1.Schema()).MustBuild()
prr := memory.NewPropertyWith(pl1p, pl2p)

ibf1 := layer.NewInfoboxField().NewID().Plugin(plugin.OfficialPluginID).Extension("textblock").Property(id.NewPropertyID()).MustBuild()
ibf2 := layer.NewInfoboxField().NewID().Plugin(pid1).Extension("a").Property(pl2p.ID()).MustBuild()
ib := layer.NewInfobox([]*layer.InfoboxField{ibf1, ibf2}, id.NewPropertyID())
l1 := layer.New().NewID().Plugin(plugin.OfficialPluginID.Ref()).Scene(sid).Infobox(ib).Item().MustBuild()
l2 := layer.New().NewID().Plugin(plugin.OfficialPluginID.Ref()).Scene(sid).Group().Layers(layer.NewIDList([]layer.ID{l1.ID()})).MustBuild()
lr := memory.NewLayerWith(l1, l2)

dsr := memory.NewDataset()

Expand Down Expand Up @@ -384,14 +394,32 @@ func TestScene_UpgradePlugin(t *testing.T) {
if tt.wantErr != nil {
assert.Equal(tt.wantErr, err)
assert.Nil(gotSc)
} else {
assert.NoError(err)
assert.Same(sc, gotSc)
assert.False(gotSc.Plugins().Has(tt.args.old))
assert.True(gotSc.Plugins().Has(tt.args.new))
p, _ := prr.FindByID(ctx, *gotSc.Plugins().Plugin(tt.args.new).Property())
assert.Equal(*pl2.Schema(), p.Schema())
return
}

assert.NoError(err)
assert.Same(sc, gotSc)
assert.False(gotSc.Plugins().Has(tt.args.old))
assert.True(gotSc.Plugins().Has(tt.args.new))
p, _ := prr.FindByID(ctx, *gotSc.Plugins().Plugin(tt.args.new).Property())
assert.Equal(*pl2.Schema(), p.Schema())

// layers plugin id should not be changed
ls, err := lr.FindByScene(ctx, sid)
assert.NoError(err)
for _, l := range ls {
assert.Equal(plugin.OfficialPluginID.Ref(), (*l).Plugin())
}

// layer > infobox > field
ll1 := *ls.Find(l1.ID())
assert.NotNil(ll1.Infobox().Field(ibf1.ID()))
assert.Equal(id.OfficialPluginID, ll1.Infobox().Field(ibf1.ID()).Plugin())
assert.NotNil(ll1.Infobox().Field(ibf2.ID()))
// assert.Equal(tt.args.new, ll1.Infobox().Field(ibf2.ID()).Plugin())
prop, err := prr.FindByID(ctx, ll1.Infobox().Field(ibf2.ID()).Property())
assert.NoError(err)
assert.Equal(tt.args.new, prop.Schema().Plugin())
})
}
}
Expand Down
8 changes: 5 additions & 3 deletions server/pkg/layer/id_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,23 @@ func (l *IDList) MoveLayerAt(fromIndex int, toIndex int) {
l.layers = append(newSlice, l.layers[toIndex:]...)
}

func (l *IDList) RemoveLayer(ids ...ID) {
func (l *IDList) RemoveLayer(ids ...ID) int {
if l == nil {
return
return 0
}

removed := 0
for i := 0; i < len(l.layers); i++ {
layer := l.layers[i]
for _, id := range ids {
if layer == id {
l.RemoveLayerAt(i)
removed++
i--
break
}
}
}
return removed
}

func (l *IDList) RemoveLayerAt(index int) {
Expand Down
6 changes: 4 additions & 2 deletions server/pkg/layer/id_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ func TestLayerIDList(t *testing.T) {

// 1, 3, 4

layers.RemoveLayer(l2)
c := layers.RemoveLayer(l2)
assert.Equal(t, 3, layers.LayerCount())
assert.Equal(t, l1, layers.LayerAt(0))
assert.Equal(t, l3, layers.LayerAt(1))
assert.Equal(t, l4, layers.LayerAt(2))
assert.False(t, layers.HasLayer(l2))
assert.Equal(t, 1, c)

// 1, 3, 4, 2

Expand All @@ -131,8 +132,9 @@ func TestLayerIDList(t *testing.T) {

// 1, 3

layers.RemoveLayer(l2, l4)
c = layers.RemoveLayer(l2, l4)
assert.Equal(t, 2, layers.LayerCount())
assert.Equal(t, l1, layers.LayerAt(0))
assert.Equal(t, l3, layers.LayerAt(1))
assert.Equal(t, 2, c)
}
4 changes: 1 addition & 3 deletions server/pkg/layer/infobox_field_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ func NewInfoboxField() *InfoboxFieldBuilder {
}

func (b *InfoboxFieldBuilder) Build() (*InfoboxField, error) {
if b.i.id.IsNil() ||
string(b.i.extension) == "" ||
b.i.property.IsNil() {
if b.i.id.IsNil() || string(b.i.extension) == "" || b.i.property.IsNil() {
yk-eukarya marked this conversation as resolved.
Show resolved Hide resolved
return nil, ErrInvalidID
}
return b.i, nil
Expand Down
20 changes: 9 additions & 11 deletions server/pkg/scene/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,23 @@ func (p *Plugins) Remove(pid PluginID) {
}

func (p *Plugins) Upgrade(from, to PluginID, pr *PropertyID, deleteProperty bool) {
if p == nil || from.IsNil() || to.IsNil() {
if p == nil || from.IsNil() || to.IsNil() || from.Equal(to) || from.Equal(OfficialPluginID) {
return
}

for i, p2 := range p.plugins {
if p2.plugin.Equal(OfficialPluginID) {
if !p2.plugin.Equal(from) {
continue
}
if p2.plugin.Equal(from) {
var newpr *PropertyID
if !deleteProperty {
newpr = pr.CloneRef()
if newpr == nil {
newpr = p2.property.CloneRef()
}
var newpr *PropertyID
if !deleteProperty {
newpr = pr.CloneRef()
if newpr == nil {
newpr = p2.property.CloneRef()
}
p.plugins[i] = &Plugin{plugin: to, property: newpr}
return
}
p.plugins[i] = &Plugin{plugin: to, property: newpr}
return
}
}

Expand Down
9 changes: 9 additions & 0 deletions server/pkg/scene/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,15 @@ func TestPlugins_Upgrade(t *testing.T) {
target: NewPlugins([]*Plugin{NewPlugin(OfficialPluginID, pr)}),
want: NewPlugins([]*Plugin{NewPlugin(OfficialPluginID, pr)}),
},
{
name: "same plugin",
args: args{
From: pid,
To: pid,
},
target: NewPlugins([]*Plugin{NewPlugin(OfficialPluginID, pr)}),
want: NewPlugins([]*Plugin{NewPlugin(OfficialPluginID, pr)}),
},
{
name: "nil",
args: args{
Expand Down
Loading