-
Notifications
You must be signed in to change notification settings - Fork 14k
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: Ensure Flask framework leverages the Flask-SQLAlchemy session (Phase I) #26200
refactor: Ensure Flask framework leverages the Flask-SQLAlchemy session (Phase I) #26200
Conversation
def get_user_by_username( | ||
self, username: str, session: Session = None | ||
) -> Optional[User]: | ||
def get_user_by_username(self, username: str) -> Optional[User]: |
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.
This method was never called with the session
parameter.
@@ -152,8 +153,7 @@ def log_with_context( # pylint: disable=too-many-locals | |||
# need to add them back before logging to capture user_id | |||
if user_id is None: | |||
try: | |||
session = current_app.appbuilder.get_session | |||
session.add(g.user) | |||
db.session.add(g.user) |
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.
Same session as current_app.appbuilder.get_session
. Consistency is key here.
@@ -106,7 +106,6 @@ def test_get_datasource_sqlatable(session_with_data: Session) -> None: | |||
result = DatasourceDAO.get_datasource( | |||
datasource_type=DatasourceType.TABLE, | |||
datasource_id=1, | |||
session=session_with_data, |
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.
The db.session
is mocked here and is included as a fixture to the test.
@@ -45,15 +44,14 @@ class DatasourceDAO(BaseDAO[Datasource]): | |||
@classmethod | |||
def get_datasource( | |||
cls, | |||
session: Session, |
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.
It is not apparent to me why the session
was included here. There are unit tests which use a session other than db.session
but, per here, the global db.session
is mocked to use said session.
0205b85
to
b37c7cf
Compare
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## master #26200 +/- ##
==========================================
- Coverage 69.08% 66.87% -2.22%
==========================================
Files 1931 1931
Lines 75351 75333 -18
Branches 8429 8429
==========================================
- Hits 52056 50376 -1680
- Misses 21148 22810 +1662
Partials 2147 2147
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
5486cd6
to
490aeff
Compare
) | ||
session.add(dashboard) | ||
session = sqla.inspect(target).session | ||
new_user = session.query(User).filter_by(id=target.id).first() |
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.
Same logic without the try
block.
@john-bodley I think it would be great to label this PR with v4.0 and merge it during the breaking window to reuse the test/stabilization efforts that will occur during that period. |
490aeff
to
1a61b1e
Compare
print(f"- {model.__name__} ({rows} rows in table {model.__tablename__})") | ||
model_rows[model] = rows | ||
session.close() |
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.
There is no need to explicitly close the session as this is handled by Flask-SQLAlchemy when the session is torn down—be that at the end of request or when a script/shell terminates.
Regarding,
as discussed with @michael-s-molina, though this is technically a non-breaking change, it seems prudent (from a safety perspective) to hold off merging this until the v4.0 breaking window. |
1a61b1e
to
6b0eb40
Compare
@michael-s-molina would you mind reviewing this PR? |
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.
LGTM
SUMMARY
Somewhat related to #26186 (though for non-Celery related workflows) this PR ensures that the remainder of the codebase (excluding tests and Alembic migrations) leverage the Flask-SQLAlchemy session (
db.session
).This refactor helps to reduce the code complexity and ensures we're not generating any rouge sessions which are often not explicitly closed and can lead to connection pooling issues.
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
TESTING INSTRUCTIONS
CI.
ADDITIONAL INFORMATION