-
Notifications
You must be signed in to change notification settings - Fork 14.4k
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
rusackas
merged 6 commits into
apache:master
from
sandeep-patel11:query_sync_role_definition
Oct 11, 2023
Merged
refactor: Issue #25040; Refactored sync_role_definition function in order to reduce number of query. #25340
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
929affd
modified sync role definition to decrease number of query
sandeep-patel11 615d7df
lint issue fixed
sandeep-patel11 ea08931
type of pvms changed, code formatted
sandeep-patel11 da17439
pre-commit hooks passed
sandeep-patel11 a8ca0ce
new function for fetching all pvms
sandeep-patel11 d25fa01
improved code
sandeep-patel11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ | |
from jwt.api_jwt import _jwt_global_obj | ||
from sqlalchemy import and_, inspect, or_ | ||
from sqlalchemy.engine.base import Connection | ||
from sqlalchemy.orm import Session | ||
from sqlalchemy.orm import Session, eagerload | ||
from sqlalchemy.orm.mapper import Mapper | ||
from sqlalchemy.orm.query import Query as SqlaQuery | ||
|
||
|
@@ -736,7 +736,9 @@ 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).all(): | ||
for pv in self.get_session.query(self.permissionview_model).options( | ||
eagerload(self.permissionview_model.permission), | ||
eagerload(self.permissionview_model.view_menu)).all(): | ||
if pv.permission and pv.view_menu: | ||
all_pvs.add((pv.permission.name, pv.view_menu.name)) | ||
|
||
|
@@ -784,11 +786,17 @@ 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] | ||
|
||
# Creating default roles | ||
self.set_role("Admin", self._is_admin_pvm) | ||
self.set_role("Alpha", self._is_alpha_pvm) | ||
self.set_role("Gamma", self._is_gamma_pvm) | ||
self.set_role("sql_lab", self._is_sql_lab_pvm) | ||
self.set_role("Admin", self._is_admin_pvm, pvms) | ||
self.set_role("Alpha", self._is_alpha_pvm, pvms) | ||
self.set_role("Gamma", self._is_gamma_pvm, pvms) | ||
self.set_role("sql_lab", self._is_sql_lab_pvm, pvms) | ||
|
||
# Configure public role | ||
if current_app.config["PUBLIC_ROLE_LIKE"]: | ||
|
@@ -864,7 +872,7 @@ def copy_role( | |
self.get_session.commit() | ||
|
||
def set_role( | ||
self, role_name: str, pvm_check: Callable[[PermissionView], bool] | ||
self, role_name: str, pvm_check: Callable[[PermissionView], bool], pvms:[] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct. Changes Done. |
||
) -> None: | ||
""" | ||
Set the FAB permission/views for the role. | ||
|
@@ -874,8 +882,6 @@ def set_role( | |
""" | ||
|
||
logger.info("Syncing %s perms", role_name) | ||
pvms = self.get_session.query(PermissionView).all() | ||
pvms = [p for p in pvms if p.permission and p.view_menu] | ||
role = self.add_role(role_name) | ||
role_pvms = [ | ||
permission_view for permission_view in pvms if pvm_check(permission_view) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this check do anything? These are required relations, aren't they?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this is required relation but as this condition was written in previous query I thought of not removing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah, I see you haven't changed this. Fair enough.