mirror of
https://github.com/multica-ai/multica.git
synced 2026-08-07 11:14:28 +02:00
* fix(workspace): optimize application-owned deletion Co-authored-by: multica-agent <github@multica.ai> * fix(workspace): address deletion review findings Co-authored-by: multica-agent <github@multica.ai> --------- Co-authored-by: Eve <eve@multica-ai.local> Co-authored-by: multica-agent <github@multica.ai>
419 lines
13 KiB
SQL
419 lines
13 KiB
SQL
-- Workspace deletion is application-owned. These statements form a fixed,
|
|
-- bottom-up deletion plan; the legacy foreign keys remain only as a
|
|
-- compatibility safety net during the expand phase.
|
|
|
|
-- name: SetWorkspaceTeardownMode :exec
|
|
-- The setting is transaction-local. Migration 242 makes only the three DELETE
|
|
-- dirty triggers skip their per-row work while this flag is on.
|
|
SELECT set_config('multica.workspace_teardown', 'on', true);
|
|
|
|
-- name: LockTaskUsageRollupForWorkspaceDelete :exec
|
|
-- The hourly rollup uses session advisory lock 4246. The transaction-scoped
|
|
-- lock shares that namespace: an in-flight rollup finishes first, then no new
|
|
-- rollup can write the workspace's aggregates until this delete commits.
|
|
SELECT pg_advisory_xact_lock(4246);
|
|
|
|
-- name: PrepareWorkspaceDeletionLinks :exec
|
|
-- Break self-references and the task/run cycle explicitly before deleting
|
|
-- either side. This keeps their ordering in the application-owned graph
|
|
-- instead of relying on ON DELETE SET NULL actions.
|
|
WITH
|
|
ws_agents AS MATERIALIZED (
|
|
SELECT id FROM agent WHERE agent.workspace_id = $1
|
|
),
|
|
ws_runtimes AS MATERIALIZED (
|
|
SELECT id FROM agent_runtime WHERE agent_runtime.workspace_id = $1
|
|
),
|
|
ws_issues AS MATERIALIZED (
|
|
SELECT id FROM issue WHERE issue.workspace_id = $1
|
|
),
|
|
ws_tasks AS MATERIALIZED (
|
|
-- Keep all three ownership paths until the application enforces that a
|
|
-- task's agent/runtime/issue always belong to the same workspace. The OR
|
|
-- can scan globally, but simplifying it before that invariant exists
|
|
-- would turn a performance optimization into a tenant-cleanup bug.
|
|
SELECT id
|
|
FROM agent_task_queue
|
|
WHERE agent_id IN (SELECT id FROM ws_agents)
|
|
OR issue_id IN (SELECT id FROM ws_issues)
|
|
OR runtime_id IN (SELECT id FROM ws_runtimes)
|
|
),
|
|
detached_tasks AS (
|
|
UPDATE agent_task_queue
|
|
SET parent_task_id = NULL,
|
|
autopilot_run_id = NULL
|
|
WHERE id IN (SELECT id FROM ws_tasks)
|
|
AND (parent_task_id IS NOT NULL OR autopilot_run_id IS NOT NULL)
|
|
),
|
|
detached_comments AS (
|
|
UPDATE comment
|
|
SET parent_id = NULL
|
|
WHERE comment.workspace_id = $1
|
|
AND parent_id IS NOT NULL
|
|
),
|
|
detached_issues AS (
|
|
UPDATE issue
|
|
SET parent_issue_id = NULL
|
|
WHERE issue.workspace_id = $1
|
|
AND parent_issue_id IS NOT NULL
|
|
)
|
|
UPDATE webhook_delivery
|
|
SET replayed_from_delivery_id = NULL
|
|
WHERE webhook_delivery.workspace_id = $1
|
|
AND replayed_from_delivery_id IS NOT NULL;
|
|
|
|
-- name: DeleteWorkspaceLeafData :exec
|
|
WITH
|
|
ws_agents AS MATERIALIZED (
|
|
SELECT id FROM agent WHERE workspace_id = $1
|
|
),
|
|
ws_runtimes AS MATERIALIZED (
|
|
SELECT id FROM agent_runtime WHERE workspace_id = $1
|
|
),
|
|
ws_issues AS MATERIALIZED (
|
|
SELECT id FROM issue WHERE workspace_id = $1
|
|
),
|
|
ws_labels AS MATERIALIZED (
|
|
SELECT id FROM issue_label WHERE workspace_id = $1
|
|
),
|
|
ws_skills AS MATERIALIZED (
|
|
SELECT id FROM skill WHERE workspace_id = $1
|
|
),
|
|
ws_squads AS MATERIALIZED (
|
|
SELECT id FROM squad WHERE workspace_id = $1
|
|
),
|
|
ws_tasks AS MATERIALIZED (
|
|
SELECT id
|
|
FROM agent_task_queue
|
|
WHERE agent_id IN (SELECT id FROM ws_agents)
|
|
OR issue_id IN (SELECT id FROM ws_issues)
|
|
OR runtime_id IN (SELECT id FROM ws_runtimes)
|
|
),
|
|
ws_sessions AS MATERIALIZED (
|
|
SELECT id FROM chat_session WHERE workspace_id = $1
|
|
),
|
|
ws_autopilots AS MATERIALIZED (
|
|
SELECT id FROM autopilot WHERE workspace_id = $1
|
|
),
|
|
ws_github_prs AS MATERIALIZED (
|
|
SELECT id FROM github_pull_request WHERE workspace_id = $1
|
|
),
|
|
ws_vcs_prs AS MATERIALIZED (
|
|
SELECT id FROM vcs_pull_request WHERE workspace_id = $1
|
|
),
|
|
ws_vcs_connections AS MATERIALIZED (
|
|
SELECT id FROM vcs_connection WHERE workspace_id = $1
|
|
),
|
|
ws_channel_installations AS MATERIALIZED (
|
|
SELECT id FROM channel_installation WHERE workspace_id = $1
|
|
),
|
|
ws_lark_installations AS MATERIALIZED (
|
|
SELECT id FROM lark_installation WHERE workspace_id = $1
|
|
),
|
|
deleted_task_usage AS (
|
|
DELETE FROM task_usage
|
|
WHERE task_id IN (SELECT id FROM ws_tasks)
|
|
),
|
|
deleted_task_messages AS (
|
|
DELETE FROM task_message
|
|
WHERE task_id IN (SELECT id FROM ws_tasks)
|
|
),
|
|
deleted_task_tokens AS (
|
|
DELETE FROM task_token
|
|
WHERE workspace_id = $1
|
|
OR task_id IN (SELECT id FROM ws_tasks)
|
|
OR agent_id IN (SELECT id FROM ws_agents)
|
|
),
|
|
deleted_hourly_dirty AS (
|
|
DELETE FROM task_usage_hourly_dirty WHERE workspace_id = $1
|
|
),
|
|
deleted_hourly AS (
|
|
DELETE FROM task_usage_hourly WHERE workspace_id = $1
|
|
),
|
|
deleted_attachments AS (
|
|
DELETE FROM attachment WHERE workspace_id = $1
|
|
),
|
|
deleted_channel_outbound_cards AS (
|
|
DELETE FROM channel_outbound_card_message
|
|
WHERE chat_session_id IN (SELECT id FROM ws_sessions)
|
|
OR task_id IN (SELECT id FROM ws_tasks)
|
|
),
|
|
deleted_lark_outbound_cards AS (
|
|
DELETE FROM lark_outbound_card_message
|
|
WHERE chat_session_id IN (SELECT id FROM ws_sessions)
|
|
OR task_id IN (SELECT id FROM ws_tasks)
|
|
),
|
|
deleted_draft_restores AS (
|
|
DELETE FROM chat_draft_restore
|
|
WHERE chat_session_id IN (SELECT id FROM ws_sessions)
|
|
OR task_id IN (SELECT id FROM ws_tasks)
|
|
),
|
|
deleted_comment_reactions AS (
|
|
DELETE FROM comment_reaction WHERE workspace_id = $1
|
|
),
|
|
deleted_issue_reactions AS (
|
|
DELETE FROM issue_reaction WHERE workspace_id = $1
|
|
),
|
|
deleted_activity AS (
|
|
DELETE FROM activity_log WHERE workspace_id = $1
|
|
),
|
|
deleted_inbox AS (
|
|
DELETE FROM inbox_item WHERE workspace_id = $1
|
|
),
|
|
deleted_issue_dependencies AS (
|
|
DELETE FROM issue_dependency
|
|
WHERE issue_id IN (SELECT id FROM ws_issues)
|
|
OR depends_on_issue_id IN (SELECT id FROM ws_issues)
|
|
),
|
|
deleted_issue_subscribers AS (
|
|
DELETE FROM issue_subscriber
|
|
WHERE issue_id IN (SELECT id FROM ws_issues)
|
|
),
|
|
deleted_issue_labels AS (
|
|
DELETE FROM issue_to_label
|
|
WHERE issue_id IN (SELECT id FROM ws_issues)
|
|
OR label_id IN (SELECT id FROM ws_labels)
|
|
),
|
|
deleted_agent_labels AS (
|
|
DELETE FROM agent_to_label
|
|
WHERE agent_id IN (SELECT id FROM ws_agents)
|
|
OR label_id IN (SELECT id FROM ws_labels)
|
|
),
|
|
deleted_skill_labels AS (
|
|
DELETE FROM skill_to_label
|
|
WHERE skill_id IN (SELECT id FROM ws_skills)
|
|
OR label_id IN (SELECT id FROM ws_labels)
|
|
),
|
|
deleted_issue_github_links AS (
|
|
DELETE FROM issue_pull_request
|
|
WHERE issue_id IN (SELECT id FROM ws_issues)
|
|
OR pull_request_id IN (SELECT id FROM ws_github_prs)
|
|
),
|
|
deleted_issue_vcs_links AS (
|
|
DELETE FROM issue_vcs_pull_request
|
|
WHERE issue_id IN (SELECT id FROM ws_issues)
|
|
OR pull_request_id IN (SELECT id FROM ws_vcs_prs)
|
|
),
|
|
deleted_agent_invocation_targets AS (
|
|
DELETE FROM agent_invocation_target
|
|
WHERE agent_id IN (SELECT id FROM ws_agents)
|
|
),
|
|
deleted_agent_skills AS (
|
|
DELETE FROM agent_skill
|
|
WHERE agent_id IN (SELECT id FROM ws_agents)
|
|
OR skill_id IN (SELECT id FROM ws_skills)
|
|
),
|
|
deleted_skill_files AS (
|
|
DELETE FROM skill_file
|
|
WHERE skill_id IN (SELECT id FROM ws_skills)
|
|
),
|
|
deleted_daemon_connections AS (
|
|
DELETE FROM daemon_connection
|
|
WHERE agent_id IN (SELECT id FROM ws_agents)
|
|
),
|
|
deleted_squad_members AS (
|
|
DELETE FROM squad_member
|
|
WHERE squad_id IN (SELECT id FROM ws_squads)
|
|
),
|
|
deleted_project_resources AS (
|
|
DELETE FROM project_resource WHERE workspace_id = $1
|
|
),
|
|
deleted_autopilot_collaborators AS (
|
|
DELETE FROM autopilot_collaborator
|
|
WHERE autopilot_id IN (SELECT id FROM ws_autopilots)
|
|
),
|
|
deleted_autopilot_subscribers AS (
|
|
DELETE FROM autopilot_subscriber
|
|
WHERE autopilot_id IN (SELECT id FROM ws_autopilots)
|
|
),
|
|
deleted_webhook_deliveries AS (
|
|
DELETE FROM webhook_delivery WHERE workspace_id = $1
|
|
),
|
|
deleted_github_check_runs AS (
|
|
DELETE FROM github_pull_request_check_run
|
|
WHERE pr_id IN (SELECT id FROM ws_github_prs)
|
|
),
|
|
deleted_github_check_suites AS (
|
|
DELETE FROM github_pull_request_check_suite
|
|
WHERE pr_id IN (SELECT id FROM ws_github_prs)
|
|
),
|
|
deleted_pending_github_suites AS (
|
|
DELETE FROM github_pending_check_suite WHERE workspace_id = $1
|
|
),
|
|
deleted_vcs_commit_statuses AS (
|
|
DELETE FROM vcs_commit_status
|
|
WHERE connection_id IN (SELECT id FROM ws_vcs_connections)
|
|
),
|
|
deleted_channel_chat_bindings AS (
|
|
DELETE FROM channel_chat_session_binding
|
|
WHERE installation_id IN (SELECT id FROM ws_channel_installations)
|
|
OR chat_session_id IN (SELECT id FROM ws_sessions)
|
|
),
|
|
deleted_channel_inbound_dedup AS (
|
|
DELETE FROM channel_inbound_message_dedup
|
|
WHERE installation_id IN (SELECT id FROM ws_channel_installations)
|
|
),
|
|
deleted_channel_inbound_audit AS (
|
|
DELETE FROM channel_inbound_audit
|
|
WHERE installation_id IN (SELECT id FROM ws_channel_installations)
|
|
),
|
|
deleted_channel_user_bindings AS (
|
|
DELETE FROM channel_user_binding WHERE workspace_id = $1
|
|
),
|
|
deleted_channel_binding_tokens AS (
|
|
DELETE FROM channel_binding_token WHERE workspace_id = $1
|
|
),
|
|
deleted_lark_chat_bindings AS (
|
|
DELETE FROM lark_chat_session_binding
|
|
WHERE installation_id IN (SELECT id FROM ws_lark_installations)
|
|
OR chat_session_id IN (SELECT id FROM ws_sessions)
|
|
),
|
|
deleted_lark_inbound_dedup AS (
|
|
DELETE FROM lark_inbound_message_dedup
|
|
WHERE installation_id IN (SELECT id FROM ws_lark_installations)
|
|
),
|
|
deleted_lark_inbound_audit AS (
|
|
DELETE FROM lark_inbound_audit
|
|
WHERE installation_id IN (SELECT id FROM ws_lark_installations)
|
|
),
|
|
deleted_lark_user_bindings AS (
|
|
DELETE FROM lark_user_binding WHERE workspace_id = $1
|
|
),
|
|
deleted_lark_binding_tokens AS (
|
|
DELETE FROM lark_binding_token WHERE workspace_id = $1
|
|
)
|
|
-- Keep the two-system cleanup ledger until object storage has been settled.
|
|
-- Moving every row out of pending also prevents a concurrent media bind from
|
|
-- attaching an object after the workspace teardown commits. The reconciler
|
|
-- performs the idempotent object delete and clears the row afterwards.
|
|
UPDATE channel_media_pending_object
|
|
SET state = CASE
|
|
WHEN state = 'tombstoned' THEN 'tombstoned'
|
|
ELSE 'deleting'
|
|
END,
|
|
lease_token = NULL,
|
|
lease_expires_at = NULL,
|
|
next_attempt_at = now(),
|
|
last_error = NULL
|
|
WHERE channel_media_pending_object.workspace_id = $1;
|
|
|
|
-- name: DeleteWorkspaceTasks :exec
|
|
DELETE FROM agent_task_queue
|
|
WHERE agent_id IN (SELECT id FROM agent WHERE agent.workspace_id = $1)
|
|
OR issue_id IN (SELECT id FROM issue WHERE issue.workspace_id = $1)
|
|
OR runtime_id IN (SELECT id FROM agent_runtime WHERE agent_runtime.workspace_id = $1);
|
|
|
|
-- name: DeleteWorkspaceChatMessages :exec
|
|
DELETE FROM chat_message
|
|
WHERE chat_session_id IN (
|
|
SELECT id FROM chat_session WHERE chat_session.workspace_id = $1
|
|
);
|
|
|
|
-- name: DeleteWorkspaceCommunicationRoots :exec
|
|
WITH
|
|
deleted_sessions AS (
|
|
DELETE FROM chat_session WHERE chat_session.workspace_id = $1
|
|
),
|
|
deleted_channel_installations AS (
|
|
DELETE FROM channel_installation
|
|
WHERE channel_installation.workspace_id = $1
|
|
)
|
|
DELETE FROM lark_installation WHERE lark_installation.workspace_id = $1;
|
|
|
|
-- name: DeleteWorkspaceComments :exec
|
|
DELETE FROM comment WHERE comment.workspace_id = $1;
|
|
|
|
-- name: DeleteWorkspaceIssueRoots :exec
|
|
WITH
|
|
deleted_issues AS (
|
|
DELETE FROM issue WHERE issue.workspace_id = $1
|
|
),
|
|
deleted_labels AS (
|
|
DELETE FROM issue_label WHERE issue_label.workspace_id = $1
|
|
),
|
|
deleted_properties AS (
|
|
DELETE FROM issue_property WHERE issue_property.workspace_id = $1
|
|
)
|
|
DELETE FROM quick_action WHERE quick_action.workspace_id = $1;
|
|
|
|
-- name: DeleteWorkspaceAutopilotRuns :exec
|
|
DELETE FROM autopilot_run
|
|
WHERE autopilot_id IN (
|
|
SELECT id FROM autopilot WHERE autopilot.workspace_id = $1
|
|
);
|
|
|
|
-- name: DeleteWorkspaceAutopilotChildren :exec
|
|
WITH
|
|
deleted_triggers AS (
|
|
DELETE FROM autopilot_trigger
|
|
WHERE autopilot_id IN (
|
|
SELECT id FROM autopilot WHERE autopilot.workspace_id = $1
|
|
)
|
|
)
|
|
DELETE FROM autopilot_rule_version
|
|
WHERE autopilot_rule_version.workspace_id = $1;
|
|
|
|
-- name: DeleteWorkspaceAutopilots :exec
|
|
DELETE FROM autopilot WHERE autopilot.workspace_id = $1;
|
|
|
|
-- name: DeleteWorkspacePullRequests :exec
|
|
WITH deleted_github_prs AS (
|
|
DELETE FROM github_pull_request
|
|
WHERE github_pull_request.workspace_id = $1
|
|
)
|
|
DELETE FROM vcs_pull_request WHERE vcs_pull_request.workspace_id = $1;
|
|
|
|
-- name: DeleteWorkspaceConnections :exec
|
|
WITH deleted_github_installations AS (
|
|
DELETE FROM github_installation
|
|
WHERE github_installation.workspace_id = $1
|
|
)
|
|
DELETE FROM vcs_connection WHERE vcs_connection.workspace_id = $1;
|
|
|
|
-- name: DeleteWorkspaceSquadsAndSkills :exec
|
|
WITH deleted_squads AS (
|
|
DELETE FROM squad WHERE squad.workspace_id = $1
|
|
)
|
|
DELETE FROM skill WHERE skill.workspace_id = $1;
|
|
|
|
-- name: DeleteWorkspaceAgents :exec
|
|
DELETE FROM agent WHERE agent.workspace_id = $1;
|
|
|
|
-- name: DeleteWorkspaceRuntimesAndProjects :exec
|
|
WITH
|
|
deleted_runtimes AS (
|
|
DELETE FROM agent_runtime WHERE agent_runtime.workspace_id = $1
|
|
),
|
|
deleted_profiles AS (
|
|
DELETE FROM runtime_profile WHERE runtime_profile.workspace_id = $1
|
|
)
|
|
DELETE FROM project WHERE project.workspace_id = $1;
|
|
|
|
-- name: DeleteWorkspaceAdministration :exec
|
|
WITH
|
|
deleted_members AS (
|
|
DELETE FROM member WHERE member.workspace_id = $1
|
|
),
|
|
deleted_notification_preferences AS (
|
|
DELETE FROM notification_preference
|
|
WHERE notification_preference.workspace_id = $1
|
|
),
|
|
deleted_pins AS (
|
|
DELETE FROM pinned_item WHERE pinned_item.workspace_id = $1
|
|
),
|
|
deleted_daemon_tokens AS (
|
|
DELETE FROM daemon_token WHERE daemon_token.workspace_id = $1
|
|
),
|
|
detached_feedback AS (
|
|
UPDATE feedback
|
|
SET workspace_id = NULL
|
|
WHERE feedback.workspace_id = $1
|
|
),
|
|
detached_client_usage AS (
|
|
UPDATE client_usage_daily
|
|
SET workspace_id = NULL
|
|
WHERE client_usage_daily.workspace_id = $1
|
|
)
|
|
DELETE FROM workspace_invitation
|
|
WHERE workspace_invitation.workspace_id = $1;
|