"""Helpers shared by the online Alembic environment and its tests.""" from __future__ import annotations import os from typing import Any from sqlalchemy import text def configure_mysql_migration_lock_waits(connection: Any) -> None: """Set bounded MySQL DDL lock waits without leaving a transaction open. SQLAlchemy starts an implicit transaction for ``SET SESSION`` on MySQL. Alembic's migration transaction then reuses it, and closing the connection rolls back the ``alembic_version`` updates even though MySQL DDL itself has already committed. Commit the session-local settings before Alembic starts its transaction so successful migrations are recorded durably. """ try: lock_wait = int(os.getenv("DEER_FLOW_ALEMBIC_LOCK_WAIT", "15")) except ValueError: lock_wait = 15 connection.execute(text(f"SET SESSION lock_wait_timeout = {lock_wait}")) connection.execute(text(f"SET SESSION innodb_lock_wait_timeout = {lock_wait}")) connection.commit()