Repository: incubator-airflow Updated Branches: refs/heads/master b586bd612 -> b2b9587cc
[AIRFLOW-1017] get_task_instance shouldn't throw exception when no TI get_task_instance should return None instead of throwing exception in the case where dagrun does not have the task instance. Closes #2178 from aoen/ddavydov-- one_instead_of_first_for_dagrun Project: http://git-wip-us.apache.org/repos/asf/incubator-airflow/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-airflow/commit/b2b9587c Tree: http://git-wip-us.apache.org/repos/asf/incubator-airflow/tree/b2b9587c Diff: http://git-wip-us.apache.org/repos/asf/incubator-airflow/diff/b2b9587c Branch: refs/heads/master Commit: b2b9587cca9195229ab107394ad94b7702c70e37 Parents: b586bd6 Author: Dan Davydov <dan.davy...@airbnb.com> Authored: Wed Mar 22 15:24:57 2017 -0700 Committer: Dan Davydov <dan.davy...@airbnb.com> Committed: Wed Mar 22 15:25:04 2017 -0700 ---------------------------------------------------------------------- airflow/models.py | 2 +- tests/models.py | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-airflow/blob/b2b9587c/airflow/models.py ---------------------------------------------------------------------- diff --git a/airflow/models.py b/airflow/models.py index a7d2916..df23de2 100755 --- a/airflow/models.py +++ b/airflow/models.py @@ -4017,7 +4017,7 @@ class DagRun(Base): TI.dag_id == self.dag_id, TI.execution_date == self.execution_date, TI.task_id == task_id - ).one() + ).first() return ti http://git-wip-us.apache.org/repos/asf/incubator-airflow/blob/b2b9587c/tests/models.py ---------------------------------------------------------------------- diff --git a/tests/models.py b/tests/models.py index 8ce08eb..dcba354 100644 --- a/tests/models.py +++ b/tests/models.py @@ -310,6 +310,32 @@ class DagRunTest(unittest.TestCase): state = dr.update_state() self.assertEqual(State.FAILED, state) + def test_get_task_instance_on_empty_dagrun(self): + """ + Make sure that a proper value is returned when a dagrun has no task instances + """ + session = settings.Session() + + # Any dag will work for this + dag = self.dagbag.get_dag('test_dagrun_short_circuit_false') + now = datetime.datetime.now() + + # Don't use create_dagrun since it will create the task instances too which we + # don't want + dag_run = models.DagRun( + dag_id=dag.dag_id, + run_id='manual__' + now.isoformat(), + execution_date=now, + start_date=now, + state=State.RUNNING, + external_trigger=False, + ) + session.add(dag_run) + session.commit() + + ti = dag_run.get_task_instance('test_short_circuit_false') + self.assertEqual(None, ti) + class DagBagTest(unittest.TestCase):