Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up unused triggers in a single query for all dialects except MySQL #38663

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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