Skip to content

Determine latest Dag version by version_number, not created_at#68389

Open
ephraimbuddy wants to merge 2 commits into
apache:mainfrom
astronomer:fix-dag-version-latest-ordering
Open

Determine latest Dag version by version_number, not created_at#68389
ephraimbuddy wants to merge 2 commits into
apache:mainfrom
astronomer:fix-dag-version-latest-ordering

Conversation

@ephraimbuddy

Copy link
Copy Markdown
Contributor

The latest DagVersion was selected by created_at DESC, which
is not deterministic when two versions share a timestamp (or under clock skew).
write_dag derives the next version_number from that row, so picking a non-max row collided with the (dag_id, version_number) unique constraint. Order by the monotonic, unique version_number instead, consistently across get_latest_version/write_dag, get_version, and the bulk prefetch.


Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)
    Claude opus 4.8

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes nondeterministic “latest DagVersion” selection by ordering on version_number (monotonic/unique per dag_id) instead of created_at, preventing occasional collisions with the (dag_id, version_number) unique constraint when timestamps tie or clock skew occurs.

Changes:

  • Update DagVersion “latest” selection logic to order by version_number (including get_latest_version, get_version, and the internal select used by write_dag).
  • Update bulk prefetch logic in SerializedDagModel._prefetch_dag_write_metadata to align “latest” selection with DagVersion’s version_number.
  • Add regression tests that seed DagVersions where created_at ordering disagrees with version_number ordering.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
airflow-core/src/airflow/models/dag_version.py Switches “latest” DagVersion selection to deterministic version_number DESC ordering.
airflow-core/src/airflow/models/serialized_dag.py Aligns bulk prefetch window ordering with DagVersion selection by joining and ordering on DagVersion.version_number.
airflow-core/tests/unit/models/test_dag_version.py Adds regression coverage for inverted created_at vs version_number and verifies write_dag increments from max version.

@ephraimbuddy ephraimbuddy force-pushed the fix-dag-version-latest-ordering branch 3 times, most recently from 1c31871 to 2e34957 Compare June 11, 2026 15:51
The latest DagVersion was selected by created_at DESC, which
 is not deterministic when two versions share a timestamp (or under clock skew).
write_dag derives the next version_number from that row, so picking a non-max
row collided with the (dag_id, version_number) unique constraint.
Order by the monotonic, unique version_number instead, consistently
across get_latest_version/write_dag, get_version, and the bulk prefetch.
@ephraimbuddy ephraimbuddy force-pushed the fix-dag-version-latest-ordering branch from 2e34957 to 204c9e2 Compare June 11, 2026 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants