uranusjr commented on code in PR #32883:
URL: https://github.com/apache/airflow/pull/32883#discussion_r1298073537
##########
airflow/www/security.py:
##########
@@ -578,22 +576,22 @@ def _get_all_non_dag_permissions(self) -> dict[tuple[str,
str], Permission]:
return {
(action_name, resource_name): viewmodel
for action_name, resource_name, viewmodel in (
- self.appbuilder.get_session.query(self.permission_model)
- .join(self.permission_model.action)
- .join(self.permission_model.resource)
-
.filter(~self.resource_model.name.like(f"{permissions.RESOURCE_DAG_PREFIX}%"))
- .with_entities(self.action_model.name,
self.resource_model.name, self.permission_model)
- .all()
+ self.appbuilder.get_session.execute(
+ select(self.action_model.name, self.resource_model.name,
self.permission_model)
+ .join(self.permission_model.action)
+ .join(self.permission_model.resource)
+
.where(~self.resource_model.name.like(f"{permissions.RESOURCE_DAG_PREFIX}%"))
+ ).all()
)
}
def _get_all_roles_with_permissions(self) -> dict[str, Role]:
"""Returns a dict with a key of role name and value of role with early
loaded permissions."""
return {
r.name: r
- for r in
self.appbuilder.get_session.query(self.role_model).options(
- joinedload(self.role_model.permissions)
- )
+ for r in self.appbuilder.get_session.scalars(
+
select(self.role_model).options(joinedload(self.role_model.permissions))
+ ).unique()
Review Comment:
Looks like the query API automatically deduplicates on primary key:
https://docs.sqlalchemy.org/en/20/faq/sessions.html#my-query-does-not-return-the-same-number-of-objects-as-query-count-tells-me-why
> The Query object, when asked to return a list of ORM-mapped objects, will
deduplicate the objects based on primary key.
For this particular purpose `unique()` is fine since duplicated rows would
have the same hash, but alternatively `unique(lambda r: r.id)` is probably both
more accurate and slightly faster.
--
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]