refactor(schedule): replace disabled field to active

This commit is contained in:
fiftin 2024-07-01 00:57:03 +05:00
parent 8fe600fd4f
commit f3f64f6a83
No known key found for this signature in database
GPG Key ID: 044381366A5D4731
8 changed files with 95 additions and 6 deletions

View File

@ -68,7 +68,7 @@ func GetMigrations() []Migration {
{Version: "2.9.70"},
{Version: "2.9.97"},
{Version: "2.9.100"},
{Version: "2.10.11"},
{Version: "2.10.12"},
}
}

View File

@ -6,7 +6,7 @@ type Schedule struct {
TemplateID int `db:"template_id" json:"template_id"`
CronFormat string `db:"cron_format" json:"cron_format"`
Name string `db:"name" json:"name"`
Disabled bool `db:"disabled" json:"disabled"`
Active bool `db:"active" json:"active"`
LastCommitHash *string `db:"last_commit_hash" json:"-"`
RepositoryID *int `db:"repository_id" json:"repository_id"`

View File

@ -41,6 +41,8 @@ func (d *BoltDb) ApplyMigration(m db.Migration) (err error) {
err = migration_2_8_40{migration{d.db}}.Apply()
case "2.8.91":
err = migration_2_8_91{migration{d.db}}.Apply()
case "2.10.12":
err = migration_2_10_12{migration{d.db}}.Apply()
}
if err != nil {

View File

@ -0,0 +1,30 @@
package bolt
type migration_2_10_12 struct {
migration
}
func (d migration_2_10_12) Apply() error {
projectIDs, err := d.getProjectIDs()
if err != nil {
return err
}
for _, projectID := range projectIDs {
schedules, err := d.getObjects(projectID, "schedule")
if err != nil {
return err
}
for scheduleID, schedule := range schedules {
schedule["active"] = true
err = d.setObject(projectID, "schedule", scheduleID, schedule)
if err != nil {
return err
}
}
}
return nil
}

View File

@ -0,0 +1,57 @@
package bolt
import (
"encoding/json"
"go.etcd.io/bbolt"
"testing"
)
func TestMigration_2_10_12_Apply(t *testing.T) {
store := CreateTestStore()
err := store.db.Update(func(tx *bbolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("project"))
if err != nil {
return err
}
err = b.Put([]byte("0000000001"), []byte("{}"))
if err != nil {
return err
}
r, err := tx.CreateBucketIfNotExists([]byte("project__schedule_0000000001"))
if err != nil {
return err
}
err = r.Put([]byte("0000000001"),
[]byte("{\"id\":\"1\",\"project_id\":\"1\"}"))
return err
})
if err != nil {
t.Fatal(err)
}
err = migration_2_10_12{migration{store.db}}.Apply()
if err != nil {
t.Fatal(err)
}
var scheduleData map[string]interface{}
err = store.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte("project__schedule_0000000001"))
str := string(b.Get([]byte("0000000001")))
return json.Unmarshal([]byte(str), &scheduleData)
})
if err != nil {
t.Fatal(err)
}
if !scheduleData["active"].(bool) {
t.Fatal("invalid role")
}
}

View File

@ -1,3 +1,3 @@
alter table `project__template` add `tasks` int not null default 0;
alter table `project__schedule` add `name` varchar(100);
alter table `project__schedule` add `disabled` boolean not null default false;
alter table `project__schedule` add `active` boolean not null default true;

View File

@ -4,7 +4,7 @@ import (
"database/sql"
"github.com/Masterminds/squirrel"
"github.com/ansible-semaphore/semaphore/db"
"math/rand/v2"
"math/rand"
)
func (d *SqlDb) CreateTaskStage(stage db.TaskStage) (db.TaskStage, error) {
@ -28,7 +28,7 @@ func (d *SqlDb) clearTasks(projectID int, templateID int, maxTasks int) {
nTasks := tpl.Tasks
if rand.IntN(10) == 0 { // randomly recalculate number of tasks for the template
if rand.Intn(10) == 0 { // randomly recalculate number of tasks for the template
var n int64
n, err = d.sql.SelectInt("SELECT count(*) FROM task WHERE template_id=?", templateID)
if err != nil {

View File

@ -111,7 +111,7 @@ func (p *SchedulePool) Refresh() {
p.locker.Lock()
p.clear()
for _, schedule := range schedules {
if schedule.Disabled {
if !schedule.Active {
continue
}