diff --git a/db/Migration.go b/db/Migration.go index 0f94b981..09097b9b 100644 --- a/db/Migration.go +++ b/db/Migration.go @@ -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"}, } } diff --git a/db/Schedule.go b/db/Schedule.go index 27f499f2..f18f78dc 100644 --- a/db/Schedule.go +++ b/db/Schedule.go @@ -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"` diff --git a/db/bolt/migration.go b/db/bolt/migration.go index 9c2918e4..b07a6e3b 100644 --- a/db/bolt/migration.go +++ b/db/bolt/migration.go @@ -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 { diff --git a/db/bolt/migration_2_10_12.go b/db/bolt/migration_2_10_12.go new file mode 100644 index 00000000..49ebf8fa --- /dev/null +++ b/db/bolt/migration_2_10_12.go @@ -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 +} diff --git a/db/bolt/migration_2_10_12_test.go b/db/bolt/migration_2_10_12_test.go new file mode 100644 index 00000000..c6988044 --- /dev/null +++ b/db/bolt/migration_2_10_12_test.go @@ -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") + } +} diff --git a/db/sql/migrations/v2.10.11.sql b/db/sql/migrations/v2.10.12.sql similarity index 61% rename from db/sql/migrations/v2.10.11.sql rename to db/sql/migrations/v2.10.12.sql index e2568e92..1ac12b7e 100644 --- a/db/sql/migrations/v2.10.11.sql +++ b/db/sql/migrations/v2.10.12.sql @@ -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; \ No newline at end of file +alter table `project__schedule` add `active` boolean not null default true; \ No newline at end of file diff --git a/db/sql/task.go b/db/sql/task.go index cebc74c1..992c592a 100644 --- a/db/sql/task.go +++ b/db/sql/task.go @@ -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 { diff --git a/services/schedules/SchedulePool.go b/services/schedules/SchedulePool.go index dcdefe27..c8419f17 100644 --- a/services/schedules/SchedulePool.go +++ b/services/schedules/SchedulePool.go @@ -111,7 +111,7 @@ func (p *SchedulePool) Refresh() { p.locker.Lock() p.clear() for _, schedule := range schedules { - if schedule.Disabled { + if !schedule.Active { continue }