Fix cnt query scan. Cosmetic changes.

This commit is contained in:
Jan Eitzinger 2022-11-25 15:15:05 +01:00
parent 948da8f10b
commit fd16a1b637
3 changed files with 16 additions and 8 deletions

View File

@ -630,7 +630,7 @@ func (api *RestApi) deleteJobByRequest(rw http.ResponseWriter, r *http.Request)
// deleteJobBefore godoc
// @summary Remove a job from the sql database
// @tags remove
// @description Job to stop is specified by database ID. This will not remove the job from the job archive.
// @description Remove all jobs with start time before timestamp. The jobs will not be removed from the job archive.
// @produce json
// @param ts path int true "Unix epoch timestamp"
// @success 200 {object} api.DeleteJobApiResponse "Success message"

View File

@ -248,16 +248,24 @@ func (r *JobRepository) Stop(
func (r *JobRepository) DeleteJobsBefore(startTime int64) (int, error) {
var cnt int
q := sq.Select("COUNT(*)").From("job").Where("job.start_time < ?", startTime)
q.QueryRow().Scan(&cnt)
_, err := r.DB.Exec(`DELETE FROM job WHERE job.start_time < ?`, startTime)
qs := fmt.Sprintf("SELECT count(*) FROM job WHERE job.start_time < %d", startTime)
err := r.DB.Get(&cnt, qs) //ignore error as it will also occur in delete statement
_, err = r.DB.Exec(`DELETE FROM job WHERE job.start_time < ?`, startTime)
if err != nil {
log.Warnf(" DeleteJobsBefore(%d): error %v", startTime, err)
} else {
log.Infof("DeleteJobsBefore(%d): Deleted %d jobs", startTime, cnt)
}
return cnt, err
}
func (r *JobRepository) DeleteJobById(id int64) error {
_, err := r.DB.Exec(`DELETE FROM job WHERE job.id = ?`, id)
if err != nil {
log.Warnf("DeleteJobById(%d): error %v", id, err)
} else {
log.Infof("DeleteJobById(%d): Success", id)
}
return err
}