pankajkoti commented on code in PR #39638:
URL: https://github.com/apache/airflow/pull/39638#discussion_r1602152626


##########
airflow/migrations/versions/0143_2_9_2_add_indexes_on_dag_id_column_in_referencing_tables.py:
##########
@@ -0,0 +1,115 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""Add indexes on dag_id column in referencing tables.
+
+Revision ID: 0fd0c178cbe8
+Revises: 686269002441
+Create Date: 2024-05-15 16:52:39.077349
+
+"""
+
+from __future__ import annotations
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision = "0fd0c178cbe8"
+down_revision = "686269002441"
+branch_labels = None
+depends_on = None
+airflow_version = "2.9.2"
+
+
+def upgrade():
+    """Apply Add indexes on dag_id column in referencing tables."""
+    with op.batch_alter_table("dag_owner_attributes", schema=None) as batch_op:
+        batch_op.create_index("idx_dag_owner_attributes_dag_id", ["dag_id"], 
unique=False)
+
+    with op.batch_alter_table("dag_schedule_dataset_reference", schema=None) 
as batch_op:
+        batch_op.create_index("idx_dag_schedule_dataset_reference_dag_id", 
["dag_id"], unique=False)
+
+    with op.batch_alter_table("dag_tag", schema=None) as batch_op:
+        batch_op.create_index("idx_dag_tag_dag_id", ["dag_id"], unique=False)
+
+    with op.batch_alter_table("dag_warning", schema=None) as batch_op:
+        batch_op.create_index("idx_dag_warning_dag_id", ["dag_id"], 
unique=False)
+
+    with op.batch_alter_table("dataset_dag_run_queue", schema=None) as 
batch_op:
+        batch_op.create_index("idx_dataset_dag_run_queue_target_dag_id", 
["target_dag_id"], unique=False)
+
+    with op.batch_alter_table("task_outlet_dataset_reference", schema=None) as 
batch_op:
+        batch_op.create_index("idx_task_outlet_dataset_reference_dag_id", 
["dag_id"], unique=False)
+
+
+def _handle_foreign_key_constraint_index_deletion(
+    batch_op, constraint_name, index_name, local_fk_column_name
+):
+    batch_op.drop_constraint(constraint_name, type_="foreignkey")
+    batch_op.drop_index(index_name)
+    batch_op.create_foreign_key(
+        constraint_name, "dag", [local_fk_column_name], ["dag_id"], 
ondelete="CASCADE"
+    )
+
+
+def downgrade():
+    """Unapply Add indexes on dag_id column in referencing tables."""
+    # conn = op.get_bind()
+    # with op.batch_alter_table("dag_owner_attributes", schema=None) as 
batch_op:
+    #     if conn.dialect.name == "mysql":
+    #         batch_op.execute("ALTER TABLE dag_owner_attributes DROP FOREIGN 
KEY `dag.dag_id`;")
+    #         batch_op.drop_index("idx_dag_owner_attributes_dag_id")
+    #         batch_op.create_foreign_key("dag.dag_id", "dag", ["dag_id"], 
["dag_id"], ondelete="CASCADE")
+    #     else:
+    #         _handle_foreign_key_constraint_index_deletion(
+    #             batch_op, "dag.dag_id", "idx_dag_owner_attributes_dag_id", 
"dag_id"
+    #         )

Review Comment:
   I have commented this block temporarily to check if everything else runs 
fine in the CI.
   This block when uncommented is failing in CI however the same runs fine 
locally with breeze on all database backends MySQL, PostgresSQL and SQLite. It 
[fails](https://github.com/apache/airflow/actions/runs/9100399106/job/25015312491)
 saying that it cannot find the constraint `dag.dag_id` for dropping. However, 
when I check locally in the db, that table does have that constraint. 
   The diff for this when compared to other models/tables, is that when the 
ForeignKey is declared in the model, it is defined with name `dag.dag_id` (I 
mean with a period in between the name) and I'm guessing this is somehow 
affecting finding the constraint. I will investigate this further.



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