--
-- PostgreSQL database dump
--

\restrict IIs1ywVfui7Zb3f5SRgOBwj2LGJXpFF5jpVnQbpR7cCgjNpjBLehhEAmULW9yma

-- Dumped from database version 17.4 (Debian 17.4-1.pgdg120+2)
-- Dumped by pg_dump version 18.1

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET transaction_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: chat_message_file; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.chat_message_file (
    id integer NOT NULL,
    thread_id character varying(255) NOT NULL,
    checkpoint_id character varying(255) NOT NULL,
    message_index integer NOT NULL,
    file_id integer NOT NULL,
    created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP
);


--
-- Name: chat_message_file_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.chat_message_file_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: chat_message_file_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.chat_message_file_id_seq OWNED BY public.chat_message_file.id;


--
-- Name: chat_messages; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.chat_messages (
    id integer NOT NULL,
    thread_id character varying(255) NOT NULL,
    checkpoint_id character varying(255) NOT NULL,
    message_index integer NOT NULL,
    role character varying(20) NOT NULL,
    content text NOT NULL,
    injected_content text,
    has_files boolean DEFAULT false,
    metadata jsonb,
    created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    name character varying(255)
);


--
-- Name: TABLE chat_messages; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON TABLE public.chat_messages IS '聊天消息表，存储用户原始消息和AI响应的关键信息';


--
-- Name: COLUMN chat_messages.thread_id; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_messages.thread_id IS '会话线程ID';


--
-- Name: COLUMN chat_messages.checkpoint_id; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_messages.checkpoint_id IS '关联的checkpoint ID';


--
-- Name: COLUMN chat_messages.message_index; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_messages.message_index IS '消息在checkpoint中的索引（从0开始）';


--
-- Name: COLUMN chat_messages.role; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_messages.role IS '消息角色：user、assistant、system、tool';


--
-- Name: COLUMN chat_messages.content; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_messages.content IS '用户的原始问题或AI的响应';


--
-- Name: COLUMN chat_messages.injected_content; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_messages.injected_content IS '注入给AI的完整内容（包含文件内容）';


--
-- Name: COLUMN chat_messages.has_files; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_messages.has_files IS '是否关联了文件';


--
-- Name: COLUMN chat_messages.metadata; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_messages.metadata IS '额外信息：token、模型、推理内容等';


--
-- Name: COLUMN chat_messages.name; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_messages.name IS '工具消息时的工具名称（如 internet_search、text_to_image）';


--
-- Name: chat_messages_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.chat_messages_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: chat_messages_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.chat_messages_id_seq OWNED BY public.chat_messages.id;


--
-- Name: chat_thread_chunk; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.chat_thread_chunk (
    id integer NOT NULL,
    file_id integer NOT NULL,
    thread_id character varying(255) NOT NULL,
    chunk_index integer NOT NULL,
    content text NOT NULL,
    metadata jsonb,
    vector_id character varying(255),
    created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    summary text
);


--
-- Name: chat_thread_chunk_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.chat_thread_chunk_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: chat_thread_chunk_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.chat_thread_chunk_id_seq OWNED BY public.chat_thread_chunk.id;


--
-- Name: chat_thread_file; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.chat_thread_file (
    id integer NOT NULL,
    thread_id character varying(255) NOT NULL,
    user_id integer NOT NULL,
    file_name character varying(255) NOT NULL,
    file_path character varying(500) NOT NULL,
    file_size integer DEFAULT 0,
    file_type character varying(50) DEFAULT 'pdf'::character varying,
    status character varying(20) DEFAULT 'processing'::character varying,
    chunk_count integer DEFAULT 0,
    created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    is_deleted boolean DEFAULT false,
    deleted_at timestamp with time zone,
    progress_percent smallint DEFAULT 0 NOT NULL,
    processing_stage character varying(50),
    CONSTRAINT ck_chat_thread_file_progress_percent CHECK (((progress_percent >= 0) AND (progress_percent <= 100)))
);


--
-- Name: COLUMN chat_thread_file.progress_percent; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_thread_file.progress_percent IS '处理进度 0-100';


--
-- Name: COLUMN chat_thread_file.processing_stage; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_thread_file.processing_stage IS 'processing 阶段';


--
-- Name: chat_thread_file_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.chat_thread_file_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: chat_thread_file_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.chat_thread_file_id_seq OWNED BY public.chat_thread_file.id;


--
-- Name: chat_threads; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.chat_threads (
    id integer NOT NULL,
    thread_id character varying(255) NOT NULL,
    user_id integer NOT NULL,
    title character varying(50) NOT NULL,
    first_query text NOT NULL,
    created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    message_count integer DEFAULT 1,
    is_deleted boolean DEFAULT false,
    knowledge_base_id integer,
    novel_graph_id integer,
    knowledge_graph_id integer,
    ip character varying(128)
);


--
-- Name: TABLE chat_threads; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON TABLE public.chat_threads IS '聊天会话记录表，记录每个用户的会话基本信息';


--
-- Name: COLUMN chat_threads.id; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_threads.id IS '主键 ID';


--
-- Name: COLUMN chat_threads.thread_id; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_threads.thread_id IS '会话线程 ID（UUID 格式）';


--
-- Name: COLUMN chat_threads.user_id; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_threads.user_id IS '用户 ID，关联 user_list 表';


