S: remove deletable_builds CTE
it was weird having the deleted_builds CTE unused - instead, we can
convert deletable_builds to a subquery and re-add the RETURNING clause
to deleted_builds. we then delete build events from deleted_builds
@@ -18,8 +18,9 @@ func NewCheckLifecycle(conn Conn) CheckLifecycle {
func (cl *checkLifecycle) DeleteCompletedChecks() error {
_, err := cl.conn.Exec(`
- WITH deletable_builds AS
+ WITH deleted_builds AS
(
+ DELETE FROM builds USING (
SELECT id
FROM builds b
WHERE completed AND resource_id IS NOT NULL
@@ -29,12 +30,10 @@ func (cl *checkLifecycle) DeleteCompletedChecks() error {
FROM builds b
WHERE completed AND resource_type_id IS NOT NULL
AND EXISTS (SELECT * FROM builds b2 WHERE b.resource_type_id = b2.resource_type_id AND b.id < b2.id)
- ),
- deleted_builds AS
- (
- DELETE FROM builds USING deletable_builds WHERE builds.id = deletable_builds.id
+ ) AS deletable_builds WHERE builds.id = deletable_builds.id
+ RETURNING builds.id
)
- DELETE FROM check_build_events USING deletable_builds WHERE build_id = deletable_builds.id
+ DELETE FROM check_build_events USING deleted_builds WHERE build_id = deleted_builds.id
`)
return err
}
