forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(migrations): add 0027_2_10_3_fix_dag_schedule_dataset_alias_refer…
…ence_naming to fix constraint naming (apache#43373)
- Loading branch information
Showing
20 changed files
with
144 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
airflow/migrations/versions/0027_2_10_3_fix_dag_schedule_dataset_alias_reference_naming.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# | ||
# 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. | ||
|
||
""" | ||
Rename dag_schedule_dataset_alias_reference constraint names. | ||
Revision ID: 5f2621c13b39 | ||
Revises: 22ed7efa9da2 | ||
Create Date: 2024-10-25 04:03:33.002701 | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from alembic import op | ||
from sqlalchemy import inspect | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "5f2621c13b39" | ||
down_revision = "22ed7efa9da2" | ||
branch_labels = None | ||
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 upgrade(): | ||
"""Rename dag_schedule_dataset_alias_reference constraint.""" | ||
with op.batch_alter_table("dag_schedule_dataset_alias_reference", schema=None) as batch_op: | ||
bind = op.get_context().bind | ||
insp = inspect(bind) | ||
fk_constraints = [fk["name"] for fk in insp.get_foreign_keys("dag_schedule_dataset_alias_reference")] | ||
|
||
# "dsdar_dataset_alias_fkey" was the constraint name defined in the model while "dsdar_dataset_fkey" is the one | ||
# defined in the previous migration. | ||
# Rename this constraint name if user is using the name "dsdar_dataset_fkey". | ||
if "dsdar_dataset_fkey" in fk_constraints: | ||
_rename_fk_constraint( | ||
batch_op=batch_op, | ||
original_name="dsdar_dataset_fkey", | ||
new_name="dsdar_dataset_alias_fkey", | ||
referent_table="dataset_alias", | ||
local_cols=["alias_id"], | ||
remote_cols=["id"], | ||
ondelete="CASCADE", | ||
) | ||
|
||
# "dsdar_dag_fkey" was the constraint name defined in the model while "dsdar_dag_id_fkey" is the one | ||
# defined in the previous migration. | ||
# Rename this constraint name if user is using the name "dsdar_dag_fkey". | ||
if "dsdar_dag_fkey" in fk_constraints: | ||
_rename_fk_constraint( | ||
batch_op=batch_op, | ||
original_name="dsdar_dag_fkey", | ||
new_name="dsdar_dag_id_fkey", | ||
referent_table="dataset_alias", | ||
local_cols=["alias_id"], | ||
remote_cols=["id"], | ||
ondelete="CASCADE", | ||
) | ||
|
||
|
||
def downgrade(): | ||
"""Undo dag_schedule_dataset_alias_reference constraint rename.""" | ||
with op.batch_alter_table("dag_schedule_dataset_alias_reference", schema=None) as batch_op: | ||
bind = op.get_context().bind | ||
insp = inspect(bind) | ||
fk_constraints = [fk["name"] for fk in insp.get_foreign_keys("dag_schedule_dataset_alias_reference")] | ||
if "dsdar_dataset_alias_fkey" in fk_constraints: | ||
_rename_fk_constraint( | ||
batch_op=batch_op, | ||
original_name="dsdar_dataset_alias_fkey", | ||
new_name="dsdar_dataset_fkey", | ||
referent_table="dataset_alias", | ||
local_cols=["alias_id"], | ||
remote_cols=["id"], | ||
ondelete="CASCADE", | ||
) | ||
|
||
if "dsdar_dag_id_fkey" in fk_constraints: | ||
_rename_fk_constraint( | ||
batch_op=batch_op, | ||
original_name="dsdar_dag_id_fkey", | ||
new_name="dsdar_dag_fkey", | ||
referent_table="dataset_alias", | ||
local_cols=["alias_id"], | ||
remote_cols=["id"], | ||
ondelete="CASCADE", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
bda4fb36d2ac0f34e60a9969b6c1c1e6a98b555b0fc6d0e7bfcee9a89fb95fbf | ||
5067b6eac290220d23ce1bef6ce6d02478757f626f5a81a53ca2bba8e31c094c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters