Skip to content
This repository was archived by the owner on Aug 16, 2022. It is now read-only.

Commit 861c4bb

Browse files
committed
fix: scenes and properties are not updated properly when plugin is updated
1 parent 5009c5e commit 861c4bb

File tree

12 files changed

+105
-84
lines changed

12 files changed

+105
-84
lines changed

internal/usecase/interactor/plugin_upload.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,20 @@ func (i *Plugin) upload(ctx context.Context, p *pluginpack.Package, sid id.Scene
9090
}
9191
}()
9292

93+
var oldPManifest *manifest.Manifest
9394
newpid := p.Manifest.Plugin.ID()
9495
oldpid := s.Plugins().PluginByName(newpid.Name()).PluginRef()
95-
var oldp *plugin.Plugin
9696
if oldpid != nil {
97-
oldp, err = i.pluginRepo.FindByID(ctx, *oldpid, []id.SceneID{sid})
97+
oldPlugin, err := i.pluginRepo.FindByID(ctx, *oldpid, []id.SceneID{sid})
9898
if err != nil {
9999
return nil, nil, err
100100
}
101+
102+
oldPManifest2, err := i.pluginManifestFromPlugin(ctx, oldPlugin)
103+
if err != nil {
104+
return nil, nil, err
105+
}
106+
oldPManifest = &oldPManifest2
101107
}
102108

103109
// new (oldpid == nil): upload files, save plugin and properties -> install
@@ -136,19 +142,19 @@ func (i *Plugin) upload(ctx context.Context, p *pluginpack.Package, sid id.Scene
136142
return nil, nil, err
137143
}
138144

