|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | + |
| 19 | +""" |
| 20 | +Add backward compatibility for serialized DAG format v3 to v2. |
| 21 | +
|
| 22 | +Revision ID: cc92b33c6709 |
| 23 | +Revises: eaf332f43c7c |
| 24 | +Create Date: 2025-09-22 22:50:48.035121 |
| 25 | +
|
| 26 | +""" |
| 27 | + |
| 28 | +from __future__ import annotations |
| 29 | + |
| 30 | +from textwrap import dedent |
| 31 | + |
| 32 | +import sqlalchemy as sa |
| 33 | +from alembic import context, op |
| 34 | + |
| 35 | +# revision identifiers, used by Alembic. |
| 36 | +revision = "cc92b33c6709" |
| 37 | +down_revision = "eaf332f43c7c" |
| 38 | +branch_labels = None |
| 39 | +depends_on = None |
| 40 | +airflow_version = "3.1.0" |
| 41 | + |
| 42 | + |
| 43 | +def upgrade(): |
| 44 | + """Apply Downgrade Serialized Dag version to v2.""" |
| 45 | + # No-op: Server handles v1/v2/v3 DAGs at runtime via conversion functions |
| 46 | + pass |
| 47 | + |
| 48 | + |
| 49 | +def downgrade(): |
| 50 | + """Convert v3 serialized DAGs back to v2 format for compatibility with older Airflow versions.""" |
| 51 | + if context.is_offline_mode(): |
| 52 | + print( |
| 53 | + dedent(""" |
| 54 | + ------------ |
| 55 | + -- Manual v3 to v2 DAG conversion required (offline mode) |
| 56 | + -- |
| 57 | + -- PostgreSQL: |
| 58 | + -- UPDATE serialized_dag SET data = jsonb_set((data::jsonb - 'client_defaults'), '{__version}', '2')::json |
| 59 | + -- WHERE id IN (SELECT id FROM serialized_dag WHERE data->>'__version' = '3' AND data_compressed IS NULL); |
| 60 | + -- |
| 61 | + -- MySQL/SQLite: |
| 62 | + -- UPDATE serialized_dag SET data = JSON_SET(JSON_REMOVE(data, '$.client_defaults'), '$.__version', 2) |
| 63 | + -- WHERE JSON_EXTRACT(data, '$.__version') = '3' AND data_compressed IS NULL; |
| 64 | + -- |
| 65 | + -- For compressed DAGs: run online migration. |
| 66 | + ------------ |
| 67 | + """) |
| 68 | + ) |
| 69 | + return |
| 70 | + |
| 71 | + import gzip |
| 72 | + import json |
| 73 | + |
| 74 | + connection = op.get_bind() |
| 75 | + dialect = connection.dialect.name |
| 76 | + |
| 77 | + if dialect == "postgresql": |
| 78 | + # PostgreSQL - pre-filter v3 DAGs to avoid parsing all rows |
| 79 | + connection.execute( |
| 80 | + sa.text(""" |
| 81 | + UPDATE serialized_dag |
| 82 | + SET data = jsonb_set( |
| 83 | + (data::jsonb - 'client_defaults'), |
| 84 | + '{__version}', |
| 85 | + '2' |
| 86 | + )::json |
| 87 | + WHERE id IN ( |
| 88 | + SELECT id FROM serialized_dag |
| 89 | + WHERE data->>'__version' = '3' |
| 90 | + AND data_compressed IS NULL |
| 91 | + ) |
| 92 | + """) |
| 93 | + ) |
| 94 | + elif dialect == "mysql": |
| 95 | + connection.execute( |
| 96 | + sa.text(""" |
| 97 | + UPDATE serialized_dag |
| 98 | + SET data = JSON_SET( |
| 99 | + JSON_REMOVE(data, '$.client_defaults'), |
| 100 | + '$.__version', |
| 101 | + 2 |
| 102 | + ) |
| 103 | + WHERE JSON_EXTRACT(data, '$.__version') = '3' |
| 104 | + AND data_compressed IS NULL |
| 105 | + """) |
| 106 | + ) |
| 107 | + else: |
| 108 | + json_functions_available = False |
| 109 | + try: |
| 110 | + connection.execute(sa.text("SELECT JSON_SET('{}', '$.test', 'value')")).fetchone() |
| 111 | + json_functions_available = True |
| 112 | + print("SQLite JSON functions detected, using optimized SQL approach") |
| 113 | + except Exception: |
| 114 | + print("SQLite JSON functions not available, using Python fallback for JSON processing") |
| 115 | + |
| 116 | + if json_functions_available: |
| 117 | + connection.execute( |
| 118 | + sa.text(""" |
| 119 | + UPDATE serialized_dag |
| 120 | + SET data = JSON_SET( |
| 121 | + JSON_REMOVE(data, '$.client_defaults'), |
| 122 | + '$.__version', |
| 123 | + 2 |
| 124 | + ) |
| 125 | + WHERE JSON_EXTRACT(data, '$.__version') = '3' |
| 126 | + AND data_compressed IS NULL |
| 127 | + """) |
| 128 | + ) |
| 129 | + else: |
| 130 | + result = connection.execute( |
| 131 | + sa.text(""" |
| 132 | + SELECT id, data |
| 133 | + FROM serialized_dag |
| 134 | + WHERE data_compressed IS NULL |
| 135 | + """) |
| 136 | + ) |
| 137 | + |
| 138 | + for row in result: |
| 139 | + dag_id, data_json = row |
| 140 | + try: |
| 141 | + if data_json is None: |
| 142 | + continue |
| 143 | + |
| 144 | + dag_data = json.loads(data_json) |
| 145 | + |
| 146 | + if dag_data.get("__version") != 3: |
| 147 | + continue |
| 148 | + |
| 149 | + if "client_defaults" in dag_data: |
| 150 | + del dag_data["client_defaults"] |
| 151 | + dag_data["__version"] = 2 |
| 152 | + |
| 153 | + new_json = json.dumps(dag_data) |
| 154 | + connection.execute( |
| 155 | + sa.text("UPDATE serialized_dag SET data = :data WHERE id = :id"), |
| 156 | + {"data": new_json, "id": dag_id}, |
| 157 | + ) |
| 158 | + |
| 159 | + except Exception as e: |
| 160 | + print(f"Failed to downgrade uncompressed DAG {dag_id}: {e}") |
| 161 | + continue |
| 162 | + try: |
| 163 | + result = connection.execute( |
| 164 | + sa.text(""" |
| 165 | + SELECT id, data_compressed |
| 166 | + FROM serialized_dag |
| 167 | + WHERE data_compressed IS NOT NULL |
| 168 | + """) |
| 169 | + ) |
| 170 | + |
| 171 | + for row in result: |
| 172 | + dag_id, compressed_data = row |
| 173 | + try: |
| 174 | + if compressed_data is None: |
| 175 | + continue |
| 176 | + |
| 177 | + decompressed = gzip.decompress(compressed_data) |
| 178 | + dag_data = json.loads(decompressed) |
| 179 | + |
| 180 | + if dag_data.get("__version") != 3: |
| 181 | + continue |
| 182 | + |
| 183 | + if "client_defaults" in dag_data: |
| 184 | + del dag_data["client_defaults"] |
| 185 | + dag_data["__version"] = 2 |
| 186 | + |
| 187 | + new_compressed = gzip.compress(json.dumps(dag_data).encode("utf-8")) |
| 188 | + connection.execute( |
| 189 | + sa.text("UPDATE serialized_dag SET data_compressed = :data WHERE id = :id"), |
| 190 | + {"data": new_compressed, "id": dag_id}, |
| 191 | + ) |
| 192 | + |
| 193 | + except Exception as e: |
| 194 | + print(f"Failed to downgrade compressed DAG {dag_id}: {e}") |
| 195 | + continue |
| 196 | + |
| 197 | + except Exception as e: |
| 198 | + print(f"Failed to process compressed DAGs during downgrade: {e}") |
| 199 | + raise |
0 commit comments