52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
|
|
"""补建缺失列: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")
|