You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "Lee-W (via GitHub)" <gi...@apache.org> on 2026/05/04 06:31:06 UTC

Re: [PR] ci: add migration round-trip check for SQLite FK cascade regressions [airflow]

Lee-W commented on code in PR #65547:
URL: https://github.com/apache/airflow/pull/65547#discussion_r3167330001


##########
scripts/in_container/run_migration_round_trip.py:
##########
@@ -0,0 +1,336 @@
+#!/usr/bin/env python
+# 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.
+"""
+Migration round-trip regression check — intended to run inside Breeze.
+
+Walks every revision base → head (upgrade direction) and head → base
+(downgrade direction) one rev at a time, on a single fresh SQLite DB
+with ``PRAGMA foreign_keys = ON``.  Before each step the seed fixture is
+idempotently restored so any FK chain a rebuild needs to fire is in
+place.
+
+Per-migration walking is essential.  ``airflow_db.upgradedb`` /
+``airflow_db.downgrade`` reconfigures Airflow's engine via
+``_single_connection_pool`` for every call, so each migration starts
+on a freshly-connected SQLAlchemy engine where ``setup_event_handlers``
+fires ``PRAGMA foreign_keys=ON``.  In contrast a single-call
+``upgradedb(to_revision=tip)`` walks all migrations on one shared
+connection: once an earlier migration's ``disable_sqlite_fkeys`` flips
+FK off on that connection, the off state simply persists into later
+migrations, silently masking broken or missing wrappers downstream.
+
+The placement convention every migration must follow (only DML breaks
+PRAGMA, but the safe rule is strictly stronger) is documented in
+``contributing-docs/26_migration_round_trip_check.rst``.
+
+For the placement convention every migration must follow and the FK
+chains exercised by the seed, see
+``contributing-docs/26_migration_round_trip_check.rst``.
+"""
+
+from __future__ import annotations
+
+import os
+import sys
+import tempfile
+from pathlib import Path
+from typing import Any
+
+from alembic.script import ScriptDirectory
+from rich.console import Console
+from rich.panel import Panel
+from rich.rule import Rule
+from sqlalchemy import text
+
+console = Console(width=140, color_system="standard", highlight=False)
+
+SEED_DAG_ID = "round_trip_dag"
+SEED_DAG_VERSION_ID = "abcdef0123456789abcdef0123456789"
+SEED_BUNDLE_NAME = "dags-folder"
+SEED_RUN_ID = "manual_run_1"
+SEED_TASK_ID = "task1"
+SEED_TASK_INSTANCE_ID = "fedcba9876543210fedcba9876543210"
+
+# Hardcoded INSERT values for tables we seed.  Each entry maps column name
+# to a SQL literal.  When a column is not present in the live schema (e.g.
+# an older revision predating its addition), it is dropped from the INSERT
+# — provided every NOT-NULL-no-default column at that revision is covered.
+# When a future migration introduces a brand-new NOT NULL column without a
+# default, ``_insert_or_skip`` raises with a clear message pointing at the
+# table that needs an entry here.
+SEED_VALUES: dict[str, dict[str, str]] = {
+    "log_template": {
+        "id": "1",
+        "filename": "'{{ ti.dag_id }}/{{ ts }}.log'",
+        "elasticsearch_id": "'{{ ti.dag_id }}-{{ ts }}'",
+        "created_at": "'2024-01-01 00:00:00.000000+00:00'",
+    },
+    "dag_bundle": {
+        "name": f"'{SEED_BUNDLE_NAME}'",
+        "active": "1",
+        "version": "NULL",
+        "last_refreshed": "NULL",
+        "signed_url_template": "NULL",
+        "url_template": "NULL",
+        "bundle_template_params": "NULL",
+    },
+    "dag": {
+        "dag_id": f"'{SEED_DAG_ID}'",
+        "max_active_tasks": "16",
+        "has_task_concurrency_limits": "0",
+        "is_paused": "0",
+        "is_active": "1",
+        "is_stale": "0",
+        "fileloc": "'/tmp/round_trip_dag.py'",
+        "fail_fast": "0",
+        "max_consecutive_failed_dag_runs": "0",
+        "bundle_name": f"'{SEED_BUNDLE_NAME}'",
+        "has_import_errors": "0",
+        "max_active_runs": "16",
+        "_default_view": "'grid'",
+        "exceeds_max_non_backfill": "0",
+        "timetable_type": "'cron'",
+        "timetable_partitioned": "0",
+        "timetable_periodic": "0",
+    },
+    "dag_version": {
+        "id": f"'{SEED_DAG_VERSION_ID}'",
+        "version_number": "1",
+        "dag_id": f"'{SEED_DAG_ID}'",
+        "bundle_name": f"'{SEED_BUNDLE_NAME}'",
+        "bundle_version": "NULL",
+        "created_at": "'2024-01-01 00:00:00+00:00'",
+        "last_updated": "'2024-01-01 00:00:00+00:00'",
+    },
+    "dag_run": {
+        "id": "1",
+        "dag_id": f"'{SEED_DAG_ID}'",
+        "execution_date": "'2024-01-01 00:00:00.000000+00:00'",
+        "logical_date": "'2024-01-01 00:00:00.000000+00:00'",
+        "run_id": f"'{SEED_RUN_ID}'",
+        "run_type": "'manual'",
+        "state": "'success'",
+        "log_template_id": "1",
+        "run_after": "'2024-01-01 00:00:00+00:00'",
+        "span_status": "'not_started'",
+        "clear_number": "0",
+    },
+    "task_instance": {
+        "id": f"'{SEED_TASK_INSTANCE_ID}'",
+        "task_id": f"'{SEED_TASK_ID}'",
+        "dag_id": f"'{SEED_DAG_ID}'",
+        "run_id": f"'{SEED_RUN_ID}'",
+        "map_index": "-1",
+        "pool": "'default'",
+        "pool_slots": "1",
+        "state": "'success'",
+        "try_number": "1",
+        "max_tries": "0",
+    },
+}
+
+# FK-dependency-respecting insertion order.
+SEED_ORDER = ("log_template", "dag_bundle", "dag", "dag_version", "dag_run", "task_instance")
+
+
+def _make_fresh_db() -> Path:
+    """Create a fresh SQLite file and re-point Airflow's engine at it."""
+    fd, path = tempfile.mkstemp(prefix="round_trip_", suffix=".db")
+    os.close(fd)
+    db = Path(path)
+    db.unlink()  # Airflow creates the file on first connect
+
+    new_conn = f"sqlite:///{db}"
+    os.environ["AIRFLOW__DATABASE__SQL_ALCHEMY_CONN"] = new_conn
+
+    from airflow import settings
+
+    settings.SQL_ALCHEMY_CONN = new_conn
+    settings.dispose_orm()
+    settings.configure_orm()
+    return db

Review Comment:
   it's intentional



-- 
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-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org