Files
hunjing/backend/alembic/versions/002_add_chat_session_owner_scope.py
2026-08-21 16:00:13 +08:00

57 lines
1.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""add_chat_session_owner_scope
Revision ID: 002
Revises: 001
Create Date: 2026-07-14 07:54:32.969191
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = '002'
down_revision: str | Sequence[str] | None = '001'
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
conn = op.get_bind()
is_sqlite = conn.engine.dialect.name == "sqlite"
if is_sqlite:
# SQLite:用 batch 模式加列(新增 NOT NULL 列需 batch recreating)
with op.batch_alter_table("chat_sessions") as batch_op:
batch_op.add_column(
sa.Column("owner_scope", sa.String(length=80), nullable=False, server_default=""),
)
batch_op.create_index("ix_chat_sessions_owner_scope", ["owner_scope"])
# 移除 server_default(仅 DDL 阶段需要)
with op.batch_alter_table("chat_sessions") as batch_op:
batch_op.alter_column("owner_scope", server_default=None)
else:
op.add_column(
"chat_sessions",
sa.Column("owner_scope", sa.String(length=80), nullable=False, server_default=""),
)
op.create_index(
op.f("ix_chat_sessions_owner_scope"), "chat_sessions", ["owner_scope"], unique=False
)
op.alter_column("chat_sessions", "owner_scope", server_default=None)
def downgrade() -> None:
conn = op.get_bind()
is_sqlite = conn.engine.dialect.name == "sqlite"
if is_sqlite:
with op.batch_alter_table("chat_sessions") as batch_op:
batch_op.drop_index("ix_chat_sessions_owner_scope")
batch_op.drop_column("owner_scope")
else:
op.drop_index(op.f("ix_chat_sessions_owner_scope"), table_name="chat_sessions")
op.drop_column("chat_sessions", "owner_scope")