--
-- Name: COLUMN chat_threads.title; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_threads.title IS '会话标题（首次请求内容的前10个字）';


--
-- Name: COLUMN chat_threads.first_query; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_threads.first_query IS '首次请求的完整内容';


--
-- Name: COLUMN chat_threads.created_at; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_threads.created_at IS '会话创建时间';


--
-- Name: COLUMN chat_threads.updated_at; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_threads.updated_at IS '最后更新时间';


--
-- Name: COLUMN chat_threads.message_count; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_threads.message_count IS '该会话的消息总数';


--
-- Name: COLUMN chat_threads.is_deleted; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_threads.is_deleted IS '是否已删除（软删除标记）';


--
-- Name: COLUMN chat_threads.knowledge_graph_id; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_threads.knowledge_graph_id IS '绑定的知识图谱 graphs.id，与 knowledge_base_id 二选一';


--
-- Name: COLUMN chat_threads.ip; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.chat_threads.ip IS '最近一次发起聊天时的客户端 IP（可选）';


--
-- Name: chat_threads_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.chat_threads_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: chat_threads_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.chat_threads_id_seq OWNED BY public.chat_threads.id;


--
-- Name: checkpoint_blobs; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.checkpoint_blobs (
    thread_id text NOT NULL,
    checkpoint_ns text DEFAULT ''::text NOT NULL,
    channel text NOT NULL,
    version text NOT NULL,
    type text NOT NULL,
    blob bytea
);


--
-- Name: checkpoint_migrations; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.checkpoint_migrations (
    v integer NOT NULL
);


--
-- Name: checkpoint_writes; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.checkpoint_writes (
    thread_id text NOT NULL,
    checkpoint_ns text DEFAULT ''::text NOT NULL,
    checkpoint_id text NOT NULL,
    task_id text NOT NULL,
    idx integer NOT NULL,
    channel text NOT NULL,
    type text,
    blob bytea NOT NULL,
    task_path text DEFAULT ''::text NOT NULL
);


--
-- Name: checkpoints; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.checkpoints (
    thread_id text NOT NULL,
    checkpoint_ns text DEFAULT ''::text NOT NULL,
    checkpoint_id text NOT NULL,
    parent_checkpoint_id text,
    type text,
    checkpoint jsonb NOT NULL,
    metadata jsonb DEFAULT '{}'::jsonb NOT NULL
);


--
-- Name: department; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.department (
    id integer NOT NULL,
    enterprise_id integer NOT NULL,
    name character varying(255) NOT NULL,
    parent_id integer,
    created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
    updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
    leader_user_id integer
);


--
-- Name: TABLE department; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON TABLE public.department IS '部门';


--
-- Name: COLUMN department.leader_user_id; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.department.leader_user_id IS '部门负责人 user_id，对应 role=leader 的用户';


--
-- Name: department_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.department_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: department_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.department_id_seq OWNED BY public.department.id;


--
-- Name: enterprise; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.enterprise (
    id integer NOT NULL,
    name character varying(255) NOT NULL,
    code character varying(64),
    created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
    updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
    ai_display_name character varying(128) DEFAULT '智能助手 AI'::character varying NOT NULL
);


--
-- Name: TABLE enterprise; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON TABLE public.enterprise IS '企业（单租户部署通常仅一条记录）';


--
-- Name: COLUMN enterprise.ai_display_name; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.enterprise.ai_display_name IS 'AI 助手对外展示名称（写入系统提示词等），管理端可修改';


--
-- Name: enterprise_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.enterprise_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: enterprise_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.enterprise_id_seq OWNED BY public.enterprise.id;


--
-- Name: graphs; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.graphs (
    id integer NOT NULL,
    user_id integer NOT NULL,
    name character varying(255) NOT NULL,
    description text,
    csv_file_name character varying(255),
    node_count integer DEFAULT 0,
    edge_count integer DEFAULT 0,
    neo4j_graph_id character varying(100) NOT NULL,
    graph_type character varying(20) DEFAULT 'knowledge'::character varying NOT NULL,
    build_status character varying(20),
    build_error text,
    rag_chunk_count integer DEFAULT 0 NOT NULL,
    created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    enterprise_id integer NOT NULL,
    department_id integer,
    creator_id integer NOT NULL,
    visibility character varying(32) DEFAULT 'private'::character varying NOT NULL,
    progress_percent smallint DEFAULT 0 NOT NULL,
    processing_stage character varying(50),
    CONSTRAINT ck_graphs_progress_percent CHECK (((progress_percent >= 0) AND (progress_percent <= 100))),
    CONSTRAINT ck_graphs_visibility CHECK (((visibility)::text = ANY ((ARRAY['private'::character varying, 'department'::character varying, 'enterprise'::character varying])::text[])))
);


--
-- Name: TABLE graphs; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON TABLE public.graphs IS '知识图谱元数据表，图数据在 Neo4j；向量块数量见 rag_chunk_count';


--
-- Name: COLUMN graphs.neo4j_graph_id; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.graphs.neo4j_graph_id IS 'Neo4j 中图谱唯一标识';


--
-- Name: COLUMN graphs.graph_type; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.graphs.graph_type IS '兼容字段，默认 knowledge';


--
-- Name: COLUMN graphs.build_status; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.graphs.build_status IS '构建状态：pending/processing/completed/failed';


--
-- Name: COLUMN graphs.build_error; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.graphs.build_error IS '构建失败时的错误信息';


