Implement Clara's suggestion
got rid of the stupid second query! Thanks Clara!
@@ -25,18 +25,11 @@ type pipelinePauser struct {
}
func (p *pipelinePauser) PausePipelines(daysSinceLastBuild int) error {
- activePipelines, err := getActivePipelines(p.conn, daysSinceLastBuild)
- if err != nil {
- return err
- }
-
rows, err := pipelinesQuery.Where(sq.And{
sq.Eq{
"p.paused": false,
},
- sq.NotEq{
- "p.id": activePipelines,
- },
+ sq.Expr("NOT EXISTS (SELECT 1 FROM jobs j WHERE j.pipeline_id = p.id AND j.last_scheduled > CURRENT_DATE - ?::INTERVAL)", strconv.Itoa(daysSinceLastBuild)+" day"),
}).RunWith(p.conn).Query()
if err != nil {
@@ -57,36 +50,3 @@ func (p *pipelinePauser) PausePipelines(daysSinceLastBuild int) error {
return nil
}
-
-// Couldn't put a subquery inside a WHERE clause: https://github.com/Masterminds/squirrel/issues/258
-// This is a workaround. I really tried to put this in as a subquery similar to
-// how it's done in worker_lifecycle.go but the placeholder value for the days
-// was never getting parsed correctly, even after figuring out that it also
-// needs to be cast to INTERVAL.
-// Maybe I was doing something stupid; there may be a way to get this in as a subquery
-func getActivePipelines(conn Conn, days int) ([]int, error) {
- stmt, err := conn.Prepare(`SELECT p.id FROM pipelines AS p
- LEFT JOIN jobs AS j ON j.pipeline_id = p.id
- WHERE j.last_scheduled > CURRENT_DATE - $1::INTERVAL`)
- if err != nil {
- return nil, err
- }
- defer stmt.Close()
-
- rows, err := stmt.Query(strconv.Itoa(days) + " day")
- if err != nil {
- return nil, err
- }
-
- var pipelineIds []int
- for rows.Next() {
- var id int
- err = rows.Scan(&id)
- if err != nil {
- return nil, err
- }
- pipelineIds = append(pipelineIds, id)
- }
-
- return pipelineIds, nil
-}
