From 8a1623b9c6d2b02388d3cf695c215ac0ae8c9d5d Mon Sep 17 00:00:00 2001 From: liangxu Date: Fri, 19 Jun 2026 16:00:09 +0800 Subject: [PATCH] fix: improve SQL queries for tenant desktop task cancellation and add deleted_at field to PublishRecord --- .../repository/generated/desktop_task.sql.go | 26 ++++++++++++------- .../tenant/repository/generated/models.go | 1 + .../repository/queries/desktop_task.sql | 18 ++++++------- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/server/internal/tenant/repository/generated/desktop_task.sql.go b/server/internal/tenant/repository/generated/desktop_task.sql.go index 31478a2..037dc5f 100644 --- a/server/internal/tenant/repository/generated/desktop_task.sql.go +++ b/server/internal/tenant/repository/generated/desktop_task.sql.go @@ -914,26 +914,26 @@ func (q *Queries) ReconcileDesktopTask(ctx context.Context, arg ReconcileDesktop } const tenantCancelDesktopTask = `-- name: TenantCancelDesktopTask :one -UPDATE desktop_tasks +UPDATE desktop_tasks AS t SET status = 'aborted', error = $1, active_attempt_id = NULL, lease_token_hash = NULL, lease_expires_at = NULL, updated_at = now() -WHERE desktop_id = $2 - AND tenant_id = $3 - AND workspace_id = $4 - AND status = 'queued' +WHERE t.desktop_id = $2 + AND t.tenant_id = $3 + AND t.workspace_id = $4 + AND t.status = 'queued' AND EXISTS ( SELECT 1 FROM desktop_publish_jobs AS j - WHERE j.desktop_id = desktop_tasks.job_id - AND j.tenant_id = desktop_tasks.tenant_id - AND j.workspace_id = desktop_tasks.workspace_id + WHERE j.desktop_id = t.job_id + AND j.tenant_id = t.tenant_id + AND j.workspace_id = t.workspace_id AND j.created_by_user_id = $5 ) -RETURNING id, desktop_id, job_id, tenant_id, workspace_id, target_account_id, target_client_id, platform_id, kind, payload, status, dedup_key, active_attempt_id, lease_token_hash, lease_expires_at, attempts, result, error, created_at, updated_at, priority, lane, lane_weight, source, scheduler_group_key, monitor_task_id, supersedes_task_id, control_flags, interrupt_generation, started_at, interrupted_at, interrupt_reason, enqueued_at, publish_submit_started_at +RETURNING t.id, t.desktop_id, t.job_id, t.tenant_id, t.workspace_id, t.target_account_id, t.target_client_id, t.platform_id, t.kind, t.payload, t.status, t.dedup_key, t.active_attempt_id, t.lease_token_hash, t.lease_expires_at, t.attempts, t.result, t.error, t.created_at, t.updated_at, t.priority, t.lane, t.lane_weight, t.source, t.scheduler_group_key, t.monitor_task_id, t.supersedes_task_id, t.control_flags, t.interrupt_generation, t.started_at, t.interrupted_at, t.interrupt_reason, t.enqueued_at, t.publish_submit_started_at ` type TenantCancelDesktopTaskParams struct { @@ -945,7 +945,13 @@ type TenantCancelDesktopTaskParams struct { } func (q *Queries) TenantCancelDesktopTask(ctx context.Context, arg TenantCancelDesktopTaskParams) (DesktopTask, error) { - row := q.db.QueryRow(ctx, tenantCancelDesktopTask, arg.Error, arg.DesktopID, arg.TenantID, arg.WorkspaceID, arg.UserID) + row := q.db.QueryRow(ctx, tenantCancelDesktopTask, + arg.Error, + arg.DesktopID, + arg.TenantID, + arg.WorkspaceID, + arg.UserID, + ) var i DesktopTask err := row.Scan( &i.ID, diff --git a/server/internal/tenant/repository/generated/models.go b/server/internal/tenant/repository/generated/models.go index 2629eec..5290382 100644 --- a/server/internal/tenant/repository/generated/models.go +++ b/server/internal/tenant/repository/generated/models.go @@ -874,6 +874,7 @@ type PublishRecord struct { UpdatedAt pgtype.Timestamptz `json:"updated_at"` TargetType string `json:"target_type"` TargetConnectionID pgtype.Int8 `json:"target_connection_id"` + DeletedAt pgtype.Timestamptz `json:"deleted_at"` } type QuotaReservation struct { diff --git a/server/internal/tenant/repository/queries/desktop_task.sql b/server/internal/tenant/repository/queries/desktop_task.sql index 6f0cfca..a3059d3 100644 --- a/server/internal/tenant/repository/queries/desktop_task.sql +++ b/server/internal/tenant/repository/queries/desktop_task.sql @@ -190,26 +190,26 @@ WHERE t.desktop_id = sqlc.arg(desktop_id) RETURNING t.*; -- name: TenantCancelDesktopTask :one -UPDATE desktop_tasks +UPDATE desktop_tasks AS t SET status = 'aborted', error = sqlc.narg(error), active_attempt_id = NULL, lease_token_hash = NULL, lease_expires_at = NULL, updated_at = now() -WHERE desktop_id = sqlc.arg(desktop_id) - AND tenant_id = sqlc.arg(tenant_id) - AND workspace_id = sqlc.arg(workspace_id) - AND status = 'queued' +WHERE t.desktop_id = sqlc.arg(desktop_id) + AND t.tenant_id = sqlc.arg(tenant_id) + AND t.workspace_id = sqlc.arg(workspace_id) + AND t.status = 'queued' AND EXISTS ( SELECT 1 FROM desktop_publish_jobs AS j - WHERE j.desktop_id = desktop_tasks.job_id - AND j.tenant_id = desktop_tasks.tenant_id - AND j.workspace_id = desktop_tasks.workspace_id + WHERE j.desktop_id = t.job_id + AND j.tenant_id = t.tenant_id + AND j.workspace_id = t.workspace_id AND j.created_by_user_id = sqlc.arg(user_id) ) -RETURNING *; +RETURNING t.*; -- name: ReconcileDesktopTask :one UPDATE desktop_tasks