--
-- Name: COLUMN graphs.rag_chunk_count; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.graphs.rag_chunk_count IS 'Chroma 中知识图谱 RAG 分块数量';


--
-- Name: COLUMN graphs.creator_id; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.graphs.creator_id IS '创建者（与 user_id 通常一致，用于权限判断）';


--
-- Name: COLUMN graphs.visibility; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.graphs.visibility IS 'private | department | enterprise';


--
-- Name: COLUMN graphs.progress_percent; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.graphs.progress_percent IS '构建进度 0-100';


--
-- Name: COLUMN graphs.processing_stage; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.graphs.processing_stage IS '构建阶段: extracting/indexing/completed 等';


--
-- Name: graphs_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.graphs_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: graphs_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.graphs_id_seq OWNED BY public.graphs.id;


--
-- Name: kb_audit_log; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.kb_audit_log (
    id integer NOT NULL,
    enterprise_id integer NOT NULL,
    actor_id integer NOT NULL,
    target_user_id integer,
    department_id integer,
    kb_id integer,
    file_id integer,
    action character varying(50) NOT NULL,
    ip character varying(128),
    user_agent text,
    metadata jsonb,
    created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL
);


--
-- Name: TABLE kb_audit_log; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON TABLE public.kb_audit_log IS '知识库操作审计日志';


--
-- Name: COLUMN kb_audit_log.action; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.kb_audit_log.action IS 'upload | download | delete | archive | create_kb | delete_kb | permission_change';


--
-- Name: kb_audit_log_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.kb_audit_log_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: kb_audit_log_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.kb_audit_log_id_seq OWNED BY public.kb_audit_log.id;


--
-- Name: knowledge_base; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.knowledge_base (
    id integer NOT NULL,
    user_id integer NOT NULL,
    name character varying(255) NOT NULL,
    description text,
    created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    is_deleted boolean DEFAULT false,
    deleted_at timestamp with time zone,
    enterprise_id integer DEFAULT 1 NOT NULL,
    department_id integer,
    creator_id integer,
    visibility character varying(32) DEFAULT 'private'::character varying NOT NULL,
    CONSTRAINT ck_knowledge_base_visibility CHECK (((visibility)::text = ANY ((ARRAY['private'::character varying, 'department'::character varying, 'enterprise'::character varying])::text[])))
);


--
-- Name: COLUMN knowledge_base.creator_id; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.knowledge_base.creator_id IS '创建者（与 user_id 通常一致，用于权限判断）';


--
-- Name: COLUMN knowledge_base.visibility; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.knowledge_base.visibility IS 'private | department | enterprise';


--
-- Name: knowledge_base_chunk; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.knowledge_base_chunk (
    id integer NOT NULL,
    file_id integer NOT NULL,
    knowledge_base_id integer NOT NULL,
    chunk_index integer NOT NULL,
    content text NOT NULL,
    metadata jsonb,
    vector_id character varying(255),
    created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    summary text
);


--
-- Name: knowledge_base_chunk_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.knowledge_base_chunk_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: knowledge_base_chunk_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.knowledge_base_chunk_id_seq OWNED BY public.knowledge_base_chunk.id;


--
-- Name: knowledge_base_file; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.knowledge_base_file (
    id integer NOT NULL,
    knowledge_base_id integer NOT NULL,
    user_id integer NOT NULL,
    file_name character varying(255) NOT NULL,
    file_path character varying(500) NOT NULL,
    file_size bigint NOT NULL,
    file_type character varying(50) DEFAULT 'pdf'::character varying NOT NULL,
    status character varying(20) DEFAULT 'processing'::character varying NOT NULL,
    chunk_count integer DEFAULT 0,
    created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    is_deleted boolean DEFAULT false,
    deleted_at timestamp with time zone,
    progress_percent smallint DEFAULT 0 NOT NULL,
    processing_stage character varying(50),
    CONSTRAINT ck_kb_file_progress_percent CHECK (((progress_percent >= 0) AND (progress_percent <= 100)))
);


--
-- Name: COLUMN knowledge_base_file.progress_percent; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.knowledge_base_file.progress_percent IS '处理进度 0-100';


--
-- Name: COLUMN knowledge_base_file.processing_stage; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.knowledge_base_file.processing_stage IS 'processing 阶段: uploading/parsing/ocr/embedding/summarizing/completed 等';


--
-- Name: knowledge_base_file_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.knowledge_base_file_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: knowledge_base_file_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.knowledge_base_file_id_seq OWNED BY public.knowledge_base_file.id;


--
-- Name: knowledge_base_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.knowledge_base_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: knowledge_base_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.knowledge_base_id_seq OWNED BY public.knowledge_base.id;


--
-- Name: knowledge_processing_task; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.knowledge_processing_task (
    id integer NOT NULL,
    user_id integer NOT NULL,
    knowledge_base_id integer NOT NULL,
    task_name character varying(255) NOT NULL,
    instruction text NOT NULL,
    file_ids integer[] NOT NULL,
    task_type character varying(50) NOT NULL,
    status character varying(20) DEFAULT 'pending'::character varying,
    result text,
    result_file_url text,
    error_message text,
    created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    started_at timestamp with time zone,
    completed_at timestamp with time zone,
    progress_percent smallint DEFAULT 0 NOT NULL,
    processing_stage character varying(50),
    CONSTRAINT ck_kb_processing_task_progress_percent CHECK (((progress_percent >= 0) AND (progress_percent <= 100)))
);


