Skip to content

Commit

Permalink
refactor(api_connextion): use session.row_count to decide whether to …
Browse files Browse the repository at this point in the history
…return NotFound for queueEvent batch delete
  • Loading branch information
Lee-W committed Feb 7, 2024
1 parent 576f9de commit a9ae4c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
38 changes: 17 additions & 21 deletions airflow/api_connexion/endpoints/dataset_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,15 @@ def delete_dag_dataset_queue_events(
) -> APIResponse:
"""Delete queued Dataset events for a DAG."""
where_clauses = _build_get_ddrqs_where_clause(dag_id=dag_id, before=before)
query = select(DatasetDagRunQueue).where(*where_clauses)
ddrqs = session.scalars(query).all()
if not ddrqs:
raise NotFound(
"Queue event not found",
detail=f"Queue event with dag_id: `{dag_id}` was not found",
)
delete_stmt = delete(DatasetDagRunQueue).where(*where_clauses)
session.execute(delete_stmt)
return NoContent, HTTPStatus.NO_CONTENT
if where_clauses:
delete_stmt = delete(DatasetDagRunQueue).where(*where_clauses)
s = session.execute(delete_stmt)
if s.rowcount:
return NoContent, HTTPStatus.NO_CONTENT
raise NotFound(
"Queue event not found",
detail=f"Queue event with dag_id: `{dag_id}` was not found",
)


def _build_get_dataset_ddrqs_where_clause(uri: str, session: Session, before: str | None = None):
Expand Down Expand Up @@ -266,15 +265,12 @@ def delete_dataset_queue_events(
) -> APIResponse:
"""Delete queued Dataset events for a Dataset"""
where_clauses = _build_get_dataset_ddrqs_where_clause(uri=uri, session=session, before=before)
ddrqs = None
if where_clauses:
query = select(DatasetDagRunQueue).where(*where_clauses)
ddrqs = session.scalars(query).all()
if not where_clauses or not ddrqs:
raise NotFound(
"Queue event not found",
detail=f"Queue event with dataset uri: `{uri}` was not found",
)
delete_stmt = delete(DatasetDagRunQueue).where(*where_clauses)
session.execute(delete_stmt)
return NoContent, HTTPStatus.NO_CONTENT
delete_stmt = delete(DatasetDagRunQueue).where(*where_clauses)
s = session.execute(delete_stmt)
if s.rowcount:
return NoContent, HTTPStatus.NO_CONTENT
raise NotFound(
"Queue event not found",
detail=f"Queue event with dataset uri: `{uri}` was not found",
)
2 changes: 1 addition & 1 deletion tests/api_connexion/endpoints/test_dataset_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ def test_should_respond_404(self):

assert response.status_code == 404
assert {
"detail": "Queue event with datast_uri: `not_exists` was not found",
"detail": "Queue event with dataset uri: `not_exists` was not found",
"status": 404,
"title": "Queue event not found",
"type": EXCEPTIONS_LINK_MAP[404],
Expand Down

0 comments on commit a9ae4c2

Please sign in to comment.