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 committed Dec 1, 2021
1 parent 8f3ae7a commit 19b0ba1
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 @@ -18,6 +18,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 @@ -77,7 +79,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 @@ -74,7 +74,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 @@ -83,7 +83,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 @@ -123,7 +123,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 @@ -157,7 +157,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 @@ -175,7 +175,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 19b0ba1

Please sign in to comment.