Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion airflow-core/docs/img/airflow_erd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a8edcc8b63444f327ddea859988c23c87a1a722e2b6a430734e61518777d3e61
72b7bb2d4e109d8f786d229e68c83d2b6f7442e48a6617ea3d47842f1bfa33eb
2,965 changes: 1,494 additions & 1,471 deletions airflow-core/docs/img/airflow_erd.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion airflow-core/docs/migrations-ref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ Here's the list of all the Database Migrations that are executed via when you ru
+-------------------------+------------------+-------------------+--------------------------------------------------------------+
| Revision ID | Revises ID | Airflow Version | Description |
+=========================+==================+===================+==============================================================+
| ``66a7743fe20e`` (head) | ``583e80dfcef4`` | ``3.1.0`` | Add triggering user to dag_run. |
| ``ffdb0566c7c0`` (head) | ``66a7743fe20e`` | ``3.1.0`` | Add dag_favorite table. |
+-------------------------+------------------+-------------------+--------------------------------------------------------------+
| ``66a7743fe20e`` | ``583e80dfcef4`` | ``3.1.0`` | Add triggering user to dag_run. |
+-------------------------+------------------+-------------------+--------------------------------------------------------------+
| ``583e80dfcef4`` | ``3ac9e5732b1f`` | ``3.1.0`` | Add task_inlet_asset_reference table. |
+-------------------------+------------------+-------------------+--------------------------------------------------------------+
Expand Down
36 changes: 35 additions & 1 deletion airflow-core/src/airflow/api_fastapi/common/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
from fastapi import Depends, HTTPException, Query, status
from pendulum.parsing.exceptions import ParserError
from pydantic import AfterValidator, BaseModel, NonNegativeInt
from sqlalchemy import Column, and_, case, func, or_
from sqlalchemy import Column, and_, case, func, not_, or_, select
from sqlalchemy.inspection import inspect

from airflow.api_fastapi.core_api.base import OrmClause
from airflow.api_fastapi.core_api.security import GetUserDep
from airflow.models import Base
from airflow.models.asset import (
AssetAliasModel,
Expand All @@ -48,6 +49,7 @@
)
from airflow.models.connection import Connection
from airflow.models.dag import DagModel, DagTag
from airflow.models.dag_favorite import DagFavorite
from airflow.models.dag_version import DagVersion
from airflow.models.dagrun import DagRun
from airflow.models.pool import Pool
Expand Down Expand Up @@ -109,6 +111,37 @@ def depends(cls, offset: NonNegativeInt = 0) -> OffsetFilter:
return cls().set_value(offset)


class _FavoriteFilter(BaseParam[bool]):
"""Filter DAGs by favorite status."""

user_id: str

def to_orm(self, select_stmt: Select) -> Select:
if self.value is None and self.skip_none:
return select_stmt

if self.value:
select_stmt = select_stmt.join(DagFavorite, DagFavorite.dag_id == DagModel.dag_id).where(
DagFavorite.user_id == self.user_id
)
else:
select_stmt = select_stmt.where(
not_(
select(DagFavorite)
.where(and_(DagFavorite.dag_id == DagModel.dag_id, DagFavorite.user_id == self.user_id))
.exists()
)
)

return select_stmt

@classmethod
def depends(cls, user: GetUserDep, is_favorite: bool | None = Query(None)) -> _FavoriteFilter:
instance = cls().set_value(is_favorite)
instance.user_id = str(user.get_id())
return instance


class _ExcludeStaleFilter(BaseParam[bool]):
"""Filter on is_stale."""

