Skip to content

Commit

Permalink
refactor(schedule): replace disabled field to active
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftin committed Jun 30, 2024
1 parent 8fe600f commit f3f64f6
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 6 deletions.
2 changes: 1 addition & 1 deletion db/Migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
}
}

Expand Down
2 changes: 1 addition & 1 deletion db/Schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 2 additions & 0 deletions db/bolt/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions db/bolt/migration_2_10_12.go
Original file line number Diff line number Diff line change
@@ -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
}
57 changes: 57 additions & 0 deletions db/bolt/migration_2_10_12_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions db/sql/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion services/schedules/SchedulePool.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (p *SchedulePool) Refresh() {
p.locker.Lock()
p.clear()
for _, schedule := range schedules {
if schedule.Disabled {
if !schedule.Active {
continue
}

Expand Down

0 comments on commit f3f64f6

Please sign in to comment.