You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "Arunodoy18 (via GitHub)" <gi...@apache.org> on 2026/01/01 06:17:58 UTC

[PR] Fix/remove orm from alembic migrations [airflow]

Arunodoy18 opened a new pull request, #60012:
URL: https://github.com/apache/airflow/pull/60012

   
   What does this PR do?
   
   - Refactors the `0015_2_9_0_update_trigger_kwargs_type.py` migration script to remove ORM model imports and usage
   - Removes direct imports of `airflow.models.Trigger` from the migration
   - Adds a pre-commit hook (`check-no-orm-in-migrations`) that prevents ORM model imports in future Alembic migration scripts
   - Adds explanatory comments about why the original ORM-based operations were unnecessary
   
   ### Why is this needed?
   
   Using ORM models directly in Alembic migration scripts causes issues when the model schema changes. Specifically:
   
   1. If a new column is added to the Trigger model in a future version (e.g., 3.2.0)
   2. The ORM reference in the 2.9.0 migration script will include that new column
   3. Downgrades from 3.2.X to ≤ 2.9.0 will fail with errors like:
      \`\`\`
      sqlalchemy.exc.ProgrammingError: column trigger.trigger_queue does not exist
      \`\`\`
   
   This is a well-known anti-pattern in Alembic migrations. The general recommendation is to use SQLAlchemy Core (table reflection) or raw SQL instead of ORM models.
   
   ### How to test
   
   1. Run the pre-commit hook: `python dev/check_no_orm_in_migrations.py`
   2. Verify no ORM imports are detected in migration scripts
   3. Test upgrade/downgrade scenarios:
      \`\`\`bash
      breeze shell 'airflow db reset --skip-init -y && airflow db migrate --to-revision heads' \
          --use-airflow-version 2.11.0 --airflow-extras pydantic --answer y \
          && breeze shell \"export AIRFLOW__DATABASE__EXTERNAL_DB_MANAGERS=airflow.providers.fab.auth_manager.models.db.FABDBManager \
          && airflow db migrate --to-revision heads \
          && airflow db downgrade -n 2.7.0 -y \
          && airflow db migrate\"
      \`\`\`
   
   ### Related Issues
   
   Fixes issue with Alembic migrations using ORM models that prevent future schema changes." --base main --head fix/remove-orm-from-alembic-migrations
   
   Closes: 59871


-- 
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-unsubscribe@airflow.apache.org

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


Re: [PR] Fix/remove orm from alembic migrations [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on PR #60012:
URL: https://github.com/apache/airflow/pull/60012#issuecomment-3711712890

   @Arunodoy18 I am going to close your PRs -- Please review and test your changes with correct PR description. Using LLMs without those increase maintenance burdens and CI run time.
   
   Feel free to recreate focussed PRs following those guidelines.


-- 
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-unsubscribe@airflow.apache.org

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


Re: [PR] Fix/remove orm from alembic migrations [airflow]

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #60012:
URL: https://github.com/apache/airflow/pull/60012#issuecomment-3703699561

   You have conflicts - likely with this one https://github.com/apache/airflow/pull/59239  that addresses the issue partially. My suggestion: always pull latest main first before opening a PR :).
   
   You shoudl rebase and resolve conflicts


-- 
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-unsubscribe@airflow.apache.org

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


Re: [PR] Fix/remove orm from alembic migrations [airflow]

Posted by "zach-overflow (via GitHub)" <gi...@apache.org>.
zach-overflow commented on code in PR #60012:
URL: https://github.com/apache/airflow/pull/60012#discussion_r2657756225


##########
dev/check_no_orm_in_migrations.py:
##########
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+"""
+Pre-commit hook: Ensure no Alembic migration script imports ORM models.
+"""
+import sys
+import re
+from pathlib import Path
+
+MIGRATIONS_DIRS = [
+    Path("airflow-core/src/airflow/migrations/versions"),
+]
+ORM_IMPORT_PATTERNS = [
+    re.compile(r"from +airflow\\.models"),
+    re.compile(r"import +airflow\\.models"),
+    re.compile(r"from +airflow\\.models\\.", re.IGNORECASE),
+    re.compile(r"import +airflow\\.models\\.", re.IGNORECASE),
+]

Review Comment:
   Thanks for putting together this pre-commit check, it will definitely help out! A few initial thoughts:
   
   1. I wonder if the use of the [`ast`](https://docs.python.org/3/library/ast.html) module might provide a more robust detection mechanism for improper ORM references in migration scripts. That would mitigate needing to filter false positives (for example, if you're reading from a commented line, string, docstring, etc containing the string "from airflow import "). The `ast` module will also likely speed up the file parsing a bit as a nice side-effect.
   2. I believe there are _some_ edge cases where certain constants are fine to import from the `airflow.models` module. You will probably want to account for some small allowlist of valid import targets. For example, there are a number of migration scripts which have the following valid import ([example](https://github.com/apache/airflow/blob/a09ed06a2b59057d1e397b8e28821b26038ea2ab/airflow-core/src/airflow/migrations/versions/0081_3_1_0_remove_dag_id_from_deadline.py#L34)):
       ```python
       from airflow.models import ID_LEN
       ```



-- 
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-unsubscribe@airflow.apache.org

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


Re: [PR] Fix/remove orm from alembic migrations [airflow]

Posted by "ephraimbuddy (via GitHub)" <gi...@apache.org>.
ephraimbuddy commented on code in PR #60012:
URL: https://github.com/apache/airflow/pull/60012#discussion_r2660743826


##########
airflow-core/src/airflow/migrations/versions/0015_2_9_0_update_trigger_kwargs_type.py:
##########
@@ -78,14 +65,8 @@ def downgrade():
         ------------
         """)
         )
-    else:
-        session = get_session()
-        try:
-            for trigger in session.query(Trigger).options(lazyload(Trigger.task_instance)):
-                trigger.encrypted_kwargs = json.dumps(BaseSerialization.serialize(trigger.kwargs))
-            session.commit()
-        finally:
-            session.close()
+    # Note: The original migration used ORM to attempt serialization, but this is not necessary
+    # for the downgrade. The column type change below handles the conversion.

Review Comment:
   Interesting, did you test this?



-- 
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-unsubscribe@airflow.apache.org

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


Re: [PR] Fix/remove orm from alembic migrations [airflow]

Posted by "Arunodoy18 (via GitHub)" <gi...@apache.org>.
Arunodoy18 commented on code in PR #60012:
URL: https://github.com/apache/airflow/pull/60012#discussion_r2661372121


##########
airflow-core/src/airflow/migrations/versions/0015_2_9_0_update_trigger_kwargs_type.py:
##########
@@ -78,14 +65,8 @@ def downgrade():
         ------------
         """)
         )
-    else:
-        session = get_session()
-        try:
-            for trigger in session.query(Trigger).options(lazyload(Trigger.task_instance)):
-                trigger.encrypted_kwargs = json.dumps(BaseSerialization.serialize(trigger.kwargs))
-            session.commit()
-        finally:
-            session.close()
+    # Note: The original migration used ORM to attempt serialization, but this is not necessary
+    # for the downgrade. The column type change below handles the conversion.

Review Comment:
   Yeah it was done , I did test this.



-- 
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-unsubscribe@airflow.apache.org

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


Re: [PR] Fix/remove orm from alembic migrations [airflow]

Posted by "zach-overflow (via GitHub)" <gi...@apache.org>.
zach-overflow commented on code in PR #60012:
URL: https://github.com/apache/airflow/pull/60012#discussion_r2657756225


##########
dev/check_no_orm_in_migrations.py:
##########
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+"""
+Pre-commit hook: Ensure no Alembic migration script imports ORM models.
+"""
+import sys
+import re
+from pathlib import Path
+
+MIGRATIONS_DIRS = [
+    Path("airflow-core/src/airflow/migrations/versions"),
+]
+ORM_IMPORT_PATTERNS = [
+    re.compile(r"from +airflow\\.models"),
+    re.compile(r"import +airflow\\.models"),
+    re.compile(r"from +airflow\\.models\\.", re.IGNORECASE),
+    re.compile(r"import +airflow\\.models\\.", re.IGNORECASE),
+]

Review Comment:
   Thanks for putting together this pre-commit check, it will definitely help out! A few initial thoughts:
   
   1. I wonder if the use of the [`ast`](https://docs.python.org/3/library/ast.html) module might provide a more robust detection mechanism for improper ORM references in migration scripts. That would mitigate needing to determine if you're reading from a commented line, string, docstring, etc. The `ast` module will also likely speed up the file parsing a bit as a nice side-effect.
   2. I believe there are _some_ edge cases where certain constants are fine to import from the `airflow.models` module. You will probably want to account for some small allowlist of valid import targets. For example, there are a number of migration scripts which have the following valid import ([example](https://github.com/apache/airflow/blob/a09ed06a2b59057d1e397b8e28821b26038ea2ab/airflow-core/src/airflow/migrations/versions/0081_3_1_0_remove_dag_id_from_deadline.py#L34)):
       ```python
       from airflow.models import ID_LEN
       ```



-- 
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-unsubscribe@airflow.apache.org

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


Re: [PR] Fix/remove orm from alembic migrations [airflow]

Posted by "Arunodoy18 (via GitHub)" <gi...@apache.org>.
Arunodoy18 commented on PR #60012:
URL: https://github.com/apache/airflow/pull/60012#issuecomment-3704064633

   Yehh Sure, Working on this, Will report you soon, Thank You.


-- 
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-unsubscribe@airflow.apache.org

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


Re: [PR] Fix/remove orm from alembic migrations [airflow]

Posted by "Arunodoy18 (via GitHub)" <gi...@apache.org>.
Arunodoy18 commented on PR #60012:
URL: https://github.com/apache/airflow/pull/60012#issuecomment-3708063655

   Yeahh thanks , Following the issue and telling you.
   Thank you


-- 
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-unsubscribe@airflow.apache.org

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


Re: [PR] Fix/remove orm from alembic migrations [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil closed pull request #60012: Fix/remove orm from alembic migrations
URL: https://github.com/apache/airflow/pull/60012


-- 
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-unsubscribe@airflow.apache.org

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


Re: [PR] Fix/remove orm from alembic migrations [airflow]

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on code in PR #60012:
URL: https://github.com/apache/airflow/pull/60012#discussion_r2659120450


##########
dev/check_no_orm_in_migrations.py:
##########
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+"""
+Pre-commit hook: Ensure no Alembic migration script imports ORM models.
+"""
+import sys
+import re
+from pathlib import Path
+
+MIGRATIONS_DIRS = [
+    Path("airflow-core/src/airflow/migrations/versions"),
+]
+ORM_IMPORT_PATTERNS = [
+    re.compile(r"from +airflow\\.models"),
+    re.compile(r"import +airflow\\.models"),
+    re.compile(r"from +airflow\\.models\\.", re.IGNORECASE),
+    re.compile(r"import +airflow\\.models\\.", re.IGNORECASE),
+]

Review Comment:
   Yeah - withh `ast` you could check if the imported classes derive from BaseModel



-- 
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-unsubscribe@airflow.apache.org

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


Re: [PR] Fix/remove orm from alembic migrations [airflow]

Posted by "Arunodoy18 (via GitHub)" <gi...@apache.org>.
Arunodoy18 commented on PR #60012:
URL: https://github.com/apache/airflow/pull/60012#issuecomment-3703318495

   I've opened this PR to address the issue discussed above. The goal is to make the migration behavior safer and more predictable by avoiding ORM usage in Alembic scripts. I’d appreciate a review when you have time, and I’m happy to iterate on the approach based on feedback.


-- 
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-unsubscribe@airflow.apache.org

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


Re: [PR] Fix/remove orm from alembic migrations [airflow]

Posted by "ephraimbuddy (via GitHub)" <gi...@apache.org>.
ephraimbuddy commented on code in PR #60012:
URL: https://github.com/apache/airflow/pull/60012#discussion_r2660739199


##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/dag_run.py:
##########
@@ -138,6 +138,15 @@ def delete_dag_run(dag_id: str, dag_run_id: str, session: SessionDep):
             status.HTTP_404_NOT_FOUND,
             f"The DagRun with dag_id: `{dag_id}` and run_id: `{dag_run_id}` was not found",
         )
+
+    # Only allow deletion if dag_run.state is in DAGRunPatchStates
+    allowed_states = {state.value for state in DAGRunPatchStates}
+    if dag_run.state not in allowed_states:
+        raise HTTPException(
+            status.HTTP_400_BAD_REQUEST,
+            f"Cannot delete DagRun in state '{dag_run.state}'. Only allowed in states: {', '.join(allowed_states)}."
+        )

Review Comment:
   Seems there's some mix up here with your other PR



-- 
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-unsubscribe@airflow.apache.org

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