Expand Down Expand Up @@ -526,6 +559,7 @@ def depends_float(
FilterParam[bool | None],
Depends(filter_param_factory(DagModel.is_paused, bool | None, filter_name="paused")),
]
QueryFavoriteFilter = Annotated[_FavoriteFilter, Depends(_FavoriteFilter.depends)]
QueryExcludeStaleFilter = Annotated[_ExcludeStaleFilter, Depends(_ExcludeStaleFilter.depends)]
QueryDagIdPatternSearch = Annotated[
_SearchParam, Depends(search_param_factory(DagModel.dag_id, "dag_id_pattern"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ paths:
type: string
default: dag_id
title: Order By
- name: is_favorite
in: query
required: false
schema:
anyOf:
- type: boolean
- type: 'null'
title: Is Favorite
responses:
'200':
description: Successful Response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2758,6 +2758,14 @@ paths:
type: string
default: dag_id
title: Order By
- name: is_favorite
in: query
required: false
schema:
anyOf:
- type: boolean
- type: 'null'
title: Is Favorite
responses:
'200':
description: Successful Response
Expand Down Expand Up @@ -3140,6 +3148,98 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
/api/v2/dags/{dag_id}/favorite:
post:
tags:
- DAG
summary: Favorite Dag
description: Mark the DAG as favorite.
operationId: favorite_dag
security:
- OAuth2PasswordBearer: []
parameters:
- name: dag_id
in: path
required: true
schema:
type: string
title: Dag Id
responses:
'204':
description: Successful Response
'401':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
description: Unauthorized
'403':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
description: Forbidden
'404':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
description: Not Found
'422':
description: Validation Error
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
/api/v2/dags/{dag_id}/unfavorite:
post:
tags:
- DAG
summary: Unfavorite Dag
description: Unmark the DAG as favorite.
operationId: unfavorite_dag
security:
- OAuth2PasswordBearer: []
parameters:
- name: dag_id
in: path
required: true
schema:
type: string
title: Dag Id
responses:
'204':
description: Successful Response
'401':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
description: Unauthorized
'403':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
description: Forbidden
'404':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
description: Not Found
'409':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
description: Conflict
'422':
description: Validation Error
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
/api/v2/eventLogs/{event_log_id}:
get:
tags:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from fastapi import Depends, HTTPException, Query, Response, status
from fastapi.exceptions import RequestValidationError
from pydantic import ValidationError
from sqlalchemy import select, update
from sqlalchemy import delete, insert, select, update

from airflow.api.common import delete_dag as delete_dag_module
from airflow.api_fastapi.common.dagbag import DagBagDep
Expand All @@ -38,6 +38,7 @@
QueryDagIdPatternSearch,
QueryDagIdPatternSearchWithNone,
QueryExcludeStaleFilter,
QueryFavoriteFilter,
QueryLastDagRunStateFilter,
QueryLimit,
QueryOffset,
Expand All @@ -60,12 +61,14 @@
from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc
from airflow.api_fastapi.core_api.security import (
EditableDagsFilterDep,
GetUserDep,
ReadableDagsFilterDep,
requires_access_dag,
)
from airflow.api_fastapi.logging.decorators import action_logging
from airflow.exceptions import AirflowException, DagNotFound
from airflow.models import DAG, DagModel
from airflow.models.dag_favorite import DagFavorite
from airflow.models.dagrun import DagRun

dags_router = AirflowRouter(tags=["DAG"], prefix="/dags")
Expand Down Expand Up @@ -113,6 +116,7 @@ def get_dags(
],
readable_dags_filter: ReadableDagsFilterDep,
session: SessionDep,
is_favorite: QueryFavoriteFilter,
) -> DAGCollectionResponse:
"""Get all DAGs."""
query = generate_dag_with_latest_run_query(
Expand All @@ -133,6 +137,7 @@ def get_dags(
dag_id_pattern,
dag_display_name_pattern,
tags,
is_favorite,
owners,
readable_dags_filter,
],
Expand Down Expand Up @@ -317,6 +322,54 @@ def patch_dags(
)


@dags_router.post(
"/{dag_id}/favorite",
status_code=status.HTTP_204_NO_CONTENT,
responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
dependencies=[Depends(requires_access_dag(method="GET")), Depends(action_logging())],
)
def favorite_dag(dag_id: str, session: SessionDep, user: GetUserDep):
"""Mark the DAG as favorite."""
dag = session.get(DagModel, dag_id)
if not dag:
raise HTTPException(status.HTTP_404_NOT_FOUND, detail=f"DAG with id '{dag_id}' not found")

user_id = str(user.get_id())
session.execute(insert(DagFavorite).values(dag_id=dag_id, user_id=user_id))


@dags_router.post(
"/{dag_id}/unfavorite",
status_code=status.HTTP_204_NO_CONTENT,
responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND, status.HTTP_409_CONFLICT]),
dependencies=[Depends(requires_access_dag(method="GET")), Depends(action_logging())],
)
def unfavorite_dag(dag_id: str, session: SessionDep, user: GetUserDep):
"""Unmark the DAG as favorite."""
dag = session.get(DagModel, dag_id)
if not dag:
raise HTTPException(status.HTTP_404_NOT_FOUND, detail=f"DAG with id '{dag_id}' not found")

user_id = str(user.get_id())

favorite_exists = session.execute(
select(DagFavorite).where(
DagFavorite.dag_id == dag_id,
DagFavorite.user_id == user_id,
)
).first()

if not favorite_exists:
raise HTTPException(status.HTTP_409_CONFLICT, detail="DAG is not marked as favorite")

session.execute(
delete(DagFavorite).where(
DagFavorite.dag_id == dag_id,
DagFavorite.user_id == user_id,
)
)


@dags_router.delete(
"/{dag_id}",
responses=create_openapi_http_exception_doc(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
QueryDagDisplayNamePatternSearch,
QueryDagIdPatternSearch,
QueryExcludeStaleFilter,
QueryFavoriteFilter,
QueryLastDagRunStateFilter,
QueryLimit,
QueryOffset,
Expand Down Expand Up @@ -92,6 +93,7 @@ def get_dags(
).dynamic_depends()
),
],
is_favorite: QueryFavoriteFilter,
readable_dags_filter: ReadableDagsFilterDep,
session: SessionDep,
dag_runs_limit: int = 10,
Expand All @@ -116,6 +118,7 @@ def get_dags(
tags,
owners,
last_dag_run_state,
is_favorite,
readable_dags_filter,
],
order_by=order_by,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#
# 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.

"""
Add dag_favorite table.

Revision ID: ffdb0566c7c0
Revises: 66a7743fe20e
Create Date: 2025-06-05 15:06:08.903908

"""

from __future__ import annotations

import sqlalchemy as sa
from alembic import op

from airflow.models.base import COLLATION_ARGS

revision = "ffdb0566c7c0"
down_revision = "66a7743fe20e"
branch_labels = None
depends_on = None
airflow_version = "3.1.0"


def upgrade():
"""Apply add dag_favorite table."""
op.create_table(
"dag_favorite",
sa.Column("user_id", sa.String(length=250), nullable=False),
sa.Column("dag_id", sa.String(length=250, **COLLATION_ARGS), nullable=False),
sa.ForeignKeyConstraint(
["dag_id"], ["dag.dag_id"], name=op.f("dag_favorite_dag_id_fkey"), ondelete="CASCADE"
),
sa.PrimaryKeyConstraint("user_id", "dag_id", name=op.f("dag_favorite_pkey")),
)


def downgrade():
"""Unapply add dag_favorite table."""
op.drop_table("dag_favorite")
Loading