From 4570a468232fe34a55e196c18b43639d11a412d3 Mon Sep 17 00:00:00 2001 From: fanjiankong Date: Sun, 2 Aug 2020 11:48:39 +0800 Subject: [PATCH] Fix bug of update preheat instance default. Signed-off-by: fanjiankong --- src/pkg/p2p/preheat/dao/instance/dao.go | 14 +++++--------- src/pkg/p2p/preheat/dao/instance/dao_test.go | 15 +++++++-------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/pkg/p2p/preheat/dao/instance/dao.go b/src/pkg/p2p/preheat/dao/instance/dao.go index 3754b0ae8..f30c87f64 100644 --- a/src/pkg/p2p/preheat/dao/instance/dao.go +++ b/src/pkg/p2p/preheat/dao/instance/dao.go @@ -2,6 +2,7 @@ package instance import ( "context" + "fmt" beego_orm "github.com/astaxie/beego/orm" "github.com/goharbor/harbor/src/lib/orm" @@ -95,15 +96,10 @@ func (d *dao) Update(ctx context.Context, instance *provider.Instance, props ... } // check default instances first - for _, prop := range props { - if prop == "default" && instance.Default { - - _, err = o.Raw("UPDATE ? SET default = false WHERE id != ?", instance.TableName(), instance.ID).Exec() - if err != nil { - return - } - - break + if instance.Default { + _, err = o.Raw(fmt.Sprintf("UPDATE %s SET is_default = false WHERE id != ?", instance.TableName()), instance.ID).Exec() + if err != nil { + return } } diff --git a/src/pkg/p2p/preheat/dao/instance/dao_test.go b/src/pkg/p2p/preheat/dao/instance/dao_test.go index 53a76bd80..8d36688c7 100644 --- a/src/pkg/p2p/preheat/dao/instance/dao_test.go +++ b/src/pkg/p2p/preheat/dao/instance/dao_test.go @@ -24,7 +24,7 @@ var ( AuthMode: "basic", AuthData: "{\"username\": \"admin\", \"password\": \"123456\"}", Status: "healthy", - Enabled: true, + Enabled: false, SetupTimestamp: 1582721396, } ) @@ -91,18 +91,17 @@ func (is *instanceSuite) TestUpdate() { assert.Nil(t, err) assert.NotNil(t, i) - i.Enabled = false - err = is.dao.Update(is.ctx, i, "enabled") - assert.Nil(t, err) - + // test set default i.Default = true - err = is.dao.Update(is.ctx, i, "default") - assert.NotNil(t, err) + i.Enabled = true + err = is.dao.Update(is.ctx, i) + assert.Nil(t, err) i, err = is.dao.Get(is.ctx, defaultInstance.ID) assert.Nil(t, err) assert.NotNil(t, i) - assert.False(t, i.Enabled) + assert.True(t, i.Default) + assert.True(t, i.Enabled) } func (is *instanceSuite) TestList() {