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

refactor: Issue #25040; Refactored sync_role_definition function in order to reduce number of query. #25340

Merged
merged 6 commits into from
Oct 11, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions superset/security/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,14 +736,7 @@ def create_missing_perms(self) -> None:

logger.info("Fetching a set of all perms to lookup which ones are missing")
all_pvs = set()
for pv in (
self.get_session.query(self.permissionview_model)
.options(
eagerload(self.permissionview_model.permission),
eagerload(self.permissionview_model.view_menu),
)
.all()
):
for pv in self._get_all_pvms():
if pv.permission and pv.view_menu:
all_pvs.add((pv.permission.name, pv.view_menu.name))

Expand Down Expand Up @@ -791,16 +784,7 @@ def sync_role_definitions(self) -> None:

self.create_custom_permissions()

# Fetch all pvms
pvms = (
self.get_session.query(PermissionView)
.options(
eagerload(PermissionView.permission),
eagerload(PermissionView.view_menu),
)
.all()
)
pvms = [p for p in pvms if p.permission and p.view_menu]
pvms = self._get_all_pvms()

# Creating default roles
self.set_role("Admin", self._is_admin_pvm, pvms)
Expand All @@ -822,6 +806,19 @@ def sync_role_definitions(self) -> None:
self.get_session.commit()
self.clean_perms()

def _get_all_pvms(self) -> list[PermissionView]:
# Fetch all pvms
pvms = (
self.get_session.query(self.permissionview_model)
.options(
eagerload(self.permissionview_model.permission),
eagerload(self.permissionview_model.view_menu),
)
.all()
)
pvms = [p for p in pvms if p.permission and p.view_menu]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pedantic comment but you could just return this line instead of updating the var and returning the var.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, Thanks for the the reviews @giftig .

return pvms

def _get_pvms_from_builtin_role(self, role_name: str) -> list[PermissionView]:
"""
Gets a list of model PermissionView permissions inferred from a builtin role
Expand Down