Files
LWLT-AIBOT/control-plane/migrations/023_leader_summary_webhook_delivery.sql
2026-09-09 17:24:16 +08:00

75 lines
3.3 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Replace AgentBus-routed team-lead summaries with one organization-level
-- external webhook delivery stream. Legacy rows remain for audit/history but
-- cannot be claimed after this migration.
UPDATE leader_task_summary_subscriptions
SET enabled = false,
target_verified_at = NULL,
revision = revision + 1,
updated_at = now()
WHERE enabled = true OR target_verified_at IS NOT NULL;
UPDATE leader_task_summary_deliveries
SET delivery_status = 'cancelled',
last_error = '组长摘要已切换为外部 Webhook,旧 AgentBus 待发记录已取消。',
updated_at = now()
WHERE delivery_status IN ('pending', 'sending', 'failed');
CREATE TABLE IF NOT EXISTS leader_task_summary_webhook_state (
organization_id uuid PRIMARY KEY REFERENCES organizations(id) ON DELETE CASCADE,
enabled boolean NOT NULL DEFAULT false,
starts_at timestamptz NOT NULL DEFAULT now(),
revision integer NOT NULL DEFAULT 0,
configuration_fingerprint text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT leader_task_summary_webhook_state_revision_check
CHECK (revision >= 0),
CONSTRAINT leader_task_summary_webhook_state_fingerprint_check
CHECK (configuration_fingerprint ~ '^[a-f0-9]{64}$')
);
CREATE TABLE IF NOT EXISTS leader_task_summary_webhook_deliveries (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
organization_id uuid NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
webhook_revision integer NOT NULL,
task_id uuid NOT NULL,
source_outbox_event_id bigint NOT NULL,
milestone text NOT NULL,
payload_ciphertext text NOT NULL,
payload_fingerprint text NOT NULL,
delivery_status text NOT NULL DEFAULT 'pending',
attempt_count integer NOT NULL DEFAULT 0,
response_status integer,
last_error text,
accepted_at timestamptz,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT leader_task_summary_webhook_deliveries_milestone_check
CHECK (milestone IN ('final', 'needs_review', 'resolved')),
CONSTRAINT leader_task_summary_webhook_deliveries_status_check
CHECK (delivery_status IN ('pending', 'sending', 'accepted', 'failed', 'uncertain', 'cancelled')),
CONSTRAINT leader_task_summary_webhook_deliveries_attempt_check
CHECK (attempt_count BETWEEN 0 AND 1),
CONSTRAINT leader_task_summary_webhook_deliveries_revision_check
CHECK (webhook_revision >= 0),
CONSTRAINT leader_task_summary_webhook_deliveries_response_status_check
CHECK (response_status IS NULL OR response_status BETWEEN 100 AND 599),
CONSTRAINT leader_task_summary_webhook_deliveries_payload_fingerprint_check
CHECK (payload_fingerprint ~ '^[a-f0-9]{64}$'),
CONSTRAINT leader_task_summary_webhook_deliveries_task_scope_fkey
FOREIGN KEY (organization_id, task_id)
REFERENCES tasks (organization_id, id)
ON DELETE CASCADE,
UNIQUE (organization_id, webhook_revision, task_id, milestone)
);
CREATE INDEX IF NOT EXISTS leader_task_summary_webhook_deliveries_pending_idx
ON leader_task_summary_webhook_deliveries
(organization_id, webhook_revision, delivery_status, created_at, id)
WHERE delivery_status = 'pending';
CREATE INDEX IF NOT EXISTS leader_task_summary_webhook_deliveries_task_idx
ON leader_task_summary_webhook_deliveries
(organization_id, task_id, created_at DESC);