Skip to content

Commit

Permalink
Add limit 1 if required first value from query result (#33672)
Browse files Browse the repository at this point in the history
  • Loading branch information
Taragolis authored Aug 24, 2023
1 parent 370348a commit e8ba579
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
10 changes: 5 additions & 5 deletions airflow/auth/managers/fab/security_manager/modules/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from flask_appbuilder import const
from flask_appbuilder.models.sqla import Base
from sqlalchemy import func, inspect
from sqlalchemy import func, inspect, select
from sqlalchemy.exc import MultipleResultsFound
from werkzeug.security import generate_password_hash

Expand Down Expand Up @@ -257,10 +257,10 @@ def find_user(self, username=None, email=None):
return None

def find_register_user(self, registration_hash):
return (
self.get_session.query(self.registeruser_model)
.filter(self.registeruser_model.registration_hash == registration_hash)
.scalar()
return self.get_session.scalar(
select(self.registeruser_mode)
.where(self.registeruser_model.registration_hash == registration_hash)
.limit(1)
)

def update_user(self, user):
Expand Down
5 changes: 3 additions & 2 deletions airflow/models/dagrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ def get_previous_dagrun(
]
if state is not None:
filters.append(DagRun.state == state)
return session.scalar(select(DagRun).where(*filters).order_by(DagRun.execution_date.desc()))
return session.scalar(select(DagRun).where(*filters).order_by(DagRun.execution_date.desc()).limit(1))

@provide_session
def get_previous_scheduled_dagrun(self, session: Session = NEW_SESSION) -> DagRun | None:
Expand All @@ -543,6 +543,7 @@ def get_previous_scheduled_dagrun(self, session: Session = NEW_SESSION) -> DagRu
DagRun.run_type != DagRunType.MANUAL,
)
.order_by(DagRun.execution_date.desc())
.limit(1)
)

def _tis_for_dagrun_state(self, *, dag, tis):
Expand Down Expand Up @@ -1387,7 +1388,7 @@ def schedule_tis(
@provide_session
def get_log_template(self, *, session: Session = NEW_SESSION) -> LogTemplate:
if self.log_template_id is None: # DagRun created before LogTemplate introduction.
template = session.scalar(select(LogTemplate).order_by(LogTemplate.id))
template = session.scalar(select(LogTemplate).order_by(LogTemplate.id).limit(1))
else:
template = session.get(LogTemplate, self.log_template_id)
if template is None:
Expand Down

0 comments on commit e8ba579

Please sign in to comment.