139-
if oldpid == nil {
145+
if oldPManifest == nil {
140146
// new: install plugin
141147
if err := i.installScenePlugin(ctx, p, s); err != nil {
142148
return nil, nil, err
143149
}
144150
} else {
145151
// same, diff: migrate
146-
if err := i.migrateScenePlugin(ctx, p, s, oldp); err != nil {
152+
if err := i.migrateScenePlugin(ctx, *oldPManifest, p, s); err != nil {
147153
return nil, nil, err
148154
}
149155
}
150156

151-
if oldpid != nil && !oldpid.Equal(newpid) {
157+
if oldpid != nil && oldPManifest != nil && !oldpid.Equal(newpid) {
152158
// diff only: delete old files
153159
if err := i.file.RemovePlugin(ctx, *oldpid); err != nil {
154160
return nil, nil, err
@@ -159,7 +165,7 @@ func (i *Plugin) upload(ctx context.Context, p *pluginpack.Package, sid id.Scene
159165
if err := i.pluginRepo.Remove(ctx, *oldpid); err != nil {
160166
return nil, nil, err
161167
}
162-
if ps := oldp.PropertySchemas(); len(ps) > 0 {
168+
if ps := oldPManifest.Plugin.PropertySchemas(); len(ps) > 0 {
163169
if err := i.propertySchemaRepo.RemoveAll(ctx, ps); err != nil {
164170
return nil, nil, err
165171
}
@@ -195,17 +201,12 @@ func (i *Plugin) installScenePlugin(ctx context.Context, p *pluginpack.Package,
195201
return nil
196202
}
197203

198-
func (i *Plugin) migrateScenePlugin(ctx context.Context, p *pluginpack.Package, s *scene.Scene, oldp *plugin.Plugin) (err error) {
199-
if oldp == nil || p.Manifest == nil {
204+
func (i *Plugin) migrateScenePlugin(ctx context.Context, oldm manifest.Manifest, p *pluginpack.Package, s *scene.Scene) (err error) {
205+
if oldm.Plugin == nil || p.Manifest == nil {
200206
return nil
201207
}
202208

203-
oldPManifest, err := i.pluginManifestFromPlugin(ctx, oldp)
204-
if err != nil {
205-
return err
206-
}
207-
208-
diff := manifest.DiffFrom(oldPManifest, *p.Manifest)
209+
diff := manifest.DiffFrom(oldm, *p.Manifest)
209210
updatedProperties := property.List{}
210211

211212
// update scene
@@ -220,18 +221,14 @@ func (i *Plugin) migrateScenePlugin(ctx context.Context, p *pluginpack.Package,
220221
updatedProperties = append(updatedProperties, p)
221222
}
222223

223-
sp := s.Plugins().Plugin(diff.From)
224-
if sp != nil && sp.Property() != nil && diff.PropertySchemaDeleted {
224+
if sp := s.Plugins().Plugin(diff.From); sp != nil && sp.Property() != nil && diff.PropertySchemaDeleted {
225225
// plugin property should be removed
226226
if err := i.propertyRepo.Remove(ctx, *sp.Property()); err != nil {
227227
return err
228228
}
229229
}
230230

231231
s.Plugins().Upgrade(diff.From, diff.To, spp, diff.PropertySchemaDeleted)
232-
if err := i.sceneRepo.Save(ctx, s); err != nil {
233-
return err
234-
}
235232

236233
// delete layers, blocks and widgets
237234
for _, e := range diff.DeletedExtensions {
@@ -255,6 +252,10 @@ func (i *Plugin) migrateScenePlugin(ctx context.Context, p *pluginpack.Package,
255252
}
256253
}
257254

255+
if err := i.sceneRepo.Save(ctx, s); err != nil {
256+
return err
257+
}
258+
258259
// migrate layers
259260
if err := i.layerRepo.UpdatePlugin(ctx, diff.From, diff.To, []id.SceneID{s.ID()}); err != nil {
260261
return err

internal/usecase/interactor/plugin_upload_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ func TestPlugin_Upload_SameVersion(t *testing.T) {
137137
sid := id.NewSceneID()
138138
pid := mockPluginID.WithScene(sid.Ref())
139139
eid := id.PluginExtensionID("marker")
140+
eid2 := id.PluginExtensionID("widget")
141+
wid := id.NewWidgetID()
140142

141143
repos := memory.InitRepos(nil)
142144
mfs := mockFS(map[string]string{
@@ -146,16 +148,21 @@ func TestPlugin_Upload_SameVersion(t *testing.T) {
146148
assert.NoError(t, err)
147149

148150
ps := property.NewSchema().ID(property.NewSchemaID(pid, eid.String())).MustBuild()
151+
ps2 := property.NewSchema().ID(property.NewSchemaID(pid, eid2.String())).MustBuild()
149152
pl := plugin.New().ID(pid).Extensions([]*plugin.Extension{
150153
plugin.NewExtension().ID(eid).Type(plugin.ExtensionTypePrimitive).Schema(ps.ID()).MustBuild(),
154+
plugin.NewExtension().ID(eid2).Type(plugin.ExtensionTypeWidget).Schema(ps2.ID()).MustBuild(),
151155
}).MustBuild()
152156

153157
p := property.New().NewID().Schema(ps.ID()).Scene(sid).MustBuild()
158+
p2 := property.New().NewID().Schema(ps2.ID()).Scene(sid).MustBuild()
154159
pluginLayer := layer.NewItem().NewID().Scene(sid).Plugin(pid.Ref()).Extension(eid.Ref()).Property(p.IDRef()).MustBuild()
155160
rootLayer := layer.NewGroup().NewID().Scene(sid).Layers(layer.NewIDList([]layer.ID{pluginLayer.ID()})).Root(true).MustBuild()
156161
scene := scene.New().ID(sid).Team(team).RootLayer(rootLayer.ID()).Plugins(scene.NewPlugins([]*scene.Plugin{
157162
scene.NewPlugin(pid, nil),
158-
})).MustBuild()
163+
})).Widgets(scene.NewWidgets([]*scene.Widget{
164+
scene.MustNewWidget(wid, pid, eid2, p2.ID(), false, false),
165+
}, nil)).MustBuild()
159166

160167
_ = repos.PropertySchema.Save(ctx, ps)
161168
_ = repos.Plugin.Save(ctx, pl)
@@ -188,6 +195,11 @@ func TestPlugin_Upload_SameVersion(t *testing.T) {
188195
nscene, err := repos.Scene.FindByID(ctx, scene.ID(), nil)
189196
assert.NoError(t, err)
190197
assert.True(t, nscene.Plugins().HasPlugin(pl.ID()))
198+
assert.Nil(t, nscene.Widgets().Widget(wid))
199+
200+
nlp2, err := repos.Property.FindByID(ctx, p.ID(), nil)
201+
assert.Nil(t, nlp2) // deleted
202+
assert.Equal(t, rerror.ErrNotFound, err)
191203

192204
// plugin
193205
npl, err := repos.Plugin.FindByID(ctx, pid, []id.SceneID{scene.ID()})
@@ -198,6 +210,10 @@ func TestPlugin_Upload_SameVersion(t *testing.T) {
198210
assert.Nil(t, nlps) // deleted
199211
assert.Equal(t, rerror.ErrNotFound, err)
200212

213+
nlps2, err := repos.PropertySchema.FindByID(ctx, ps2.ID())
214+
assert.Nil(t, nlps2) // deleted
215+
assert.Equal(t, rerror.ErrNotFound, err)
216+
201217
_, err = mfs.Open("plugins/" + pid.String() + "/hogehoge")
202218
assert.True(t, os.IsNotExist(err)) // deleted
203219

pkg/scene/widgets_test.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,30 +155,31 @@ func TestWidgets_RemoveAllByPlugin(t *testing.T) {
155155
w3 := MustNewWidget(NewWidgetID(), pid2, "e1", NewPropertyID(), true, false)
156156

157157
tests := []struct {
158-
Name string
159-
PID PluginID
160-
EID *PluginExtensionID
161-
WS, Expected *Widgets
162-
ExpectedResult []PropertyID
158+
Name string
159+
ArgsPID PluginID
160+
ArgsEID *PluginExtensionID
161+
Target, Expected *Widgets
162+
ExpectedResult []PropertyID
163163
}{
164164
{
165165
Name: "remove widgets",
166-
PID: pid,
167-
WS: NewWidgets([]*Widget{w1, w2, w3}, nil),
166+
ArgsPID: pid,
167+
ArgsEID: nil,
168+
Target: NewWidgets([]*Widget{w1, w2, w3}, nil),
168169
Expected: NewWidgets([]*Widget{w3}, nil),
169170
ExpectedResult: []PropertyID{w1.Property(), w2.Property()},
170171
},
171172
{
172-
Name: "remove widgets",
173-
PID: pid,
174-
EID: PluginExtensionID("e2").Ref(),
175-
WS: NewWidgets([]*Widget{w1, w2, w3}, nil),
173+
Name: "remove widgets of extension",
174+
ArgsPID: pid,
175+
ArgsEID: PluginExtensionID("e2").Ref(),
176+
Target: NewWidgets([]*Widget{w1, w2, w3}, nil),
176177
Expected: NewWidgets([]*Widget{w1, w3}, nil),
177178
ExpectedResult: []PropertyID{w2.Property()},
178179
},
179180
{
180181
Name: "remove from nil widgets",
181-
WS: nil,
182+
Target: nil,
182183
Expected: nil,
183184
ExpectedResult: nil,
184185
},
@@ -188,8 +189,8 @@ func TestWidgets_RemoveAllByPlugin(t *testing.T) {
188189
tc := tc
189190
t.Run(tc.Name, func(t *testing.T) {
190191
t.Parallel()
191-
assert.Equal(t, tc.ExpectedResult, tc.WS.RemoveAllByPlugin(tc.PID, tc.EID))
192-
assert.Equal(t, tc.Expected, tc.WS)
192+
assert.Equal(t, tc.ExpectedResult, tc.Target.RemoveAllByPlugin(tc.ArgsPID, tc.ArgsEID))
193+
assert.Equal(t, tc.Expected, tc.Target)
193194
})
194195
}
195196
}

pkg/value/bool.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var TypeBool Type = "bool"
66

77
type propertyBool struct{}
88

9-
func (*propertyBool) I2V(i interface{}) (interface{}, bool) {
9+
func (p *propertyBool) I2V(i interface{}) (interface{}, bool) {
1010
switch v := i.(type) {
1111
case bool:
1212
return v, true
@@ -16,13 +16,11 @@ func (*propertyBool) I2V(i interface{}) (interface{}, bool) {
1616
}
1717
case *bool:
1818
if v != nil {
19-
return *v, true
19+
return p.I2V(*v)
2020
}
2121
case *string:
2222
if v != nil {
23-
if b, err := strconv.ParseBool(*v); err == nil {
24-
return b, true
25-
}
23+
return p.I2V(*v)
2624
}
2725
}
2826
return nil, false

pkg/value/coordinates.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ var TypeCoordinates Type = "coordinates"
3232

3333
type propertyCoordinates struct{}
3434

35-
func (*propertyCoordinates) I2V(i interface{}) (interface{}, bool) {
35+
func (p *propertyCoordinates) I2V(i interface{}) (interface{}, bool) {
3636
if v, ok := i.(Coordinates); ok {
3737
return v, true
3838
} else if v, ok := i.(*Coordinates); ok {
3939
if v != nil {
40-
return *v, true
40+
return p.I2V(*v)
4141
}
4242
return nil, false
4343
} else if v2, ok := i.([]float64); ok {

pkg/value/latlng.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ var TypeLatLng Type = "latlng"
2121

2222
type propertyLatLng struct{}
2323

24-
func (*propertyLatLng) I2V(i interface{}) (interface{}, bool) {
24+
func (p *propertyLatLng) I2V(i interface{}) (interface{}, bool) {
2525
switch v := i.(type) {
2626
case LatLng:
2727
return v, true
2828
case LatLngHeight:
2929
return LatLng{Lat: v.Lat, Lng: v.Lng}, true
3030
case *LatLng:
3131
if v != nil {
32-
return *v, true
32+
return p.I2V(*v)
3333
}
3434
case *LatLngHeight:
3535
if v != nil {
36-
return LatLng{Lat: v.Lat, Lng: v.Lng}, true
36+
return p.I2V(*v)
3737
}
3838
}
3939

pkg/value/latlngheight.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ var TypeLatLngHeight Type = "latlngheight"
2323

2424
type propertyLatLngHeight struct{}
2525

26-
func (*propertyLatLngHeight) I2V(i interface{}) (interface{}, bool) {
26+
func (p *propertyLatLngHeight) I2V(i interface{}) (interface{}, bool) {
2727
switch v := i.(type) {
2828
case LatLngHeight:
2929
return v, true
3030
case LatLng:
3131
return LatLngHeight{Lat: v.Lat, Lng: v.Lng, Height: 0}, true
3232
case *LatLngHeight:
3333
if v != nil {
34-
return *v, true
34+
return p.I2V(*v)
3535
}
3636
case *LatLng:
3737
if v != nil {
38-
return LatLngHeight{Lat: v.Lat, Lng: v.Lng, Height: 0}, true
38+
return p.I2V(*v)
3939
}
4040
}
4141

0 commit comments

Comments
 (0)