59 lines
2.1 KiB
Python
59 lines
2.1 KiB
Python
"""Add persistent assistant conversations and messages.
|
|
|
|
Revision ID: 20260831_0011
|
|
Revises: 20260830_0010
|
|
Create Date: 2026-08-31
|
|
"""
|
|
|
|
from collections.abc import Sequence
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
from sqlalchemy.dialects import postgresql
|
|
|
|
revision: str = "20260831_0011"
|
|
down_revision: str | None = "20260830_0010"
|
|
branch_labels: str | Sequence[str] | None = None
|
|
depends_on: str | Sequence[str] | None = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.create_table(
|
|
"conversations",
|
|
sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
|
|
sa.Column("user_id", sa.BigInteger(), nullable=False),
|
|
sa.Column("title", sa.String(length=120), nullable=False),
|
|
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
|
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
|
sa.ForeignKeyConstraint(["user_id"], ["user_accounts.id"], ondelete="CASCADE"),
|
|
sa.PrimaryKeyConstraint("id"),
|
|
)
|
|
op.create_index("ix_conversations_user_updated", "conversations", ["user_id", "updated_at"])
|
|
|
|
op.create_table(
|
|
"conversation_messages",
|
|
sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
|
|
sa.Column("conversation_id", sa.BigInteger(), nullable=False),
|
|
sa.Column("role", sa.String(length=20), nullable=False),
|
|
sa.Column("content", sa.Text(), nullable=False),
|
|
sa.Column(
|
|
"structured_data",
|
|
postgresql.JSONB(astext_type=sa.Text()),
|
|
server_default="{}",
|
|
nullable=False,
|
|
),
|
|
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
|
sa.ForeignKeyConstraint(["conversation_id"], ["conversations.id"], ondelete="CASCADE"),
|
|
sa.PrimaryKeyConstraint("id"),
|
|
)
|
|
op.create_index(
|
|
"ix_conversation_messages_conversation_created",
|
|
"conversation_messages",
|
|
["conversation_id", "created_at"],
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_table("conversation_messages")
|
|
op.drop_table("conversations")
|