Skip to content

Commit

Permalink
Clean up unused triggers in a single query for all dialects except My…
Browse files Browse the repository at this point in the history
…SQL (#38663)

(cherry picked from commit 12d38e1)
  • Loading branch information
hussein-awala authored and ephraimbuddy committed Apr 2, 2024
1 parent 90dc2cc commit 4260f56
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions airflow/models/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,16 @@ def clean_unused(cls, session: Session = NEW_SESSION) -> None:
.values(trigger_id=None)
)

# Get all triggers that have no task instances depending on them...
ids = session.scalars(
# Get all triggers that have no task instances depending on them and delete them
ids = (
select(cls.id)
.join(TaskInstance, cls.id == TaskInstance.trigger_id, isouter=True)
.group_by(cls.id)
.having(func.count(TaskInstance.trigger_id) == 0)
).all()
# ...and delete them (we can't do this in one query due to MySQL)
)
if session.bind.dialect.name == "mysql":
# MySQL doesn't support DELETE with JOIN, so we need to do it in two steps
ids = session.scalars(ids).all()
session.execute(
delete(Trigger).where(Trigger.id.in_(ids)).execution_options(synchronize_session=False)
)
Expand Down

0 comments on commit 4260f56

Please sign in to comment.