Dev-iL commented on code in PR #37545:
URL: https://github.com/apache/airflow/pull/37545#discussion_r1496493598


##########
airflow/utils/orm_event_handlers.py:
##########
@@ -23,18 +23,25 @@
 import traceback
 
 import sqlalchemy.orm.mapper
-from sqlalchemy import event, exc
+from sqlalchemy import __version__ as sqlalchemy_version, event, exc
 
 from airflow.configuration import conf
 
 log = logging.getLogger(__name__)
 
+SQL_ALCHEMY_V1 = sqlalchemy_version.startswith("1")
+
 
 def setup_event_handlers(engine):
     """Setups event handlers."""
     from airflow.models import import_all_models
 
-    event.listen(sqlalchemy.orm.mapper, "before_configured", 
import_all_models, once=True)
+    event.listen(
+        sqlalchemy.orm.mapper if SQL_ALCHEMY_V1 else sqlalchemy.orm.Mapper,

Review Comment:
   I really want to do something like this, but don't know where to put it. The 
main reason for not wanting to append it to an existing file is because I want 
it to be as standalone as possible, so that it cannot ever cause circular 
imports.
   
   ```python
   # e.g. airflow/utils/dependency_versions.py
   from importlib.metadata import version as get_version_string
   
   from packaging.version import parse as parse_version
   
   def is_installed_lib_major_version(lib_name: str, major_version: int) -> 
bool:
       """Check if an installed library has the specified major version."""   
       return parse_version(get_version_string(lib_name)).major == major_version
   ```
   Then wherever this is needed will either have a helper wrapper or be used 
directly
   ```python
   # airflow/utils/sqlalchemy.py, or airflow/settings.py
   from airflow.utils.dependency_versions import is_installed_lib_major_version
   
   # OPTION 1:
   def is_sqlalchemy_v1() -> bool:
       return is_installed_lib_major_version("sqlalchemy", 1)
   
   ...
   if is_sqlalchemy_v1():
       # do something
   
   # OPTION 2:
   if is_installed_lib_major_version("sqlalchemy", 1):
       # do something
   ```
   
   ----
   
   BTW, why is `airflow.utils.orm_event_handlers` a separate file and not part 
of `airflow.utils.sqlalchemy`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to