feat(migration): add tts_audio_urls column to segments and conversation_messages tables

This migration introduces the 'tts_audio_urls' column to both the 'segments' and 'conversation_messages' tables, addressing a discrepancy between the ORM model and the production database. The upgrade function checks for the existence of these columns and adds them if they are missing, while the downgrade function allows for their removal if necessary.
This commit is contained in:
penghanyuan
2026-05-17 21:38:19 +02:00
parent 4ed3491aea
commit 8df6e42a30

View File

@@ -0,0 +1,51 @@
"""补建缺失列segments.tts_audio_urls, conversation_messages.tts_audio_urls
0001 用 create_all 建表,对已有表不会 ALTER 追加列。
本迁移补齐 ORM 模型中存在但生产库缺失的 tts_audio_urls 列。
Revision ID: 0020_add_tts_audio_urls_column
Revises: 0019_backfill_missing_columns
"""
from typing import Sequence, Union
import sqlalchemy as sa
from alembic import op
revision: str = "0020_add_tts_audio_urls_column"
down_revision: Union[str, None] = "0019_backfill_missing_columns"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def _column_names(table_name: str) -> set[str]:
bind = op.get_bind()
inspector = sa.inspect(bind)
return {column["name"] for column in inspector.get_columns(table_name)}
def upgrade() -> None:
seg_cols = _column_names("segments")
if "tts_audio_urls" not in seg_cols:
op.add_column(
"segments",
sa.Column("tts_audio_urls", sa.JSON(), nullable=True),
)
msg_cols = _column_names("conversation_messages")
if "tts_audio_urls" not in msg_cols:
op.add_column(
"conversation_messages",
sa.Column("tts_audio_urls", sa.JSON(), nullable=True),
)
def downgrade() -> None:
msg_cols = _column_names("conversation_messages")
if "tts_audio_urls" in msg_cols:
op.drop_column("conversation_messages", "tts_audio_urls")
seg_cols = _column_names("segments")
if "tts_audio_urls" in seg_cols:
op.drop_column("segments", "tts_audio_urls")