Skip to content

Commit

Permalink
feat: add pipeline-crons soft_delete_at (#6287)
Browse files Browse the repository at this point in the history
  • Loading branch information
Malyue authored Feb 28, 2024
1 parent a89edd2 commit b8ba43a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `pipeline_crons` ADD COLUMN `soft_deleted_at` bigint(20) NOT NULL DEFAULT '0' COMMENT '软删除' AFTER `cluster_name`;
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2952,4 +2952,4 @@ sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
sourcegraph.com/sourcegraph/appdash v0.0.0-20180531100431-4c381bd170b4/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0 h1:ucqkfpjg9WzSUubAO62csmucvxl4/JeW3F4I4909XkM=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=
volcano.sh/volcano v0.4.0/go.mod h1:2sNJRhY/oNg0MYdBYORxozuDhvgZxoyeOvKJww/Tl8A=
volcano.sh/volcano v0.4.0/go.mod h1:2sNJRhY/oNg0MYdBYORxozuDhvgZxoyeOvKJww/Tl8A=
7 changes: 4 additions & 3 deletions internal/tools/pipeline/providers/cron/db/define.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ const (
)

type PipelineCron struct {
ID uint64 `json:"id" xorm:"pk autoincr"`
TimeCreated time.Time `json:"timeCreated" xorm:"created"` // 记录创建时间
TimeUpdated time.Time `json:"timeUpdated" xorm:"updated"` // 记录更新时间
ID uint64 `json:"id" xorm:"pk autoincr"`
TimeCreated time.Time `json:"timeCreated" xorm:"created"` // 记录创建时间
TimeUpdated time.Time `json:"timeUpdated" xorm:"updated"` // 记录更新时间
SoftDeletedAt uint64 `json:"softDeletedAt" xorm:"deleted"` // 记录删除时间(时间戳形式)

PipelineSource apistructs.PipelineSource `json:"pipelineSource"`
PipelineYmlName string `json:"pipelineYmlName"`
Expand Down
79 changes: 79 additions & 0 deletions internal/tools/pipeline/providers/cron/db/define_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package db

import (
"os"
"path/filepath"
"strconv"
"testing"
"time"
Expand All @@ -23,6 +25,7 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"

"github.com/erda-project/erda-infra/providers/mysqlxorm/sqlite3"
"github.com/erda-project/erda-proto-go/core/pipeline/pb"
"github.com/erda-project/erda/apistructs"
)
Expand Down Expand Up @@ -163,3 +166,79 @@ func TestPipelineCron_Convert2DTO(t *testing.T) {
})
}
}

func newSqlite3DB(dbSourceName string) *sqlite3.Sqlite3 {
sqlite3Db, err := sqlite3.NewSqlite3(dbSourceName + "?mode=rwc")
if err != nil {
panic(err)
}

// migrator db
err = sqlite3Db.DB().Sync2(PipelineCron{})
if err != nil {
panic(err)
}

return sqlite3Db
}

func TestDeleteCron(t *testing.T) {
dbname := filepath.Join(os.TempDir(), "test.db")
defer func() {
os.Remove(dbname)
}()

sqlite3Db := newSqlite3DB(dbname)

client := &Client{
Interface: sqlite3Db,
}

// create cron
crons := []*PipelineCron{
{ID: 1},
{ID: 2},
{ID: 3, SoftDeletedAt: 1234},
{ID: 4},
}

// create cron
for _, cron := range crons {
err := client.CreatePipelineCron(cron)
if err != nil {
t.Errorf("Create pipeline cron error : %v", err)
}
if cron.SoftDeletedAt != 0 {
err = client.DeletePipelineCron(cron.ID)
if err != nil {
t.Errorf("Delete pipeline cron error : %v", err)
}
}
}

_, found, err := client.GetPipelineCron(3)
if err != nil {
t.Errorf("Get pipeline cron error : %v", err)
}

assert.Equal(t, false, found)

// delete all cron
deleteIds := []uint64{4, 1, 2}
for _, id := range deleteIds {
err = client.DeletePipelineCron(id)
if err != nil {
t.Errorf("Delete pipeline cron error : %v", err)
}
}

// check if delete
for _, id := range deleteIds {
_, found, err = client.GetPipelineCron(id)
if err != nil {
t.Errorf("Get pipeline cron error : %v", err)
}
assert.Equal(t, false, found)
}

}

0 comments on commit b8ba43a

Please sign in to comment.