90 lines
2.6 KiB
SQL
90 lines
2.6 KiB
SQL
-- Take the locks this migration needs before changing anything, retrying
|
|
-- short waits so that concurrent chatd traffic cannot deadlock with it. See
|
|
-- 000609_chat_automations.up.sql for the reasoning. When this runs in the
|
|
-- same transaction as 000609, the locks are already held.
|
|
DO $$
|
|
DECLARE
|
|
previous_lock_timeout text := current_setting('lock_timeout');
|
|
deadline timestamptz := clock_timestamp() + interval '2 minutes';
|
|
BEGIN
|
|
LOOP
|
|
BEGIN
|
|
PERFORM set_config('lock_timeout', '100ms', true);
|
|
DROP VIEW IF EXISTS chats_expanded;
|
|
LOCK TABLE chats IN ACCESS EXCLUSIVE MODE;
|
|
EXIT;
|
|
EXCEPTION WHEN lock_not_available OR deadlock_detected THEN
|
|
IF clock_timestamp() > deadline THEN
|
|
RAISE EXCEPTION 'migration 000611 could not lock chats within 2 minutes';
|
|
END IF;
|
|
END;
|
|
PERFORM pg_sleep(0.1);
|
|
END LOOP;
|
|
PERFORM set_config('lock_timeout', previous_lock_timeout, true);
|
|
END;
|
|
$$;
|
|
|
|
ALTER TABLE chats
|
|
ADD COLUMN manage_automations_enabled boolean NOT NULL DEFAULT false;
|
|
|
|
COMMENT ON COLUMN chats.manage_automations_enabled IS 'Interim per-chat switch that offers the manage_automations tool. Only the chat owner may change it after creation.';
|
|
|
|
-- Recreate chats_expanded, dropped at the top of this migration: its
|
|
-- explicit column list hides new columns otherwise.
|
|
CREATE VIEW chats_expanded AS
|
|
SELECT c.id,
|
|
c.owner_id,
|
|
c.workspace_id,
|
|
c.title,
|
|
c.status,
|
|
c.worker_id,
|
|
c.started_at,
|
|
c.heartbeat_at,
|
|
c.created_at,
|
|
c.updated_at,
|
|
c.parent_chat_id,
|
|
c.root_chat_id,
|
|
c.last_model_config_id,
|
|
c.last_reasoning_effort,
|
|
c.archived,
|
|
c.last_error,
|
|
c.mode,
|
|
c.mcp_server_ids,
|
|
c.labels,
|
|
c.build_id,
|
|
c.agent_id,
|
|
c.pin_order,
|
|
c.last_read_message_id,
|
|
c.dynamic_tools,
|
|
c.organization_id,
|
|
c.project_id,
|
|
c.plan_mode,
|
|
c.client_type,
|
|
c.last_turn_summary,
|
|
c.summary,
|
|
c.summary_generated_at,
|
|
c.snapshot_version,
|
|
c.history_version,
|
|
c.queue_version,
|
|
c.generation_attempt,
|
|
c.retry_state,
|
|
c.retry_state_version,
|
|
c.runner_id,
|
|
c.requires_action_deadline_at,
|
|
COALESCE(root.user_acl, c.user_acl) AS user_acl,
|
|
COALESCE(root.group_acl, c.group_acl) AS group_acl,
|
|
owner.username AS owner_username,
|
|
owner.name AS owner_name,
|
|
c.context_aggregate_hash,
|
|
c.context_dirty_since,
|
|
c.context_dirty_resources,
|
|
c.context_error,
|
|
c.compaction_requested_at,
|
|
c.title_source,
|
|
c.title_updated_at,
|
|
c.automation_id,
|
|
c.manage_automations_enabled
|
|
FROM ((chats c
|
|
LEFT JOIN chats root ON ((root.id = COALESCE(c.root_chat_id, c.parent_chat_id))))
|
|
JOIN visible_users owner ON ((owner.id = c.owner_id)));
|