Files
life-echo/api/alembic/versions/0019_backfill_missing_columns.py
penghanyuan 0a9381c711 feat(migration): add missing columns to segments and conversations tables
This migration adds the columns 'audio_duration_seconds' to the 'segments' table and 'deleted_at' to the 'conversations' table, which were previously missing in the production database. The upgrade function checks for the existence of these columns and adds them if they are not present. The downgrade function allows for the removal of these columns if necessary.
2026-05-14 19:16:48 +02:00

52 lines
1.5 KiB
Python
Raw 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.
"""补建缺失列segments.audio_duration_seconds, conversations.deleted_at
0001 用 create_all 建表,对已有表不会 ALTER 追加列。
本迁移补齐 ORM 模型中存在但生产库缺失的两列。
Revision ID: 0019_backfill_missing_columns
Revises: 0018_users_language_preference
"""
from typing import Sequence, Union
import sqlalchemy as sa
from alembic import op
revision: str = "0019_backfill_missing_columns"
down_revision: Union[str, None] = "0018_users_language_preference"
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 "audio_duration_seconds" not in seg_cols:
op.add_column(
"segments",
sa.Column("audio_duration_seconds", sa.Integer(), nullable=True),
)
conv_cols = _column_names("conversations")
if "deleted_at" not in conv_cols:
op.add_column(
"conversations",
sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
)
def downgrade() -> None:
conv_cols = _column_names("conversations")
if "deleted_at" in conv_cols:
op.drop_column("conversations", "deleted_at")
seg_cols = _column_names("segments")
if "audio_duration_seconds" in seg_cols:
op.drop_column("segments", "audio_duration_seconds")