--
-- Name: TABLE knowledge_processing_task; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON TABLE public.knowledge_processing_task IS '知识加工任务表：合并、对比、总结等异步任务';


--
-- Name: COLUMN knowledge_processing_task.result_file_url; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.knowledge_processing_task.result_file_url IS '加工结果文件的 OSS 下载链接';


--
-- Name: COLUMN knowledge_processing_task.progress_percent; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.knowledge_processing_task.progress_percent IS '任务进度 0-100';


--
-- Name: COLUMN knowledge_processing_task.processing_stage; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.knowledge_processing_task.processing_stage IS '任务阶段';


--
-- Name: knowledge_processing_task_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.knowledge_processing_task_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: knowledge_processing_task_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.knowledge_processing_task_id_seq OWNED BY public.knowledge_processing_task.id;


--
-- Name: user_list; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.user_list (
    id integer NOT NULL,
    username character varying(50) NOT NULL,
    email character varying(255) NOT NULL,
    phone character varying(255) NOT NULL,
    github_id character varying(100),
    github_username character varying(100),
    github_avatar_url text,
    github_access_token text,
    github_token_expires_at timestamp with time zone,
    display_name character varying(100),
    avatar_url text,
    bio text,
    is_active boolean DEFAULT true,
    email_verified boolean DEFAULT false,
    created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    last_login_at timestamp with time zone,
    hashed_password character varying(255),
    is_search boolean DEFAULT false,
    is_reasoner boolean DEFAULT false,
    enterprise_id integer DEFAULT 1 NOT NULL,
    department_id integer,
    role character varying(32) DEFAULT 'employee'::character varying NOT NULL,
    is_first_login boolean DEFAULT true NOT NULL,
    allow_kb_upload boolean DEFAULT true NOT NULL
);


--
-- Name: COLUMN user_list.role; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.user_list.role IS 'admin | leader | employee';


--
-- Name: COLUMN user_list.is_first_login; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.user_list.is_first_login IS '首次登录可强制改密（可选业务）';


--
-- Name: COLUMN user_list.allow_kb_upload; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.user_list.allow_kb_upload IS '是否允许上传文件到知识库（上级领导或 admin 可关闭）';


--
-- Name: user_list_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.user_list_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: user_list_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.user_list_id_seq OWNED BY public.user_list.id;


--
-- Name: chat_message_file id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_message_file ALTER COLUMN id SET DEFAULT nextval('public.chat_message_file_id_seq'::regclass);


--
-- Name: chat_messages id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_messages ALTER COLUMN id SET DEFAULT nextval('public.chat_messages_id_seq'::regclass);


--
-- Name: chat_thread_chunk id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_thread_chunk ALTER COLUMN id SET DEFAULT nextval('public.chat_thread_chunk_id_seq'::regclass);


--
-- Name: chat_thread_file id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_thread_file ALTER COLUMN id SET DEFAULT nextval('public.chat_thread_file_id_seq'::regclass);


--
-- Name: chat_threads id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_threads ALTER COLUMN id SET DEFAULT nextval('public.chat_threads_id_seq'::regclass);


--
-- Name: department id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.department ALTER COLUMN id SET DEFAULT nextval('public.department_id_seq'::regclass);


--
-- Name: enterprise id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.enterprise ALTER COLUMN id SET DEFAULT nextval('public.enterprise_id_seq'::regclass);


--
-- Name: graphs id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.graphs ALTER COLUMN id SET DEFAULT nextval('public.graphs_id_seq'::regclass);


--
-- Name: kb_audit_log id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.kb_audit_log ALTER COLUMN id SET DEFAULT nextval('public.kb_audit_log_id_seq'::regclass);


--
-- Name: knowledge_base id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.knowledge_base ALTER COLUMN id SET DEFAULT nextval('public.knowledge_base_id_seq'::regclass);


--
-- Name: knowledge_base_chunk id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.knowledge_base_chunk ALTER COLUMN id SET DEFAULT nextval('public.knowledge_base_chunk_id_seq'::regclass);


--
-- Name: knowledge_base_file id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.knowledge_base_file ALTER COLUMN id SET DEFAULT nextval('public.knowledge_base_file_id_seq'::regclass);


--
-- Name: knowledge_processing_task id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.knowledge_processing_task ALTER COLUMN id SET DEFAULT nextval('public.knowledge_processing_task_id_seq'::regclass);


--
-- Name: user_list id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.user_list ALTER COLUMN id SET DEFAULT nextval('public.user_list_id_seq'::regclass);


--
-- Name: chat_message_file chat_message_file_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_message_file
    ADD CONSTRAINT chat_message_file_pkey PRIMARY KEY (id);


--
-- Name: chat_messages chat_messages_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_messages
    ADD CONSTRAINT chat_messages_pkey PRIMARY KEY (id);


--
-- Name: chat_thread_chunk chat_thread_chunk_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_thread_chunk
    ADD CONSTRAINT chat_thread_chunk_pkey PRIMARY KEY (id);


--
-- Name: chat_thread_file chat_thread_file_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_thread_file
    ADD CONSTRAINT chat_thread_file_pkey PRIMARY KEY (id);


--
-- Name: chat_threads chat_threads_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_threads
    ADD CONSTRAINT chat_threads_pkey PRIMARY KEY (id);


