pankajkoti commented on code in PR #39638: URL: https://github.com/apache/airflow/pull/39638#discussion_r1602642769
########## 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: > The problem is in your DDL, in postgres, there's no DROP FOREIGN KEY. It's a constraint. So you have to do alter table tablename drop constraint constraint_name better still you can use alembic's drop_contraint for all DB backends. batch_op.drop_constraint('name', type_="foreignkey") @ephraimbuddy no, that's not my DDL for postgres. And that's not even the DDL even for mysql for the other 5 tables below. If you review the PR files, we have the common code method that we're calling which in-fact uses alembic's drop_constraint ``` 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" ) ``` I added the DROP FOREIGN KEY for experimentation but only for mysql and only for one specific table `dag_owner_attributes`. On the same table `dag_owner_attributes`, we still call `_handle_foreign_key_constraint_index_deletion` for PostgreSQL and SQLite which leverages alembic's drop_constraint. The problem like I mentioned in my first [comment](https://github.com/apache/airflow/pull/39638#discussion_r1602152626) is that the DB backends in our CI are not able to find the constraint `dag.dag_id` for the `dag_owner_attributes` table. The CI is happy and able to find corresponding constraints for all the other 5 tables. -- 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-unsubscr...@airflow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org