Skip to content

Commit

Permalink
simplify the data interval handling
Browse files Browse the repository at this point in the history
  • Loading branch information
karakanb committed Jan 23, 2024
1 parent 6342881 commit b13fb3f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
15 changes: 8 additions & 7 deletions airflow/api_connexion/endpoints/dag_run_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,20 +338,21 @@ def post_dag_run(*, dag_id: str, session: Session = NEW_SESSION) -> APIResponse:
try:
dag = get_airflow_app().dag_bag.get_dag(dag_id)

interval = None
if post_body.get("data_interval_start") and post_body.get("data_interval_end"):
interval = DataInterval(
start=pendulum.instance(post_body["data_interval_start"]),
end=pendulum.instance(post_body["data_interval_end"]),
data_interval_start = post_body.get("data_interval_start")
data_interval_end = post_body.get("data_interval_end")
if data_interval_start and data_interval_end:
data_interval = DataInterval(
start=pendulum.instance(data_interval_start),
end=pendulum.instance(data_interval_end),
)
else:
interval = dag.timetable.infer_manual_data_interval(run_after=logical_date)
data_interval = dag.timetable.infer_manual_data_interval(run_after=logical_date)

dag_run = dag.create_dagrun(
run_type=DagRunType.MANUAL,
run_id=run_id,
execution_date=logical_date,
data_interval=interval,
data_interval=data_interval,
state=DagRunState.QUEUED,
conf=post_body.get("conf"),
external_trigger=True,
Expand Down
6 changes: 2 additions & 4 deletions airflow/api_connexion/schemas/dag_run_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,9 @@ def validate_data_interval_dates(self, data, **kwargs):
data_interval_start_exists = data.get("data_interval_start") is not None
data_interval_end_exists = data.get("data_interval_end") is not None

if (data_interval_start_exists and not data_interval_end_exists) or (
data_interval_end_exists and not data_interval_start_exists
):
if data_interval_start_exists != data_interval_end_exists:
raise ValidationError(
"Both 'data_interval_start' and 'data_interval_end' must be specified, you cannot specify only one"
"Both 'data_interval_start' and 'data_interval_end' must be specified together"
)


Expand Down
4 changes: 2 additions & 2 deletions tests/api_connexion/endpoints/test_dag_run_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -1373,12 +1373,12 @@ def test_should_response_400_for_conflicting_execution_date_logical_date(self):
(
"2020-11-10T08:25:56.939143+00:00",
None,
"{'_schema': [\"Both 'data_interval_start' and 'data_interval_end' must be specified, you cannot specify only one\"]}",
"{'_schema': [\"Both 'data_interval_start' and 'data_interval_end' must be specified together\"]}",
),
(
None,
"2020-11-10T08:25:56.939143+00:00",
"{'_schema': [\"Both 'data_interval_start' and 'data_interval_end' must be specified, you cannot specify only one\"]}",
"{'_schema': [\"Both 'data_interval_start' and 'data_interval_end' must be specified together\"]}",
),
],
)
Expand Down

0 comments on commit b13fb3f

Please sign in to comment.