--
-- Name: checkpoint_blobs checkpoint_blobs_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.checkpoint_blobs
    ADD CONSTRAINT checkpoint_blobs_pkey PRIMARY KEY (thread_id, checkpoint_ns, channel, version);


--
-- Name: checkpoint_migrations checkpoint_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.checkpoint_migrations
    ADD CONSTRAINT checkpoint_migrations_pkey PRIMARY KEY (v);


--
-- Name: checkpoint_writes checkpoint_writes_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.checkpoint_writes
    ADD CONSTRAINT checkpoint_writes_pkey PRIMARY KEY (thread_id, checkpoint_ns, checkpoint_id, task_id, idx);


--
-- Name: checkpoints checkpoints_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.checkpoints
    ADD CONSTRAINT checkpoints_pkey PRIMARY KEY (thread_id, checkpoint_ns, checkpoint_id);


--
-- Name: department department_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.department
    ADD CONSTRAINT department_pkey PRIMARY KEY (id);


--
-- Name: enterprise enterprise_code_key; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.enterprise
    ADD CONSTRAINT enterprise_code_key UNIQUE (code);


--
-- Name: enterprise enterprise_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.enterprise
    ADD CONSTRAINT enterprise_pkey PRIMARY KEY (id);


--
-- Name: graphs graphs_neo4j_graph_id_key; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.graphs
    ADD CONSTRAINT graphs_neo4j_graph_id_key UNIQUE (neo4j_graph_id);


--
-- Name: graphs graphs_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.graphs
    ADD CONSTRAINT graphs_pkey PRIMARY KEY (id);


--
-- Name: kb_audit_log kb_audit_log_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.kb_audit_log
    ADD CONSTRAINT kb_audit_log_pkey PRIMARY KEY (id);


--
-- Name: knowledge_base_chunk knowledge_base_chunk_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.knowledge_base_chunk
    ADD CONSTRAINT knowledge_base_chunk_pkey PRIMARY KEY (id);


--
-- Name: knowledge_base_file knowledge_base_file_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.knowledge_base_file
    ADD CONSTRAINT knowledge_base_file_pkey PRIMARY KEY (id);


--
-- Name: knowledge_base knowledge_base_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.knowledge_base
    ADD CONSTRAINT knowledge_base_pkey PRIMARY KEY (id);


--
-- Name: knowledge_processing_task knowledge_processing_task_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.knowledge_processing_task
    ADD CONSTRAINT knowledge_processing_task_pkey PRIMARY KEY (id);


--
-- Name: chat_messages uk_checkpoint_message; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_messages
    ADD CONSTRAINT uk_checkpoint_message UNIQUE (checkpoint_id, message_index);


--
-- Name: chat_message_file uk_message_file; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_message_file
    ADD CONSTRAINT uk_message_file UNIQUE (checkpoint_id, message_index, file_id);


--
-- Name: chat_threads uk_thread_id; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_threads
    ADD CONSTRAINT uk_thread_id UNIQUE (thread_id);


--
-- Name: user_list unique_email; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.user_list
    ADD CONSTRAINT unique_email UNIQUE (email);


--
-- Name: user_list unique_github_id; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.user_list
    ADD CONSTRAINT unique_github_id UNIQUE (github_id);


--
-- Name: department uq_department_enterprise_name; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.department
    ADD CONSTRAINT uq_department_enterprise_name UNIQUE (enterprise_id, name);


--
-- Name: user_list user_list_phone_key; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.user_list
    ADD CONSTRAINT user_list_phone_key UNIQUE (phone);


--
-- Name: user_list user_list_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.user_list
    ADD CONSTRAINT user_list_pkey PRIMARY KEY (id);


--
-- Name: user_list user_list_username_key; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.user_list
    ADD CONSTRAINT user_list_username_key UNIQUE (username);


--
-- Name: checkpoint_blobs_thread_id_idx; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX checkpoint_blobs_thread_id_idx ON public.checkpoint_blobs USING btree (thread_id);


--
-- Name: checkpoint_writes_thread_id_idx; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX checkpoint_writes_thread_id_idx ON public.checkpoint_writes USING btree (thread_id);


--
-- Name: checkpoints_thread_id_idx; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX checkpoints_thread_id_idx ON public.checkpoints USING btree (thread_id);


--
-- Name: idx_chat_message_file_checkpoint; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_message_file_checkpoint ON public.chat_message_file USING btree (checkpoint_id, message_index);


--
-- Name: idx_chat_message_file_file_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_message_file_file_id ON public.chat_message_file USING btree (file_id);


--
-- Name: idx_chat_message_file_thread_checkpoint; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_message_file_thread_checkpoint ON public.chat_message_file USING btree (thread_id, checkpoint_id);


--
-- Name: idx_chat_message_file_thread_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_message_file_thread_id ON public.chat_message_file USING btree (thread_id);


--
-- Name: idx_chat_messages_checkpoint_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_messages_checkpoint_id ON public.chat_messages USING btree (checkpoint_id);


--
-- Name: idx_chat_messages_content_search; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_messages_content_search ON public.chat_messages USING gin (to_tsvector('simple'::regconfig, content));


--
-- Name: idx_chat_messages_has_files; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_messages_has_files ON public.chat_messages USING btree (has_files);


