This is an automated email from the ASF dual-hosted git repository.

rahulvats pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new b822e4f576f fix-dowangrade with note to 2.11 (#55118)
b822e4f576f is described below

commit b822e4f576f28f8215dd492270d941cfe183613b
Author: Rahul Vats <[email protected]>
AuthorDate: Wed Sep 3 17:57:58 2025 +0530

    fix-dowangrade with note to 2.11 (#55118)
---
 airflow-core/docs/img/airflow_erd.sha256           |  2 +-
 .../versions/0035_3_0_0_update_user_id_type.py     | 75 +++++++++++++++++++++-
 2 files changed, 74 insertions(+), 3 deletions(-)

diff --git a/airflow-core/docs/img/airflow_erd.sha256 
b/airflow-core/docs/img/airflow_erd.sha256
index 0105a601064..2240ef35f74 100644
--- a/airflow-core/docs/img/airflow_erd.sha256
+++ b/airflow-core/docs/img/airflow_erd.sha256
@@ -1 +1 @@
-db83973c6b3584fbb7e06a204d3d33021f6efbfa7b00b9092fbe4af0a3a42533
\ No newline at end of file
+203118e7295d7506825eeb16edb1a01fe8428b92134eee626fb73d7b73ab016a
\ No newline at end of file
diff --git 
a/airflow-core/src/airflow/migrations/versions/0035_3_0_0_update_user_id_type.py
 
b/airflow-core/src/airflow/migrations/versions/0035_3_0_0_update_user_id_type.py
index 321a1e2bbaf..e60aaad09d1 100644
--- 
a/airflow-core/src/airflow/migrations/versions/0035_3_0_0_update_user_id_type.py
+++ 
b/airflow-core/src/airflow/migrations/versions/0035_3_0_0_update_user_id_type.py
@@ -29,6 +29,7 @@ from __future__ import annotations
 
 import sqlalchemy as sa
 from alembic import op
+from sqlalchemy import text
 
 # revision identifiers, used by Alembic.
 revision = "44eabb1904b4"
@@ -46,7 +47,77 @@ def upgrade():
 
 
 def downgrade():
+    """Unapply Update dag_run_note.user_id and task_instance_note.user_id 
columns to String."""
+    # Handle the case where user_id contains string values that cannot be 
converted to integer
+    # This is necessary because the migration from 2.11.0 -> 3.0.0 changed 
user_id from Integer to String
+    # and when downgrading, we need to safely handle cases where string 
user_ids like "admin" exist
+
+    dialect_name = op.get_bind().dialect.name
+
+    # Clean up non-numeric user_id values before type conversion to prevent 
errors
+    if dialect_name == "postgresql":
+        # For PostgreSQL, use regex to identify non-integer values and set 
them to NULL
+        op.execute(
+            text("""
+                UPDATE dag_run_note
+                SET user_id = NULL
+                WHERE user_id IS NOT NULL
+                AND user_id !~ '^[0-9]+$'
+            """)
+        )
+        op.execute(
+            text("""
+                UPDATE task_instance_note
+                SET user_id = NULL
+                WHERE user_id IS NOT NULL
+                AND user_id !~ '^[0-9]+$'
+            """)
+        )
+    elif dialect_name == "mysql":
+        # For MySQL, use REGEXP to identify non-integer values
+        op.execute(
+            text("""
+                UPDATE dag_run_note
+                SET user_id = NULL
+                WHERE user_id IS NOT NULL
+                AND user_id NOT REGEXP '^[0-9]+$'
+            """)
+        )
+        op.execute(
+            text("""
+                UPDATE task_instance_note
+                SET user_id = NULL
+                WHERE user_id IS NOT NULL
+                AND user_id NOT REGEXP '^[0-9]+$'
+            """)
+        )
+    elif dialect_name == "sqlite":
+        # SQLite doesn't have regex, so use GLOB pattern matching
+        op.execute(
+            text("""
+                UPDATE dag_run_note
+                SET user_id = NULL
+                WHERE user_id IS NOT NULL
+                AND (user_id = '' OR user_id GLOB '*[^0-9]*')
+            """)
+        )
+        op.execute(
+            text("""
+                UPDATE task_instance_note
+                SET user_id = NULL
+                WHERE user_id IS NOT NULL
+                AND (user_id = '' OR user_id GLOB '*[^0-9]*')
+            """)
+        )
+
+    # Now alter the column types back to Integer
     with op.batch_alter_table("dag_run_note") as batch_op:
-        batch_op.alter_column("user_id", type_=sa.Integer(), 
postgresql_using="user_id::integer")
+        if dialect_name == "postgresql":
+            batch_op.alter_column("user_id", type_=sa.Integer(), 
postgresql_using="user_id::integer")
+        else:
+            batch_op.alter_column("user_id", type_=sa.Integer())
     with op.batch_alter_table("task_instance_note") as batch_op:
-        batch_op.alter_column("user_id", type_=sa.Integer(), 
postgresql_using="user_id::integer")
+        if dialect_name == "postgresql":
+            batch_op.alter_column("user_id", type_=sa.Integer(), 
postgresql_using="user_id::integer")
+        else:
+            batch_op.alter_column("user_id", type_=sa.Integer())

Reply via email to