|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +""" |
| 19 | +Add dagrun_id foreign key to callback. |
| 20 | +
|
| 21 | +Revision ID: 0f4c9a2b8e1d |
| 22 | +Revises: e5a91c7f42b3 |
| 23 | +Create Date: 2026-08-06 00:00:00.000000 |
| 24 | +
|
| 25 | +""" |
| 26 | + |
| 27 | +from __future__ import annotations |
| 28 | + |
| 29 | +import json |
| 30 | + |
| 31 | +import sqlalchemy as sa |
| 32 | +from alembic import context, op |
| 33 | + |
| 34 | +from airflow.configuration import conf |
| 35 | + |
| 36 | +# revision identifiers, used by Alembic. |
| 37 | +revision = "0f4c9a2b8e1d" |
| 38 | +down_revision = "e5a91c7f42b3" |
| 39 | +branch_labels = None |
| 40 | +depends_on = None |
| 41 | +airflow_version = "3.4.0" |
| 42 | + |
| 43 | + |
| 44 | +def _deserialize_data(data): |
| 45 | + if isinstance(data, str): |
| 46 | + try: |
| 47 | + data = json.loads(data) |
| 48 | + except json.JSONDecodeError: |
| 49 | + return {} |
| 50 | + return data.get("__var", data) if isinstance(data, dict) else {} |
| 51 | + |
| 52 | + |
| 53 | +def _extract_dagrun_id(data) -> int | None: |
| 54 | + dagrun_id = _deserialize_data(data).get("dag_run_id") |
| 55 | + if dagrun_id is None: |
| 56 | + return None |
| 57 | + try: |
| 58 | + return int(dagrun_id) |
| 59 | + except (TypeError, ValueError): |
| 60 | + return None |
| 61 | + |
| 62 | + |
| 63 | +def _backfill_dagrun_id_from_deadline(conn, batch_size: int) -> None: |
| 64 | + callback = sa.table( |
| 65 | + "callback", |
| 66 | + sa.column("id", sa.Uuid()), |
| 67 | + sa.column("type", sa.String(20)), |
| 68 | + sa.column("dagrun_id", sa.Integer()), |
| 69 | + ) |
| 70 | + deadline = sa.table( |
| 71 | + "deadline", |
| 72 | + sa.column("callback_id", sa.Uuid()), |
| 73 | + sa.column("dagrun_id", sa.Integer()), |
| 74 | + ) |
| 75 | + dag_run = sa.table("dag_run", sa.column("id", sa.Integer())) |
| 76 | + |
| 77 | + last_id = None |
| 78 | + while True: |
| 79 | + query = ( |
| 80 | + sa.select(callback.c.id, deadline.c.dagrun_id) |
| 81 | + .select_from( |
| 82 | + callback.join(deadline, callback.c.id == deadline.c.callback_id).join( |
| 83 | + dag_run, deadline.c.dagrun_id == dag_run.c.id |
| 84 | + ) |
| 85 | + ) |
| 86 | + .where(callback.c.type == "executor", callback.c.dagrun_id.is_(None)) |
| 87 | + .order_by(callback.c.id) |
| 88 | + .limit(batch_size) |
| 89 | + ) |
| 90 | + if last_id is not None: |
| 91 | + query = query.where(callback.c.id > last_id) |
| 92 | + |
| 93 | + rows = conn.execute(query).fetchall() |
| 94 | + if not rows: |
| 95 | + return |
| 96 | + |
| 97 | + for callback_id, dagrun_id in rows: |
| 98 | + conn.execute(callback.update().where(callback.c.id == callback_id).values(dagrun_id=dagrun_id)) |
| 99 | + |
| 100 | + last_id = rows[-1].id |
| 101 | + |
| 102 | + |
| 103 | +def _backfill_dagrun_id(conn, batch_size: int) -> None: |
| 104 | + callback = sa.table( |
| 105 | + "callback", |
| 106 | + sa.column("id", sa.Uuid()), |
| 107 | + sa.column("type", sa.String(20)), |
| 108 | + sa.column("data", sa.Text()), |
| 109 | + sa.column("dagrun_id", sa.Integer()), |
| 110 | + ) |
| 111 | + dag_run = sa.table("dag_run", sa.column("id", sa.Integer())) |
| 112 | + |
| 113 | + last_id = None |
| 114 | + while True: |
| 115 | + query = ( |
| 116 | + sa.select(callback.c.id, callback.c.data) |
| 117 | + .where(callback.c.type == "executor", callback.c.dagrun_id.is_(None)) |
| 118 | + .order_by(callback.c.id) |
| 119 | + .limit(batch_size) |
| 120 | + ) |
| 121 | + if last_id is not None: |
| 122 | + query = query.where(callback.c.id > last_id) |
| 123 | + |
| 124 | + rows = conn.execute(query).fetchall() |
| 125 | + if not rows: |
| 126 | + return |
| 127 | + |
| 128 | + for callback_id, data in rows: |
| 129 | + dagrun_id = _extract_dagrun_id(data) |
| 130 | + if dagrun_id is None: |
| 131 | + continue |
| 132 | + |
| 133 | + dagrun_exists = conn.execute(sa.select(dag_run.c.id).where(dag_run.c.id == dagrun_id)).first() |
| 134 | + if dagrun_exists: |
| 135 | + conn.execute( |
| 136 | + callback.update().where(callback.c.id == callback_id).values(dagrun_id=dagrun_id) |
| 137 | + ) |
| 138 | + |
| 139 | + last_id = rows[-1].id |
| 140 | + |
| 141 | + |
| 142 | +def upgrade(): |
| 143 | + """Add callback.dagrun_id and backfill it from legacy callback.data.""" |
| 144 | + with op.batch_alter_table("callback") as batch_op: |
| 145 | + batch_op.add_column(sa.Column("dagrun_id", sa.Integer(), nullable=True)) |
| 146 | + |
| 147 | + if not context.is_offline_mode(): |
| 148 | + conn = op.get_bind() |
| 149 | + batch_size = conf.getint("database", "migration_batch_size") |
| 150 | + _backfill_dagrun_id_from_deadline(conn, batch_size) |
| 151 | + _backfill_dagrun_id(conn, batch_size) |
| 152 | + |
| 153 | + with op.batch_alter_table("callback") as batch_op: |
| 154 | + batch_op.create_index("callback_dagrun_id_idx", ["dagrun_id"], unique=False) |
| 155 | + batch_op.create_foreign_key( |
| 156 | + batch_op.f("callback_dagrun_id_fkey"), "dag_run", ["dagrun_id"], ["id"], ondelete="CASCADE" |
| 157 | + ) |
| 158 | + |
| 159 | + |
| 160 | +def downgrade(): |
| 161 | + """Remove callback.dagrun_id.""" |
| 162 | + with op.batch_alter_table("callback") as batch_op: |
| 163 | + batch_op.drop_constraint(batch_op.f("callback_dagrun_id_fkey"), type_="foreignkey") |
| 164 | + batch_op.drop_index("callback_dagrun_id_idx") |
| 165 | + batch_op.drop_column("dagrun_id") |
0 commit comments