--
-- Name: idx_chat_messages_metadata; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_messages_metadata ON public.chat_messages USING gin (metadata);


--
-- Name: idx_chat_messages_role; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_messages_role ON public.chat_messages USING btree (role);


--
-- Name: idx_chat_messages_thread_created; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_messages_thread_created ON public.chat_messages USING btree (thread_id, created_at DESC);


--
-- Name: idx_chat_messages_thread_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_messages_thread_id ON public.chat_messages USING btree (thread_id);


--
-- Name: idx_chat_thread_chunk_created_at; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_thread_chunk_created_at ON public.chat_thread_chunk USING btree (created_at);


--
-- Name: idx_chat_thread_chunk_file_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_thread_chunk_file_id ON public.chat_thread_chunk USING btree (file_id);


--
-- Name: idx_chat_thread_chunk_file_thread; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_thread_chunk_file_thread ON public.chat_thread_chunk USING btree (file_id, thread_id);


--
-- Name: idx_chat_thread_chunk_thread_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_thread_chunk_thread_id ON public.chat_thread_chunk USING btree (thread_id);


--
-- Name: idx_chat_thread_chunk_vector_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_thread_chunk_vector_id ON public.chat_thread_chunk USING btree (vector_id);


--
-- Name: idx_chat_thread_file_created_at; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_thread_file_created_at ON public.chat_thread_file USING btree (created_at);


--
-- Name: idx_chat_thread_file_is_deleted; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_thread_file_is_deleted ON public.chat_thread_file USING btree (is_deleted);


--
-- Name: idx_chat_thread_file_status; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_thread_file_status ON public.chat_thread_file USING btree (status);


--
-- Name: idx_chat_thread_file_thread_deleted; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_thread_file_thread_deleted ON public.chat_thread_file USING btree (thread_id, is_deleted);


--
-- Name: idx_chat_thread_file_thread_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_thread_file_thread_id ON public.chat_thread_file USING btree (thread_id);


--
-- Name: idx_chat_thread_file_thread_user; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_thread_file_thread_user ON public.chat_thread_file USING btree (thread_id, user_id);


--
-- Name: idx_chat_thread_file_user_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_thread_file_user_id ON public.chat_thread_file USING btree (user_id);


--
-- Name: idx_chat_threads_created_at; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_threads_created_at ON public.chat_threads USING btree (created_at DESC);


--
-- Name: idx_chat_threads_knowledge_graph_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_threads_knowledge_graph_id ON public.chat_threads USING btree (knowledge_graph_id);


--
-- Name: idx_chat_threads_novel_graph_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_threads_novel_graph_id ON public.chat_threads USING btree (novel_graph_id);


--
-- Name: idx_chat_threads_user_created; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_threads_user_created ON public.chat_threads USING btree (user_id, created_at DESC);


--
-- Name: idx_chat_threads_user_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_chat_threads_user_id ON public.chat_threads USING btree (user_id);


--
-- Name: idx_department_enterprise_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_department_enterprise_id ON public.department USING btree (enterprise_id);


--
-- Name: idx_department_leader_user_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_department_leader_user_id ON public.department USING btree (leader_user_id);


--
-- Name: idx_graphs_created_at; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_graphs_created_at ON public.graphs USING btree (created_at DESC);


--
-- Name: idx_graphs_creator; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_graphs_creator ON public.graphs USING btree (creator_id);


--
-- Name: idx_graphs_ent_vis; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_graphs_ent_vis ON public.graphs USING btree (enterprise_id, visibility);


--
-- Name: idx_graphs_enterprise; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_graphs_enterprise ON public.graphs USING btree (enterprise_id);


--
-- Name: idx_graphs_graph_type; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_graphs_graph_type ON public.graphs USING btree (user_id, graph_type);


--
-- Name: idx_graphs_neo4j_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_graphs_neo4j_id ON public.graphs USING btree (neo4j_graph_id);


--
-- Name: idx_graphs_user_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_graphs_user_id ON public.graphs USING btree (user_id);


--
-- Name: idx_kb_audit_log_action; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_audit_log_action ON public.kb_audit_log USING btree (action);


--
-- Name: idx_kb_audit_log_actor_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_audit_log_actor_id ON public.kb_audit_log USING btree (actor_id);


--
-- Name: idx_kb_audit_log_created_at; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_audit_log_created_at ON public.kb_audit_log USING btree (created_at DESC);


--
-- Name: idx_kb_audit_log_department_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_audit_log_department_id ON public.kb_audit_log USING btree (department_id);


--
-- Name: idx_kb_audit_log_ent_dept_created; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_audit_log_ent_dept_created ON public.kb_audit_log USING btree (enterprise_id, department_id, created_at DESC);


--
-- Name: idx_kb_audit_log_enterprise_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_audit_log_enterprise_id ON public.kb_audit_log USING btree (enterprise_id);


--
-- Name: idx_kb_audit_log_kb_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_audit_log_kb_id ON public.kb_audit_log USING btree (kb_id);


--
-- Name: idx_kb_audit_log_target_user_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_audit_log_target_user_id ON public.kb_audit_log USING btree (target_user_id);


--
-- Name: idx_kb_chunk_file_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_chunk_file_id ON public.knowledge_base_chunk USING btree (file_id);


--
-- Name: idx_kb_chunk_kb_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_chunk_kb_id ON public.knowledge_base_chunk USING btree (knowledge_base_id);


