Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: limit project filter (#20010)

ecmadao314eb69f8ac3
diff --git a/backend/store/instance.go b/backend/store/instance.go
index e7ca7f2c34..9699f4a965 100644
--- a/backend/store/instance.go
+++ b/backend/store/instance.go
@@ -566,12 +566,13 @@ func (s *Store) DeleteInstance(ctx context.Context, workspace string, resourceID
// Delete task_run_log entries for tasks associated with this instance
q = qb.Q().Space(`
- DELETE FROM task_run_log
- WHERE task_run_id IN (
- SELECT tr.id FROM task_run tr
- JOIN task t ON t.project = tr.project AND t.id = tr.task_id
- WHERE t.instance = ?
- )
+ DELETE FROM task_run_log trl
+ USING task_run tr, task t
+ WHERE trl.project = tr.project
+ AND trl.task_run_id = tr.id
+ AND tr.project = t.project
+ AND tr.task_id = t.id
+ AND t.instance = ?
`, resourceID)
query, args, err = q.ToSQL()
if err != nil {
@@ -583,10 +584,11 @@ func (s *Store) DeleteInstance(ctx context.Context, workspace string, resourceID
// Delete task_run entries for tasks associated with this instance
q = qb.Q().Space(`
- DELETE FROM task_run
- WHERE task_id IN (
- SELECT id FROM task WHERE instance = ?
- )
+ DELETE FROM task_run tr
+ USING task t
+ WHERE tr.project = t.project
+ AND tr.task_id = t.id
+ AND t.instance = ?
`, resourceID)
query, args, err = q.ToSQL()
if err != nil {
diff --git a/backend/store/plan_check_run.go b/backend/store/plan_check_run.go
index 575f48714d..d589311cbc 100644
--- a/backend/store/plan_check_run.go
+++ b/backend/store/plan_check_run.go
@@ -264,8 +264,8 @@ func (s *Store) ClaimAvailablePlanCheckRuns(ctx context.Context) ([]*ClaimedPlan
q := qb.Q().Space(`
UPDATE plan_check_run
SET status = ?, updated_at = now()
- WHERE id IN (
- SELECT id FROM plan_check_run
+ WHERE (project, id) IN (
+ SELECT project, id FROM plan_check_run
WHERE status = ?
FOR UPDATE SKIP LOCKED
)
diff --git a/backend/store/project.go b/backend/store/project.go
index 55a09db29c..256e0ea182 100644
--- a/backend/store/project.go
+++ b/backend/store/project.go
@@ -381,8 +381,7 @@ func (s *Store) DeleteProject(ctx context.Context, workspace string, resourceID
}
// Delete issue_comment entries for issues in this project
- q = qb.Q().Space("DELETE FROM issue_comment")
- q.Space("WHERE issue_id IN (SELECT id FROM issue WHERE project = ?)", resourceID)
+ q = qb.Q().Space("DELETE FROM issue_comment WHERE project = ?", resourceID)
sql, args, err = q.ToSQL()
if err != nil {
return errors.Wrap(err, "failed to build issue_comment delete query")
@@ -402,8 +401,7 @@ func (s *Store) DeleteProject(ctx context.Context, workspace string, resourceID
}
// Delete plan_webhook_delivery entries for plans in this project
- q = qb.Q().Space("DELETE FROM plan_webhook_delivery")
- q.Space("WHERE plan_id IN (SELECT id FROM plan WHERE project = ?)", resourceID)
+ q = qb.Q().Space("DELETE FROM plan_webhook_delivery WHERE project = ?", resourceID)
sql, args, err = q.ToSQL()
if err != nil {
return errors.Wrap(err, "failed to build plan_webhook_delivery delete query")
@@ -413,8 +411,7 @@ func (s *Store) DeleteProject(ctx context.Context, workspace string, resourceID
}
// Delete plan_check_run entries for plans in this project
- q = qb.Q().Space("DELETE FROM plan_check_run")
- q.Space("WHERE plan_id IN (SELECT id FROM plan WHERE project = ?)", resourceID)
+ q = qb.Q().Space("DELETE FROM plan_check_run WHERE project = ?", resourceID)
sql, args, err = q.ToSQL()
if err != nil {
return errors.Wrap(err, "failed to build plan_check_run delete query")
@@ -424,12 +421,7 @@ func (s *Store) DeleteProject(ctx context.Context, workspace string, resourceID
}
// Delete task_run_log entries for tasks in plans of this project
- q = qb.Q().Space("DELETE FROM task_run_log")
- q.Space("WHERE task_run_id IN (")
- q.Space("SELECT tr.id FROM task_run tr")
- q.Space("JOIN task t ON t.project = tr.project AND t.id = tr.task_id")
- q.Space("JOIN plan p ON p.project = t.project AND p.id = t.plan_id")
- q.Space("WHERE p.project = ?)", resourceID)
+ q = qb.Q().Space("DELETE FROM task_run_log WHERE project = ?", resourceID)
sql, args, err = q.ToSQL()
if err != nil {
return errors.Wrap(err, "failed to build task_run_log delete query")
@@ -439,11 +431,7 @@ func (s *Store) DeleteProject(ctx context.Context, workspace string, resourceID
}
// Delete task_run entries for tasks in plans of this project
- q = qb.Q().Space("DELETE FROM task_run")
- q.Space("WHERE task_id IN (")
- q.Space("SELECT t.id FROM task t")
- q.Space("JOIN plan p ON p.project = t.project AND p.id = t.plan_id")
- q.Space("WHERE p.project = ?)", resourceID)
+ q = qb.Q().Space("DELETE FROM task_run WHERE project = ?", resourceID)
sql, args, err = q.ToSQL()
if err != nil {
return errors.Wrap(err, "failed to build task_run delete query")
@@ -453,8 +441,7 @@ func (s *Store) DeleteProject(ctx context.Context, workspace string, resourceID
}
// Delete tasks in plans of this project
- q = qb.Q().Space("DELETE FROM task")
- q.Space("WHERE plan_id IN (SELECT id FROM plan WHERE project = ?)", resourceID)
+ q = qb.Q().Space("DELETE FROM task WHERE project = ?", resourceID)
sql, args, err = q.ToSQL()
if err != nil {
return errors.Wrap(err, "failed to build task delete query")
diff --git a/backend/store/task_run.go b/backend/store/task_run.go
index d5a4e4ca99..9db0031af4 100644
--- a/backend/store/task_run.go
+++ b/backend/store/task_run.go
@@ -239,8 +239,8 @@ func (s *Store) ClaimAvailableTaskRuns(ctx context.Context, replicaID string) ([
q := qb.Q().Space(`
UPDATE task_run
SET status = ?, updated_at = now(), replica_id = ?
- WHERE id IN (
- SELECT task_run.id FROM task_run
+ WHERE (project, id) IN (
+ SELECT task_run.project, task_run.id FROM task_run
WHERE task_run.status = ?
FOR UPDATE SKIP LOCKED
)