-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Add pre-commit To Prevent Usage of session.query #47275
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
base: main
Are you sure you want to change the base?
Add pre-commit To Prevent Usage of session.query #47275
Conversation
I will raise a PR soon for removing |
5d4d4dc
to
b6775a8
Compare
.pre-commit-config.yaml
Outdated
language: python | ||
additional_dependencies: ['rich>=12.4.4'] | ||
files: ^airflow.*\.py$|^task_sdk.*\.py | ||
exclude: ^tests/.*\.py$|^task_sdk/tests/.*\.py$ |
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.
exclude: ^tests/.*\.py$|^task_sdk/tests/.*\.py$ |
Since we have given it specific files to check, do we still need to exclude others?
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.
Ya you are right ! I'll remove this line
You can do it in this PR. This PR should raise and point to where there are usages of session.query |
f"\nSQLAlchemy 2.0 deprecates the `Query` object" | ||
f"use the `select()` construct instead." | ||
) | ||
errors += 1 |
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.
Thanks for the changes! We don't need this integer since occurrence isn't important in return and response text. We could make a return bool and the main can still return one similar to below. This can also do an early exit and reduce scans since a single occurrence should trigger the hook. What do you think?
return 1 if check_session_query(ast_module) else 0
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.
ya ! you are right !!
I'll update it soon
e9bcf8e
to
1c65940
Compare
airflow/models/xcom.py
Outdated
|
||
for xcom in query: | ||
for xcom in session.execute(query).all(): |
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.
I am not sure if this works fine for session.delete(xcom).
Could you please clarify when you get some free time?
1c65940
to
98be988
Compare
I have been trying to update the code but couldn't could you please help me upgrade this ? |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions. |
98be988
to
376a2dc
Compare
376a2dc
to
7f7cc6c
Compare
@@ -270,7 +270,7 @@ def set_xcom( | |||
if not run_id: | |||
raise HTTPException(status.HTTP_404_NOT_FOUND, f"Run with ID: `{run_id}` was not found") | |||
|
|||
dag_run_id = session.query(DagRun.id).filter_by(dag_id=dag_id, run_id=run_id).scalar() | |||
dag_run_id = session.execute(DagRun.id).where(dag_id=dag_id, run_id=run_id).scalar() |
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.
dag_run_id = session.execute(DagRun.id).where(dag_id=dag_id, run_id=run_id).scalar() | |
dag_run_id = session.scalar(select(DagRun.id).where(dag_id=dag_id, run_id=run_id)) |
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.
Done!
active_bundle_names = set(self._bundle_config.keys()) | ||
for name in active_bundle_names: | ||
stored = {b.name: b for b in session.scalars(select(DagBundleModel)).all()} | ||
for name in self._bundle_config.keys(): |
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.
Looks like you are omitting the active_bundle_names filter?
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 was a mistake. Rebased again and resolved
updated_rows = session.execute( | ||
update(DagVersion) | ||
.where(DagVersion.bundle_name.in_(inactive_bundle_names)) | ||
.values({DagVersion.bundle_name: new_bundle_name}) |
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.
.values({DagVersion.bundle_name: new_bundle_name}) | |
.values(bundle_name=new_bundle_name) |
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 change has been updated so I rebased and resolved it
"filename": filename, | ||
"bundle_name": bundle_name, | ||
"timestamp": utcnow(), | ||
"stacktrace": stacktrace, |
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.
values accept keyword args not dictionary
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 change has been updated, so I rebased and resolved it
…nnecessary exclude files in pre-commit yaml
7f7cc6c
to
e37f259
Compare
Instead of trying to fix everything in this PR, it might be easier to submit smaller PRs fixing things one by one, and rebase this repeatedly after each PR is merged until CI is green. |
Thanks for the suggestion—I'll proceed with this plan. |
Yep - it could also be done similar to the #52020
|
Appreciate your approach to this; I'm jumping in headfirst |
I've created a list as you suggested Total changes needed files : 116
My Plan : @potiuk Would love to hear your thoughts on this. |
Sure. Also you might split per module in "core". I think what works best is if you do one or two yourself - some representative examples of PRs - and provide instructions to others based on thsose PRs. That avoids any ambiguities with description and speeds up people ramping up |
closes : #45461
Introduce a pre-commit hook to prevent the use of session.query in the core Airflow code.
This is limited to the source code and excludes the tests/ package.
Removed session.query() and updated a new style
^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named
{pr_number}.significant.rst
or{issue_number}.significant.rst
, in newsfragments.