--
-- Name: idx_kb_chunk_metadata; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_chunk_metadata ON public.knowledge_base_chunk USING gin (metadata);


--
-- Name: idx_kb_chunk_vector_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_chunk_vector_id ON public.knowledge_base_chunk USING btree (vector_id);


--
-- Name: idx_kb_file_created_at; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_file_created_at ON public.knowledge_base_file USING btree (created_at);


--
-- Name: idx_kb_file_kb_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_file_kb_id ON public.knowledge_base_file USING btree (knowledge_base_id);


--
-- Name: idx_kb_file_status; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_file_status ON public.knowledge_base_file USING btree (status);


--
-- Name: idx_kb_file_unique_active; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX idx_kb_file_unique_active ON public.knowledge_base_file USING btree (knowledge_base_id, file_name) WHERE (is_deleted = false);


--
-- Name: idx_kb_file_user_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_file_user_id ON public.knowledge_base_file USING btree (user_id);


--
-- Name: idx_kb_processing_created_at; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_processing_created_at ON public.knowledge_processing_task USING btree (created_at DESC);


--
-- Name: idx_kb_processing_kb_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_processing_kb_id ON public.knowledge_processing_task USING btree (knowledge_base_id);


--
-- Name: idx_kb_processing_status; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_processing_status ON public.knowledge_processing_task USING btree (status);


--
-- Name: idx_kb_processing_user_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_processing_user_id ON public.knowledge_processing_task USING btree (user_id);


--
-- Name: idx_kb_processing_user_status; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_kb_processing_user_status ON public.knowledge_processing_task USING btree (user_id, status);


--
-- Name: idx_knowledge_base_created_at; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_knowledge_base_created_at ON public.knowledge_base USING btree (created_at);


--
-- Name: idx_knowledge_base_creator; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_knowledge_base_creator ON public.knowledge_base USING btree (creator_id);


--
-- Name: idx_knowledge_base_ent_vis; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_knowledge_base_ent_vis ON public.knowledge_base USING btree (enterprise_id, visibility) WHERE (is_deleted = false);


--
-- Name: idx_knowledge_base_enterprise; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_knowledge_base_enterprise ON public.knowledge_base USING btree (enterprise_id);


--
-- Name: idx_knowledge_base_is_deleted; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_knowledge_base_is_deleted ON public.knowledge_base USING btree (is_deleted);


--
-- Name: idx_knowledge_base_user_deleted; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_knowledge_base_user_deleted ON public.knowledge_base USING btree (user_id, is_deleted);


--
-- Name: idx_knowledge_base_user_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_knowledge_base_user_id ON public.knowledge_base USING btree (user_id);


--
-- Name: idx_knowledge_base_user_name; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_knowledge_base_user_name ON public.knowledge_base USING btree (user_id, name);


--
-- Name: idx_user_list_created_at; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_user_list_created_at ON public.user_list USING btree (created_at);


--
-- Name: idx_user_list_department_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_user_list_department_id ON public.user_list USING btree (department_id);


--
-- Name: idx_user_list_email; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_user_list_email ON public.user_list USING btree (email);


--
-- Name: idx_user_list_enterprise_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_user_list_enterprise_id ON public.user_list USING btree (enterprise_id);


--
-- Name: idx_user_list_github_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_user_list_github_id ON public.user_list USING btree (github_id);


--
-- Name: idx_user_list_role; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_user_list_role ON public.user_list USING btree (role);


--
-- Name: idx_user_list_username; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_user_list_username ON public.user_list USING btree (username);


--
-- Name: uk_chat_thread_file_thread_name_active; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX uk_chat_thread_file_thread_name_active ON public.chat_thread_file USING btree (thread_id, file_name) WHERE (is_deleted = false);


--
-- Name: uk_user_knowledge_base_name_active; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX uk_user_knowledge_base_name_active ON public.knowledge_base USING btree (user_id, name) WHERE (is_deleted = false);


--
-- Name: department department_enterprise_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.department
    ADD CONSTRAINT department_enterprise_id_fkey FOREIGN KEY (enterprise_id) REFERENCES public.enterprise(id) ON DELETE CASCADE;


--
-- Name: department department_leader_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.department
    ADD CONSTRAINT department_leader_user_id_fkey FOREIGN KEY (leader_user_id) REFERENCES public.user_list(id) ON DELETE SET NULL;


--
-- Name: department department_parent_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.department
    ADD CONSTRAINT department_parent_id_fkey FOREIGN KEY (parent_id) REFERENCES public.department(id) ON DELETE SET NULL;


--
-- Name: chat_message_file fk_chat_message_file_file; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_message_file
    ADD CONSTRAINT fk_chat_message_file_file FOREIGN KEY (file_id) REFERENCES public.chat_thread_file(id) ON DELETE CASCADE;


--
-- Name: chat_thread_chunk fk_chat_thread_chunk_file; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_thread_chunk
    ADD CONSTRAINT fk_chat_thread_chunk_file FOREIGN KEY (file_id) REFERENCES public.chat_thread_file(id) ON DELETE CASCADE;


--
-- Name: chat_thread_file fk_chat_thread_file_user; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_thread_file
    ADD CONSTRAINT fk_chat_thread_file_user FOREIGN KEY (user_id) REFERENCES public.user_list(id) ON DELETE CASCADE;


