Skip to content

Commit

Permalink
fix(*): globally delete/update data by GORM (#1065)
Browse files Browse the repository at this point in the history
  • Loading branch information
shhdgit authored Nov 22, 2021
1 parent be19f60 commit 7ce4e0e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
6 changes: 5 additions & 1 deletion pkg/keyvisual/storage/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/keyvisual/storage/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit 7ce4e0e

Please sign in to comment.