From 7ce4e0eb3259efa118abc4d7aad95e97cca2e259 Mon Sep 17 00:00:00 2001 From: Suhaha Date: Mon, 22 Nov 2021 15:43:49 +0800 Subject: [PATCH] fix(*): globally delete/update data by GORM (#1065) --- pkg/keyvisual/storage/model.go | 6 +++++- pkg/keyvisual/storage/model_test.go | 10 +++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/pkg/keyvisual/storage/model.go b/pkg/keyvisual/storage/model.go index 1b616f9e5e..b684b2e511 100644 --- a/pkg/keyvisual/storage/model.go +++ b/pkg/keyvisual/storage/model.go @@ -7,6 +7,8 @@ import ( "encoding/gob" "time" + "gorm.io/gorm" + "github.com/pingcap/tidb-dashboard/pkg/dbstore" "github.com/pingcap/tidb-dashboard/pkg/keyvisual/matrix" ) @@ -66,7 +68,9 @@ func CreateTableAxisModelIfNotExists(db *dbstore.DB) (bool, error) { } func ClearTableAxisModel(db *dbstore.DB) error { - return db.Delete(&AxisModel{}).Error + return db.Session(&gorm.Session{AllowGlobalUpdate: true}). + Delete(&AxisModel{}). + Error } func FindAxisModelsOrderByTime(db *dbstore.DB, layerNum uint8) ([]*AxisModel, error) { diff --git a/pkg/keyvisual/storage/model_test.go b/pkg/keyvisual/storage/model_test.go index 047104604c..3306c00132 100644 --- a/pkg/keyvisual/storage/model_test.go +++ b/pkg/keyvisual/storage/model_test.go @@ -63,7 +63,7 @@ func (t *testDbstoreSuite) TestClearTableAxisModel(c *C) { if err != nil { c.Fatalf("Count table AxisModel error: %v", err) } - c.Assert(count, Equals, 1) + c.Assert(count, Equals, int64(1)) err = ClearTableAxisModel(t.db) c.Assert(err, IsNil) @@ -72,7 +72,7 @@ func (t *testDbstoreSuite) TestClearTableAxisModel(c *C) { if err != nil { c.Fatalf("Count table AxisModel error: %v", err) } - c.Assert(count, Equals, 0) + c.Assert(count, Equals, int64(0)) } func (t *testDbstoreSuite) TestAxisModelFunc(c *C) { @@ -112,7 +112,7 @@ func (t *testDbstoreSuite) TestAxisModelFunc(c *C) { if err != nil { c.Fatalf("Count table AxisModel error: %v", err) } - c.Assert(count, Equals, 0) + c.Assert(count, Equals, int64(0)) err = axisModel.Delete(t.db) c.Assert(err, IsNil) @@ -146,7 +146,7 @@ func (t *testDbstoreSuite) TestAxisModelsFindAndDelete(c *C) { if err != nil { c.Fatalf("Count table AxisModel error: %v", err) } - c.Assert(count, Equals, int(maxLayerNum)*axisModelNumEachLayer) + c.Assert(count, Equals, int64(int(maxLayerNum)*axisModelNumEachLayer)) findLayerNum := maxLayerNum - 1 axisModels, err := FindAxisModelsOrderByTime(t.db, findLayerNum) @@ -164,7 +164,7 @@ func (t *testDbstoreSuite) TestAxisModelsFindAndDelete(c *C) { if err != nil { c.Fatalf("Count table AxisModel error: %v", err) } - c.Assert(count, Equals, int(maxLayerNum-1)*axisModelNumEachLayer) + c.Assert(count, Equals, int64(int(maxLayerNum-1)*axisModelNumEachLayer)) } func axisModelsDeepEqual(obtainedAxisModels []*AxisModel, expectedAxisModels []*AxisModel, c *C) {