2021-09-06 13:05:10 +02:00
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d *SqlDb) CreateSchedule(schedule db.Schedule) (newSchedule db.Schedule, err error) {
|
|
|
|
insertID, err := d.insert(
|
|
|
|
"id",
|
2024-06-30 23:12:49 +02:00
|
|
|
"insert into project__schedule (project_id, template_id, cron_format, repository_id, `name`, `active`)"+
|
|
|
|
"values (?, ?, ?, ?, ?, ?)",
|
2021-09-06 13:05:10 +02:00
|
|
|
schedule.ProjectID,
|
|
|
|
schedule.TemplateID,
|
2022-01-18 22:09:42 +01:00
|
|
|
schedule.CronFormat,
|
2024-06-30 23:12:49 +02:00
|
|
|
schedule.RepositoryID,
|
|
|
|
schedule.Name,
|
|
|
|
schedule.Active)
|
2021-09-06 13:05:10 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newSchedule = schedule
|
|
|
|
newSchedule.ID = insertID
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-18 22:09:42 +01:00
|
|
|
func (d *SqlDb) SetScheduleLastCommitHash(projectID int, scheduleID int, lastCommentHash string) error {
|
|
|
|
_, err := d.exec("update project__schedule set "+
|
|
|
|
"last_commit_hash=? "+
|
|
|
|
"where project_id=? and id=?",
|
|
|
|
lastCommentHash,
|
|
|
|
projectID,
|
|
|
|
scheduleID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:05:10 +02:00
|
|
|
func (d *SqlDb) UpdateSchedule(schedule db.Schedule) error {
|
2022-01-18 22:09:42 +01:00
|
|
|
_, err := d.exec("update project__schedule set "+
|
2022-01-19 11:30:22 +01:00
|
|
|
"cron_format=?, "+
|
2022-01-30 18:43:15 +01:00
|
|
|
"repository_id=?, "+
|
2024-06-30 23:12:49 +02:00
|
|
|
"template_id=?, "+
|
|
|
|
"`name`=?, "+
|
|
|
|
"`active`=?, "+
|
2022-01-31 14:04:47 +01:00
|
|
|
"last_commit_hash = NULL "+
|
2022-01-18 22:09:42 +01:00
|
|
|
"where project_id=? and id=?",
|
2021-09-06 13:05:10 +02:00
|
|
|
schedule.CronFormat,
|
2022-01-18 22:09:42 +01:00
|
|
|
schedule.RepositoryID,
|
2024-06-30 23:12:49 +02:00
|
|
|
schedule.TemplateID,
|
|
|
|
schedule.Name,
|
|
|
|
schedule.Active,
|
2021-09-06 13:05:10 +02:00
|
|
|
schedule.ProjectID,
|
|
|
|
schedule.ID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) GetSchedule(projectID int, scheduleID int) (template db.Schedule, err error) {
|
|
|
|
err = d.selectOne(
|
|
|
|
&template,
|
|
|
|
"select * from project__schedule where project_id=? and id=?",
|
|
|
|
projectID,
|
|
|
|
scheduleID)
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = db.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) DeleteSchedule(projectID int, scheduleID int) error {
|
2021-09-09 20:56:15 +02:00
|
|
|
_, err := d.exec("delete from project__schedule where project_id=? and id=?", projectID, scheduleID)
|
2021-09-06 13:05:10 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) GetSchedules() (schedules []db.Schedule, err error) {
|
2021-09-06 14:55:26 +02:00
|
|
|
_, err = d.selectAll(&schedules, "select * from project__schedule where cron_format != ''")
|
2021-09-06 13:05:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-23 19:24:22 +02:00
|
|
|
func (d *SqlDb) GetProjectSchedules(projectID int) (schedules []db.ScheduleWithTpl, err error) {
|
2024-06-22 19:32:53 +02:00
|
|
|
_, err = d.selectAll(&schedules,
|
2024-06-23 19:24:22 +02:00
|
|
|
"SELECT ps.*, pt.name as tpl_name FROM project__schedule ps "+
|
|
|
|
"JOIN project__template pt ON pt.id = ps.template_id "+
|
2024-06-23 20:59:14 +02:00
|
|
|
"WHERE ps.repository_id IS NULL AND ps.project_id=?",
|
2024-06-22 19:32:53 +02:00
|
|
|
projectID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:05:10 +02:00
|
|
|
func (d *SqlDb) GetTemplateSchedules(projectID int, templateID int) (schedules []db.Schedule, err error) {
|
|
|
|
_, err = d.selectAll(&schedules,
|
2024-07-22 12:32:59 +02:00
|
|
|
"SELECT * FROM project__schedule WHERE project_id=? AND template_id=? AND repository_id IS NOT NULL",
|
2021-09-06 13:05:10 +02:00
|
|
|
projectID,
|
|
|
|
templateID)
|
|
|
|
return
|
|
|
|
}
|
2022-01-30 18:43:15 +01:00
|
|
|
|
2024-06-30 23:12:49 +02:00
|
|
|
func (d *SqlDb) SetScheduleActive(projectID int, scheduleID int, active bool) error {
|
|
|
|
_, err := d.exec("update project__schedule set `active`=? where project_id=? and id=?",
|
|
|
|
active,
|
|
|
|
projectID,
|
|
|
|
scheduleID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-30 18:43:15 +01:00
|
|
|
func (d *SqlDb) SetScheduleCommitHash(projectID int, scheduleID int, hash string) error {
|
|
|
|
_, err := d.exec("update project__schedule set last_commit_hash=? where project_id=? and id=?",
|
|
|
|
hash,
|
|
|
|
projectID,
|
|
|
|
scheduleID)
|
|
|
|
return err
|
|
|
|
}
|