31 lines
1.2 KiB
SQL
31 lines
1.2 KiB
SQL
-- +goose Up
|
|||
|
|
-- Agent 4/10: async AI fallback jobs after FAQ match miss.
|
||
|
|
|
||
|
|
CREATE TABLE IF NOT EXISTS support_auto_jobs (
|
||
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||
|
|
ticket_id UUID NOT NULL REFERENCES support_tickets(id) ON DELETE CASCADE,
|
||
|
|
company_id UUID NOT NULL REFERENCES companies(id) ON DELETE CASCADE,
|
||
|
|
status TEXT NOT NULL DEFAULT 'pending'
|
||
|
|
CHECK (status IN ('pending', 'running', 'done', 'failed')),
|
||
|
|
attempt INT NOT NULL DEFAULT 0,
|
||
|
|
last_error TEXT,
|
||
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||
|
|
);
|
||
|
|
|
||
|
|
CREATE UNIQUE INDEX IF NOT EXISTS support_auto_jobs_ticket_active_uidx
|
||
|
|
ON support_auto_jobs (ticket_id)
|
||
|
|
WHERE status IN ('pending', 'running');
|
||
|
|
|
||
|
|
CREATE INDEX IF NOT EXISTS support_auto_jobs_status_created_idx
|
||
|
|
ON support_auto_jobs (status, created_at ASC)
|
||
|
|
WHERE status = 'pending';
|
||
|
|
|
||
|
|
CREATE INDEX IF NOT EXISTS support_auto_jobs_company_idx
|
||
|
|
ON support_auto_jobs (company_id, created_at DESC);
|
||
|
|
|
||
|
|
-- +goose Down
|
||
|
|
DROP INDEX IF EXISTS support_auto_jobs_company_idx;
|
||
|
|
DROP INDEX IF EXISTS support_auto_jobs_status_created_idx;
|
||
|
|
DROP INDEX IF EXISTS support_auto_jobs_ticket_active_uidx;
|
||
|
|
DROP TABLE IF EXISTS support_auto_jobs;
|