gitdataai/libs/migrate/sql/m20260508_000001_create_ai_conversation.sql
ZhenYi b1ef024724 fix(migrate): reorder create_project before add_workspace_id_to_project and remove foreign keys
- Move create_project migration before add_workspace_id_to_project so the
  project table exists when workspace_id column is added
- Remove all FOREIGN KEY constraints from migration SQL files
2026-05-11 01:31:33 +08:00

24 lines
1.0 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);