LiamHecht commented on code in PR #75562:
URL: https://github.com/apache/airflow/pull/75562#discussion_r4371323585


##########
airflow-core/tests/unit/models/test_trigger.py:
##########
@@ -180,6 +180,65 @@ def test_clean_unused(session, dag_maker):
     assert {result.id for result in results} == {trigger1.id, trigger4.id, 
trigger5.id, trigger6.id}
 
 
[email protected]("reference_type", ["task-instance", "asset-watcher", 
"callback"])
+def test_clean_unused_rechecks_new_references(session, create_task_instance, 
reference_type):
+    """A trigger referenced after the unused ids are read must not be deleted 
(#72061)."""
+    trigger = Trigger(classpath="airflow.triggers.testing.SuccessTrigger", 
kwargs={})
+    session.add(trigger)
+    session.commit()
+    trigger_id = trigger.id
+
+    if reference_type == "task-instance":
+        task_instance = create_task_instance(
+            session=session, logical_date=timezone.utcnow(), 
state=State.DEFERRED
+        )
+        session.commit()
+        add_reference = (
+            update(TaskInstance).where(TaskInstance.id == 
task_instance.id).values(trigger_id=trigger_id)
+        )
+        reference_count = (
+            
select(func.count()).select_from(TaskInstance).where(TaskInstance.id == 
task_instance.id)
+        )
+    elif reference_type == "asset-watcher":
+        asset = AssetModel("race-test")
+        session.add(asset)
+        session.commit()
+        add_reference = insert(AssetWatcherModel).values(
+            name="race-test-watcher", asset_id=asset.id, trigger_id=trigger_id
+        )
+        reference_count = (
+            select(func.count())
+            .select_from(AssetWatcherModel)
+            .where(AssetWatcherModel.asset_id == asset.id, 
AssetWatcherModel.trigger_id == trigger_id)
+        )
+    else:
+        callback = 
TriggererCallback(callback_def=AsyncCallback("classpath.callback"))
+        session.add(callback)
+        session.commit()
+        add_reference = update(Callback).where(Callback.id == 
callback.id).values(trigger_id=trigger_id)
+        reference_count = 
select(func.count()).select_from(Callback).where(Callback.id == callback.id)
+
+    reference_added = False
+
+    def add_reference_before_delete(connection, cursor, statement, parameters, 
context, executemany):
+        nonlocal reference_added
+        normalized_statement = " ".join(statement.lower().replace('"', 
"").replace("`", "").split())
+        if not reference_added and "delete from trigger" in 
normalized_statement:
+            reference_added = True
+            connection.execute(add_reference)
+
+    event.listen(session.bind, "before_cursor_execute", 
add_reference_before_delete)

Review Comment:
   The bug only shows up when a reference is added between the SELECT that 
collects the ids and the DELETE, so the test has to act at that exact point. 
The `before_cursor_execute` hook is the most direct way I found to do that 
without mocking the session. This test is also taken from the one that #70961 
merged on `main`, so keeping the same approach makes the backport easier to 
compare. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to