"""SQLAlchemy + aiomysql compatibility for pool_pre_ping.

SQLAlchemy 2.0.36+ pymysql do_ping may call ping() with no args; aiomysql's async
adapter requires ``ping(reconnect)``. Patch the dialect once before create_async_engine.
"""

from typing import Any

_patched = False


def patch_aiomysql_do_ping() -> None:
    global _patched
    if _patched:
        return
    from sqlalchemy.dialects.mysql.aiomysql import MySQLDialect_aiomysql

    def do_ping(self, dbapi_connection: Any) -> bool:
        dbapi_connection.ping(False)
        return True

    MySQLDialect_aiomysql.do_ping = do_ping  # type: ignore[method-assign]
    _patched = True