--
-- Name: chat_threads fk_chat_threads_knowledge_graph; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_threads
    ADD CONSTRAINT fk_chat_threads_knowledge_graph FOREIGN KEY (knowledge_graph_id) REFERENCES public.graphs(id) ON DELETE SET NULL;


--
-- Name: graphs fk_graphs_user; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.graphs
    ADD CONSTRAINT fk_graphs_user FOREIGN KEY (user_id) REFERENCES public.user_list(id) ON DELETE CASCADE;


--
-- Name: knowledge_base_chunk fk_kb; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.knowledge_base_chunk
    ADD CONSTRAINT fk_kb FOREIGN KEY (knowledge_base_id) REFERENCES public.knowledge_base(id) ON DELETE CASCADE;


--
-- Name: knowledge_base_chunk fk_kb_file; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.knowledge_base_chunk
    ADD CONSTRAINT fk_kb_file FOREIGN KEY (file_id) REFERENCES public.knowledge_base_file(id) ON DELETE CASCADE;


--
-- Name: knowledge_processing_task fk_kb_processing_kb; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.knowledge_processing_task
    ADD CONSTRAINT fk_kb_processing_kb FOREIGN KEY (knowledge_base_id) REFERENCES public.knowledge_base(id) ON DELETE CASCADE;


--
-- Name: knowledge_processing_task fk_kb_processing_user; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.knowledge_processing_task
    ADD CONSTRAINT fk_kb_processing_user FOREIGN KEY (user_id) REFERENCES public.user_list(id) ON DELETE CASCADE;


--
-- Name: knowledge_base_file fk_knowledge_base; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.knowledge_base_file
    ADD CONSTRAINT fk_knowledge_base FOREIGN KEY (knowledge_base_id) REFERENCES public.knowledge_base(id) ON DELETE CASCADE;


--
-- Name: chat_threads fk_user_id; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chat_threads
    ADD CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES public.user_list(id) ON DELETE CASCADE;


--
-- Name: graphs graphs_creator_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.graphs
    ADD CONSTRAINT graphs_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES public.user_list(id) ON DELETE SET NULL;


--
-- Name: graphs graphs_department_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.graphs
    ADD CONSTRAINT graphs_department_id_fkey FOREIGN KEY (department_id) REFERENCES public.department(id) ON DELETE SET NULL;


--
-- Name: graphs graphs_enterprise_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.graphs
    ADD CONSTRAINT graphs_enterprise_id_fkey FOREIGN KEY (enterprise_id) REFERENCES public.enterprise(id);


--
-- Name: kb_audit_log kb_audit_log_actor_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.kb_audit_log
    ADD CONSTRAINT kb_audit_log_actor_id_fkey FOREIGN KEY (actor_id) REFERENCES public.user_list(id) ON DELETE CASCADE;


--
-- Name: kb_audit_log kb_audit_log_department_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.kb_audit_log
    ADD CONSTRAINT kb_audit_log_department_id_fkey FOREIGN KEY (department_id) REFERENCES public.department(id) ON DELETE SET NULL;


--
-- Name: kb_audit_log kb_audit_log_enterprise_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.kb_audit_log
    ADD CONSTRAINT kb_audit_log_enterprise_id_fkey FOREIGN KEY (enterprise_id) REFERENCES public.enterprise(id) ON DELETE CASCADE;


--
-- Name: kb_audit_log kb_audit_log_file_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.kb_audit_log
    ADD CONSTRAINT kb_audit_log_file_id_fkey FOREIGN KEY (file_id) REFERENCES public.knowledge_base_file(id) ON DELETE SET NULL;


--
-- Name: kb_audit_log kb_audit_log_kb_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.kb_audit_log
    ADD CONSTRAINT kb_audit_log_kb_id_fkey FOREIGN KEY (kb_id) REFERENCES public.knowledge_base(id) ON DELETE SET NULL;


--
-- Name: kb_audit_log kb_audit_log_target_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.kb_audit_log
    ADD CONSTRAINT kb_audit_log_target_user_id_fkey FOREIGN KEY (target_user_id) REFERENCES public.user_list(id) ON DELETE SET NULL;


--
-- Name: knowledge_base knowledge_base_creator_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.knowledge_base
    ADD CONSTRAINT knowledge_base_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES public.user_list(id) ON DELETE CASCADE;


--
-- Name: knowledge_base knowledge_base_department_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.knowledge_base
    ADD CONSTRAINT knowledge_base_department_id_fkey FOREIGN KEY (department_id) REFERENCES public.department(id) ON DELETE SET NULL;


--
-- Name: knowledge_base knowledge_base_enterprise_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.knowledge_base
    ADD CONSTRAINT knowledge_base_enterprise_id_fkey FOREIGN KEY (enterprise_id) REFERENCES public.enterprise(id);


--
-- Name: user_list user_list_department_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.user_list
    ADD CONSTRAINT user_list_department_id_fkey FOREIGN KEY (department_id) REFERENCES public.department(id) ON DELETE SET NULL;


--
-- Name: user_list user_list_enterprise_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.user_list
    ADD CONSTRAINT user_list_enterprise_id_fkey FOREIGN KEY (enterprise_id) REFERENCES public.enterprise(id);


--
-- PostgreSQL database dump complete
--

\unrestrict IIs1ywVfui7Zb3f5SRgOBwj2LGJXpFF5jpVnQbpR7cCgjNpjBLehhEAmULW9yma

