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


##########
airflow/migrations/utils.py:
##########
@@ -58,3 +58,35 @@ def disable_sqlite_fkeys(op):
         op.execute("PRAGMA foreign_keys=on")
     else:
         yield op
+
+
+def mysql_drop_foreignkey_if_exists(constraint_name, table_name, op):
+    """Older Mysql versions do not support DROP FOREIGN KEY IF EXISTS."""
+    op.execute(f"""
+        set @var=if((SELECT true FROM information_schema.TABLE_CONSTRAINTS 
WHERE
+            CONSTRAINT_SCHEMA = DATABASE() AND
+            TABLE_NAME        = '{table_name}' AND
+            CONSTRAINT_NAME   = '{constraint_name}' AND
+            CONSTRAINT_TYPE   = 'FOREIGN KEY') = true,'ALTER TABLE {table_name}
+            DROP CONSTRAINT {constraint_name}','select 1');
+
+        prepare stmt from @var;
+        execute stmt;
+        deallocate prepare stmt;
+    """)
+
+
+def mysql_drop_index_if_exists(index_name, table_name, op):
+    """Older Mysql versions do not support DROP INDEX IF EXISTS."""
+    op.execute(f"""
+        set @var=if((SELECT true FROM information_schema.TABLE_CONSTRAINTS 
WHERE
+            CONSTRAINT_SCHEMA = DATABASE() AND
+            TABLE_NAME        = '{table_name}' AND
+            CONSTRAINT_NAME   = '{index_name}' AND
+            CONSTRAINT_TYPE   = 'INDEX') = true,'ALTER TABLE {table_name}
+            DROP INDEX {index_name}','select 1');
+
+        prepare stmt from @var;

Review Comment:
   same as https://github.com/apache/airflow/pull/44790/files#r1884311558



-- 
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