ephraimbuddy commented on code in PR #39638: URL: https://github.com/apache/airflow/pull/39638#discussion_r1602307427
########## 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: From the earlier failure, dropping foreignkeys should only be done for MySQL, right? I see that we drop it for others here as well but should be for MySQL only -- 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]
