kaxil commented on code in PR #44790:
URL: https://github.com/apache/airflow/pull/44790#discussion_r1884311558


##########
airflow/migrations/versions/0027_2_10_3_fix_dag_schedule_dataset_alias_reference_naming.py:
##########
@@ -39,91 +41,200 @@
 depends_on = None
 airflow_version = "2.10.3"
 
-if TYPE_CHECKING:
-    from alembic.operations.base import BatchOperations
-    from sqlalchemy.sql.elements import conv
-
-
-def _rename_fk_constraint(
-    *,
-    batch_op: BatchOperations,
-    original_name: str | conv,
-    new_name: str | conv,
-    referent_table: str,
-    local_cols: list[str],
-    remote_cols: list[str],
-    ondelete: str,
-) -> None:
-    batch_op.drop_constraint(original_name, type_="foreignkey")
-    batch_op.create_foreign_key(
-        constraint_name=new_name,
-        referent_table=referent_table,
-        local_cols=local_cols,
-        remote_cols=remote_cols,
-        ondelete=ondelete,
-    )
+
+def mysql_create_foreignkey_if_not_exists(
+    constraint_name, table_name, column_name, ref_table, ref_column, op
+):
+    op.execute(f"""
+        set @var = (
+        SELECT CASE
+            WHEN EXISTS (
+                SELECT 1
+                FROM information_schema.TABLE_CONSTRAINTS
+                WHERE
+                    CONSTRAINT_SCHEMA = DATABASE() AND
+                    TABLE_NAME = '{table_name}' AND
+                    CONSTRAINT_NAME = '{constraint_name}' AND
+                    CONSTRAINT_TYPE = 'FOREIGN KEY'
+            ) THEN 'SELECT 1'
+            ELSE CONCAT(
+                'ALTER TABLE {table_name} ',
+                'ADD CONSTRAINT {constraint_name} FOREIGN KEY ({column_name}) 
',
+                'REFERENCES {ref_table}({ref_column}) ',
+                'ON DELETE CASCADE'
+            )
+        END
+    );
+
+    PREPARE stmt FROM @var;

Review Comment:
   This cause issues with PyMySQL (check link below) -- not a big deal if we 
can't find an alternative but worth spending 10-15 mins
   
   https://github.com/apache/airflow/issues/43690#issuecomment-2538776099



##########
airflow/migrations/versions/0027_2_10_3_fix_dag_schedule_dataset_alias_reference_naming.py:
##########
@@ -39,91 +41,200 @@
 depends_on = None
 airflow_version = "2.10.3"
 
-if TYPE_CHECKING:
-    from alembic.operations.base import BatchOperations
-    from sqlalchemy.sql.elements import conv
-
-
-def _rename_fk_constraint(
-    *,
-    batch_op: BatchOperations,
-    original_name: str | conv,
-    new_name: str | conv,
-    referent_table: str,
-    local_cols: list[str],
-    remote_cols: list[str],
-    ondelete: str,
-) -> None:
-    batch_op.drop_constraint(original_name, type_="foreignkey")
-    batch_op.create_foreign_key(
-        constraint_name=new_name,
-        referent_table=referent_table,
-        local_cols=local_cols,
-        remote_cols=remote_cols,
-        ondelete=ondelete,
-    )
+
+def mysql_create_foreignkey_if_not_exists(
+    constraint_name, table_name, column_name, ref_table, ref_column, op
+):
+    op.execute(f"""
+        set @var = (
+        SELECT CASE
+            WHEN EXISTS (
+                SELECT 1
+                FROM information_schema.TABLE_CONSTRAINTS
+                WHERE
+                    CONSTRAINT_SCHEMA = DATABASE() AND
+                    TABLE_NAME = '{table_name}' AND
+                    CONSTRAINT_NAME = '{constraint_name}' AND
+                    CONSTRAINT_TYPE = 'FOREIGN KEY'
+            ) THEN 'SELECT 1'
+            ELSE CONCAT(
+                'ALTER TABLE {table_name} ',
+                'ADD CONSTRAINT {constraint_name} FOREIGN KEY ({column_name}) 
',
+                'REFERENCES {ref_table}({ref_column}) ',
+                'ON DELETE CASCADE'
+            )
+        END
+    );
+
+    PREPARE stmt FROM @var;

Review Comment:
   This can cause issues with PyMySQL (check link below) -- not a big deal if 
we can't find an alternative but worth spending 10-15 mins
   
   https://github.com/apache/airflow/issues/43690#issuecomment-2538776099



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to