fix: ensure idempotency in alembic migrations by checking for existing columns and indexes before creation

This commit is contained in:
Anish Sarkar 2026-05-10 22:45:26 +05:30
parent 319923fb40
commit 2c8828f60c

View file

@ -36,10 +36,17 @@ depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
columns = {c["name"] for c in inspector.get_columns("new_chat_messages")}
indexes = {i["name"] for i in inspector.get_indexes("new_chat_messages")}
if "turn_id" not in columns:
op.add_column( op.add_column(
"new_chat_messages", "new_chat_messages",
sa.Column("turn_id", sa.String(length=64), nullable=True), sa.Column("turn_id", sa.String(length=64), nullable=True),
) )
if "ix_new_chat_messages_turn_id" not in indexes:
op.create_index( op.create_index(
"ix_new_chat_messages_turn_id", "ix_new_chat_messages_turn_id",
"new_chat_messages", "new_chat_messages",