Skip to content

Commit

Permalink
refactor(db): return schedule on create and update
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Jul 25, 2023
1 parent 41fdfd1 commit 003a23d
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 44 deletions.
10 changes: 2 additions & 8 deletions api/schedule/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,30 +170,24 @@ func CreateSchedule(c *gin.Context) {
dbSchedule.SetActive(true)

// send API call to update the schedule
err = database.FromContext(c).UpdateSchedule(dbSchedule, true)
s, err = database.FromContext(c).UpdateSchedule(dbSchedule, true)
if err != nil {
retErr := fmt.Errorf("unable to set schedule %s to active: %w", dbSchedule.GetName(), err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

// send API call to capture the updated schedule
s, _ = database.FromContext(c).GetScheduleForRepo(r, dbSchedule.GetName())
} else {
// send API call to create the schedule
err = database.FromContext(c).CreateSchedule(s)
s, err = database.FromContext(c).CreateSchedule(s)
if err != nil {
retErr := fmt.Errorf("unable to create new schedule %s: %w", r.GetName(), err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

// send API call to capture the created schedule
s, _ = database.FromContext(c).GetScheduleForRepo(r, input.GetName())
}

c.JSON(http.StatusCreated, s)
Expand Down
5 changes: 1 addition & 4 deletions api/schedule/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func UpdateSchedule(c *gin.Context) {
s.SetUpdatedBy(u.GetName())

// update the schedule within the database
err = database.FromContext(c).UpdateSchedule(s, true)
s, err = database.FromContext(c).UpdateSchedule(s, true)
if err != nil {
retErr := fmt.Errorf("unable to update scheduled %s: %w", scheduleName, err)

Expand All @@ -137,8 +137,5 @@ func UpdateSchedule(c *gin.Context) {
return
}

// capture the updated scheduled
s, _ = database.FromContext(c).GetScheduleForRepo(r, scheduleName)

c.JSON(http.StatusOK, s)
}
2 changes: 1 addition & 1 deletion cmd/vela-server/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func processSchedules(start time.Time, compiler compiler.Engine, database databa
schedule.SetScheduledAt(time.Now().UTC().Unix())

// send API call to update schedule for ensuring scheduled_at field is set
err = database.UpdateSchedule(schedule, false)
_, err = database.UpdateSchedule(schedule, false)
if err != nil {
logrus.WithError(err).Warnf("%s %s", scheduleErr, schedule.GetName())

Expand Down
4 changes: 2 additions & 2 deletions database/schedule/count_active_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ func TestSchedule_Engine_CountActiveSchedules(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSchedule(_scheduleOne)
_, err := _sqlite.CreateSchedule(_scheduleOne)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}

err = _sqlite.CreateSchedule(_scheduleTwo)
_, err = _sqlite.CreateSchedule(_scheduleTwo)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions database/schedule/count_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func TestSchedule_Engine_CountSchedulesForRepo(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSchedule(_scheduleOne)
_, err := _sqlite.CreateSchedule(_scheduleOne)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}

err = _sqlite.CreateSchedule(_scheduleTwo)
_, err = _sqlite.CreateSchedule(_scheduleTwo)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions database/schedule/count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func TestSchedule_Engine_CountSchedules(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSchedule(_scheduleOne)
_, err := _sqlite.CreateSchedule(_scheduleOne)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}

err = _sqlite.CreateSchedule(_scheduleTwo)
_, err = _sqlite.CreateSchedule(_scheduleTwo)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}
Expand Down
11 changes: 5 additions & 6 deletions database/schedule/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// CreateSchedule creates a new schedule in the database.
func (e *engine) CreateSchedule(s *library.Schedule) error {
func (e *engine) CreateSchedule(s *library.Schedule) (*library.Schedule, error) {
e.logger.WithFields(logrus.Fields{
"schedule": s.GetName(),
}).Tracef("creating schedule %s in the database", s.GetName())
Expand All @@ -24,12 +24,11 @@ func (e *engine) CreateSchedule(s *library.Schedule) error {
// validate the necessary fields are populated
err := schedule.Validate()
if err != nil {
return err
return nil, err
}

// send query to the database
return e.client.
Table(constants.TableSchedule).
Create(schedule).
Error
result := e.client.Table(constants.TableSchedule).Create(schedule)

return schedule.ToLibrary(), result.Error
}
7 changes: 6 additions & 1 deletion database/schedule/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package schedule

import (
"reflect"
"testing"

"github.com/DATA-DOG/go-sqlmock"
Expand Down Expand Up @@ -58,7 +59,7 @@ VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10) RETURNING "id"`).
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := test.database.CreateSchedule(_schedule)
got, err := test.database.CreateSchedule(_schedule)

if test.failure {
if err == nil {
Expand All @@ -71,6 +72,10 @@ VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10) RETURNING "id"`).
if err != nil {
t.Errorf("CreateSchedule for %s returned err: %v", test.name, err)
}

if !reflect.DeepEqual(got, _schedule) {
t.Errorf("CreateSchedule for %s returned %s, want %s", test.name, got, _schedule)
}
})
}
}
2 changes: 1 addition & 1 deletion database/schedule/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestSchedule_Engine_DeleteSchedule(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSchedule(_schedule)
_, err := _sqlite.CreateSchedule(_schedule)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion database/schedule/get_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestSchedule_Engine_GetScheduleForRepo(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSchedule(_schedule)
_, err := _sqlite.CreateSchedule(_schedule)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion database/schedule/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestSchedule_Engine_GetSchedule(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSchedule(_schedule)
_, err := _sqlite.CreateSchedule(_schedule)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions database/schedule/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ScheduleInterface interface {
// CountSchedulesForRepo defines a function that gets the count of schedules by repo ID.
CountSchedulesForRepo(*library.Repo) (int64, error)
// CreateSchedule defines a function that creates a new schedule.
CreateSchedule(*library.Schedule) error
CreateSchedule(*library.Schedule) (*library.Schedule, error)
// DeleteSchedule defines a function that deletes an existing schedule.
DeleteSchedule(*library.Schedule) error
// GetSchedule defines a function that gets a schedule by ID.
Expand All @@ -45,5 +45,5 @@ type ScheduleInterface interface {
// ListSchedulesForRepo defines a function that gets a list of schedules by repo ID.
ListSchedulesForRepo(*library.Repo, int, int) ([]*library.Schedule, int64, error)
// UpdateSchedule defines a function that updates an existing schedule.
UpdateSchedule(*library.Schedule, bool) error
UpdateSchedule(*library.Schedule, bool) (*library.Schedule, error)
}
4 changes: 2 additions & 2 deletions database/schedule/list_active_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func TestSchedule_Engine_ListActiveSchedules(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSchedule(_scheduleOne)
_, err := _sqlite.CreateSchedule(_scheduleOne)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}

err = _sqlite.CreateSchedule(_scheduleTwo)
_, err = _sqlite.CreateSchedule(_scheduleTwo)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions database/schedule/list_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ func TestSchedule_Engine_ListSchedulesForRepo(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSchedule(_scheduleOne)
_, err := _sqlite.CreateSchedule(_scheduleOne)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}

err = _sqlite.CreateSchedule(_scheduleTwo)
_, err = _sqlite.CreateSchedule(_scheduleTwo)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions database/schedule/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ func TestSchedule_Engine_ListSchedules(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSchedule(_scheduleOne)
_, err := _sqlite.CreateSchedule(_scheduleOne)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}

err = _sqlite.CreateSchedule(_scheduleTwo)
_, err = _sqlite.CreateSchedule(_scheduleTwo)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions database/schedule/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// UpdateSchedule updates an existing schedule in the database.
func (e *engine) UpdateSchedule(s *library.Schedule, fields bool) error {
func (e *engine) UpdateSchedule(s *library.Schedule, fields bool) (*library.Schedule, error) {
e.logger.WithFields(logrus.Fields{
"schedule": s.GetName(),
}).Tracef("updating schedule %s in the database", s.GetName())
Expand All @@ -23,7 +23,7 @@ func (e *engine) UpdateSchedule(s *library.Schedule, fields bool) error {
// validate the necessary fields are populated
err := schedule.Validate()
if err != nil {
return err
return nil, err
}

// If "fields" is true, update entire record; otherwise, just update scheduled_at (part of processSchedule)
Expand All @@ -36,5 +36,5 @@ func (e *engine) UpdateSchedule(s *library.Schedule, fields bool) error {
err = e.client.Table(constants.TableSchedule).Model(schedule).UpdateColumn("scheduled_at", s.GetScheduledAt()).Error
}

return err
return schedule.ToLibrary(), err
}
18 changes: 14 additions & 4 deletions database/schedule/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package schedule

import (
"reflect"
"testing"

"github.com/DATA-DOG/go-sqlmock"
Expand Down Expand Up @@ -40,7 +41,7 @@ WHERE "id" = $10`).
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSchedule(_schedule)
_, err := _sqlite.CreateSchedule(_schedule)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}
Expand All @@ -66,7 +67,8 @@ WHERE "id" = $10`).
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err = test.database.UpdateSchedule(_schedule, true)
got, err := test.database.UpdateSchedule(_schedule, true)
_schedule.SetUpdatedAt(got.GetUpdatedAt())

if test.failure {
if err == nil {
Expand All @@ -79,6 +81,10 @@ WHERE "id" = $10`).
if err != nil {
t.Errorf("UpdateSchedule for %s returned err: %v", test.name, err)
}

if !reflect.DeepEqual(got, _schedule) {
t.Errorf("UpdateSchedule for %s returned %s, want %s", test.name, got, _schedule)
}
})
}
}
Expand Down Expand Up @@ -112,7 +118,7 @@ func TestSchedule_Engine_UpdateSchedule_NotConfig(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSchedule(_schedule)
_, err := _sqlite.CreateSchedule(_schedule)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}
Expand All @@ -138,7 +144,7 @@ func TestSchedule_Engine_UpdateSchedule_NotConfig(t *testing.T) {
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err = test.database.UpdateSchedule(_schedule, false)
got, err := test.database.UpdateSchedule(_schedule, false)

if test.failure {
if err == nil {
Expand All @@ -151,6 +157,10 @@ func TestSchedule_Engine_UpdateSchedule_NotConfig(t *testing.T) {
if err != nil {
t.Errorf("UpdateSchedule for %s returned err: %v", test.name, err)
}

if !reflect.DeepEqual(got, _schedule) {
t.Errorf("CreateSchedule for %s returned %s, want %s", test.name, got, _schedule)
}
})
}
}

0 comments on commit 003a23d

Please sign in to comment.