|
| 1 | +"""Initial schema for conversation database. |
| 2 | +
|
| 3 | +Revision ID: 001 |
| 4 | +Revises: |
| 5 | +Create Date: 2026-02-15 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +from typing import Sequence, Union |
| 10 | + |
| 11 | +import sqlalchemy as sa |
| 12 | +from alembic import op |
| 13 | + |
| 14 | +revision: str = "001" |
| 15 | +down_revision: Union[str, None] = None |
| 16 | +branch_labels: Union[str, Sequence[str], None] = None |
| 17 | +depends_on: Union[str, Sequence[str], None] = None |
| 18 | + |
| 19 | + |
| 20 | +def upgrade() -> None: |
| 21 | + """Create all tables for conversation persistence.""" |
| 22 | + # conversations |
| 23 | + op.create_table( |
| 24 | + "conversations", |
| 25 | + sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), |
| 26 | + sa.Column("title", sa.String(), nullable=True), |
| 27 | + sa.Column("created_at", sa.DateTime(), nullable=False), |
| 28 | + sa.Column("updated_at", sa.DateTime(), nullable=False), |
| 29 | + ) |
| 30 | + op.create_index( |
| 31 | + "ix_conversations_updated", |
| 32 | + "conversations", |
| 33 | + [sa.column("updated_at").desc()], |
| 34 | + ) |
| 35 | + |
| 36 | + # system_prompts |
| 37 | + op.create_table( |
| 38 | + "system_prompts", |
| 39 | + sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), |
| 40 | + sa.Column("content_hash", sa.String(), nullable=False, unique=True), |
| 41 | + sa.Column("content", sa.String(), nullable=False), |
| 42 | + ) |
| 43 | + |
| 44 | + # conversation_system_prompts |
| 45 | + op.create_table( |
| 46 | + "conversation_system_prompts", |
| 47 | + sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), |
| 48 | + sa.Column("conversation_id", sa.Integer(), nullable=False), |
| 49 | + sa.Column("system_prompt_id", sa.Integer(), nullable=False), |
| 50 | + sa.Column("position", sa.Integer(), nullable=False), |
| 51 | + sa.ForeignKeyConstraint( |
| 52 | + ["conversation_id"], ["conversations.id"], ondelete="CASCADE" |
| 53 | + ), |
| 54 | + sa.ForeignKeyConstraint(["system_prompt_id"], ["system_prompts.id"]), |
| 55 | + sa.UniqueConstraint("conversation_id", "position"), |
| 56 | + ) |
| 57 | + op.create_index( |
| 58 | + "ix_conv_sys_prompts_conv", |
| 59 | + "conversation_system_prompts", |
| 60 | + ["conversation_id"], |
| 61 | + ) |
| 62 | + |
| 63 | + # message_blocks |
| 64 | + op.create_table( |
| 65 | + "message_blocks", |
| 66 | + sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), |
| 67 | + sa.Column("conversation_id", sa.Integer(), nullable=False), |
| 68 | + sa.Column("position", sa.Integer(), nullable=False), |
| 69 | + sa.Column("conversation_system_prompt_id", sa.Integer(), nullable=True), |
| 70 | + sa.Column("model_provider", sa.String(), nullable=False), |
| 71 | + sa.Column("model_id", sa.String(), nullable=False), |
| 72 | + sa.Column( |
| 73 | + "temperature", sa.Float(), nullable=False, server_default="1.0" |
| 74 | + ), |
| 75 | + sa.Column( |
| 76 | + "max_tokens", sa.Integer(), nullable=False, server_default="4096" |
| 77 | + ), |
| 78 | + sa.Column("top_p", sa.Float(), nullable=False, server_default="1.0"), |
| 79 | + sa.Column("stream", sa.Boolean(), nullable=False, server_default="0"), |
| 80 | + sa.Column("created_at", sa.DateTime(), nullable=False), |
| 81 | + sa.Column("updated_at", sa.DateTime(), nullable=False), |
| 82 | + sa.ForeignKeyConstraint( |
| 83 | + ["conversation_id"], ["conversations.id"], ondelete="CASCADE" |
| 84 | + ), |
| 85 | + sa.ForeignKeyConstraint( |
| 86 | + ["conversation_system_prompt_id"], |
| 87 | + ["conversation_system_prompts.id"], |
| 88 | + ), |
| 89 | + sa.UniqueConstraint("conversation_id", "position"), |
| 90 | + ) |
| 91 | + op.create_index( |
| 92 | + "ix_message_blocks_conversation", |
| 93 | + "message_blocks", |
| 94 | + ["conversation_id", "position"], |
| 95 | + ) |
| 96 | + |
| 97 | + # messages |
| 98 | + op.create_table( |
| 99 | + "messages", |
| 100 | + sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), |
| 101 | + sa.Column("message_block_id", sa.Integer(), nullable=False), |
| 102 | + sa.Column("role", sa.String(), nullable=False), |
| 103 | + sa.Column("content", sa.String(), nullable=False), |
| 104 | + sa.ForeignKeyConstraint( |
| 105 | + ["message_block_id"], ["message_blocks.id"], ondelete="CASCADE" |
| 106 | + ), |
| 107 | + sa.UniqueConstraint("message_block_id", "role"), |
| 108 | + ) |
| 109 | + op.create_index("ix_messages_block", "messages", ["message_block_id"]) |
| 110 | + |
| 111 | + # attachments |
| 112 | + op.create_table( |
| 113 | + "attachments", |
| 114 | + sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), |
| 115 | + sa.Column("content_hash", sa.String(), nullable=False, unique=True), |
| 116 | + sa.Column("name", sa.String(), nullable=True), |
| 117 | + sa.Column("mime_type", sa.String(), nullable=True), |
| 118 | + sa.Column("size", sa.Integer(), nullable=True), |
| 119 | + sa.Column("location_type", sa.String(), nullable=False), |
| 120 | + sa.Column("url", sa.String(), nullable=True), |
| 121 | + sa.Column("blob_data", sa.LargeBinary(), nullable=True), |
| 122 | + sa.Column("is_image", sa.Boolean(), nullable=False, server_default="0"), |
| 123 | + sa.Column("image_width", sa.Integer(), nullable=True), |
| 124 | + sa.Column("image_height", sa.Integer(), nullable=True), |
| 125 | + ) |
| 126 | + |
| 127 | + # message_attachments |
| 128 | + op.create_table( |
| 129 | + "message_attachments", |
| 130 | + sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), |
| 131 | + sa.Column("message_id", sa.Integer(), nullable=False), |
| 132 | + sa.Column("attachment_id", sa.Integer(), nullable=False), |
| 133 | + sa.Column("position", sa.Integer(), nullable=False), |
| 134 | + sa.Column("description", sa.String(), nullable=True), |
| 135 | + sa.ForeignKeyConstraint( |
| 136 | + ["message_id"], ["messages.id"], ondelete="CASCADE" |
| 137 | + ), |
| 138 | + sa.ForeignKeyConstraint(["attachment_id"], ["attachments.id"]), |
| 139 | + sa.UniqueConstraint("message_id", "position"), |
| 140 | + ) |
| 141 | + |
| 142 | + # citations |
| 143 | + op.create_table( |
| 144 | + "citations", |
| 145 | + sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), |
| 146 | + sa.Column("message_id", sa.Integer(), nullable=False), |
| 147 | + sa.Column("position", sa.Integer(), nullable=False), |
| 148 | + sa.Column("cited_text", sa.String(), nullable=True), |
| 149 | + sa.Column("source_title", sa.String(), nullable=True), |
| 150 | + sa.Column("source_url", sa.String(), nullable=True), |
| 151 | + sa.Column("start_index", sa.Integer(), nullable=True), |
| 152 | + sa.Column("end_index", sa.Integer(), nullable=True), |
| 153 | + sa.ForeignKeyConstraint( |
| 154 | + ["message_id"], ["messages.id"], ondelete="CASCADE" |
| 155 | + ), |
| 156 | + ) |
| 157 | + |
| 158 | + |
| 159 | +def downgrade() -> None: |
| 160 | + """Drop all conversation tables.""" |
| 161 | + op.drop_table("citations") |
| 162 | + op.drop_table("message_attachments") |
| 163 | + op.drop_table("attachments") |
| 164 | + op.drop_table("messages") |
| 165 | + op.drop_table("message_blocks") |
| 166 | + op.drop_table("conversation_system_prompts") |
| 167 | + op.drop_table("system_prompts") |
| 168 | + op.drop_table("conversations") |
0 commit comments