gitdataai/libs/migrate/sql/m20260508_000001_create_ai_conversation.sql

28 lines
1.2 KiB
SQL

CREATE TABLE IF NOT EXISTS ai_conversation (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL,
project_id UUID,
scope VARCHAR(16) NOT NULL,
title VARCHAR(512),
model VARCHAR(128) NOT NULL DEFAULT 'gpt-4',
model_config JSONB,
status VARCHAR(32) NOT NULL DEFAULT 'active',
root_message_id UUID,
fork_count INT NOT NULL DEFAULT 0,
is_shared BOOLEAN NOT NULL DEFAULT false,
message_count INT NOT NULL DEFAULT 0,
token_usage_total INT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_ai_conv_user_id ON ai_conversation (user_id);
CREATE INDEX IF NOT EXISTS idx_ai_conv_project_id ON ai_conversation (project_id);
CREATE INDEX IF NOT EXISTS idx_ai_conv_scope ON ai_conversation (scope);
CREATE INDEX IF NOT EXISTS idx_ai_conv_user_created ON ai_conversation (user_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_ai_conv_project_created ON ai_conversation (project_id, created_at DESC);
ALTER TABLE ai_conversation
ADD CONSTRAINT fk_ai_conv_project
FOREIGN KEY (project_id) REFERENCES project(id) ON DELETE CASCADE;