-
Notifications
You must be signed in to change notification settings - Fork 13.9k
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
Remove redundant session.commit()
in migration
#18453
Conversation
The PR most likely needs to run full matrix of tests because it modifies parts of the core of Airflow. However, committers might decide to merge it quickly and take the risk. If they don't merge it quickly - please rebase it to the latest main at your convenience, or amend the last commit of the PR, and push it with --force-with-lease. |
Just one comment on that one to ask about some common approach here (and maybe an opportunity to get some useful knowledge sharing). I actually put that commit() deliberately there. I know it is not needed, simply for many years when dealing with the databases, I am used to set some explicit boundaries of database transactions (+ the provide_session kind of decorators which are there to pass an open session/transaction down the stack without closing it). So even if the operation does "nothing" between session.open() an exiting the scope of that session I usually mark it es explicit commit(). Especially when there are different branches you can take - and "return" like in this case (it could also be rollback() in this case), to clearly mark where the session is "closed". The benefit of that approach is that it is more future-proof. It's very easy to imagine (in general case) that someone in the future will add some modification of the database between session and exiting the function and might not notice that there is branch that might lead to "no-commit". Maybe that's not likely in this particular case, but for me it's more of "natural-habit". Whenever I see such situation that there is a branching and "commit()" in one branch and "nothing" in the other, it feels wrong - and I do not have to exercise extra mental power to understand whether this particular case is justified exception or not. For me it is pretty "natural" thing to do: open sessions() -> branch -> make sure the session is treated the same in all branches.. I'd love to hear your thoughts about it :) |
Valid point, I am discussing this right now with @ashb to get his take on it :) From my point of view, I have only used a commit with select if it is SELECT ... FOR UPDATE etc. |
Given the next statement is return, and thus that active Session will be closed, the commit here is entirely redundant -- the session will be cleaned up and any the transactions will be rolledback. So what this will actually do is:
|
Yep. I agree it's not needed, and It certainly works like that, so from performance point of view it is 'slightly` better to remove, and I am not at all against removing it, i just want to see if it bothers others from "reasoning" point of view. I even initially implemented it without the commit, but when I looked at the PR after I pushed it - my internal policeman told me "let's make it consistent" and I pushed a changed commit. But if it's just me who is bothered by that - I am perfectly OK to remove it. |
My head always goes to "this is python, we have de-facto/automatic RAII so we don't need it" :D |
Yah, I might have to teach my internal policeman a few new tricks. |
b77b991
to
751fd0f
Compare
`session.commit()` is redundant as we won't have anything to commit.
751fd0f
to
15b2f31
Compare
Failures are unrelated and already on main: https://github.com/apache/airflow/runs/3691456615#step:6:15250 Need to take a look at those |
Hi @kaxil , in some of the tests we run, we populate an empty sqlite meta data db by doing
This is happening because the test meta data db is empty. So the if condition here is True and the code just returns without committing the transaction.
Does anyone have any suggestions how to get around this error? |
@yuqian90 How can I reproduce it? I tried with an empty SQLite DB and didn't get an error. What versions of SQLite do you use?
|
Hi @kaxil, I have the exact same sqlite3 as you. Maybe it's not a problem with sqlite3. The error
Here's how to reproduce:
|
@yuqian90 Some of your requirements are different than the one in constraints file that might be a reason. Yours:
In Constraints:
|
OK I can confirm that |
Related to apache#18453 (comment) `1.5.0` was yanked so `>=1.5.1` is safe and we already have `1.7.5` in constraints-main
Related to #18453 (comment) `1.5.0` was yanked so `>=1.5.1` is safe and we already have `1.7.5` in constraints-main
Related to #18453 (comment) `1.5.0` was yanked so `>=1.5.1` is safe and we already have `1.7.5` in constraints-main (cherry picked from commit f99d0e7)
Related to apache/airflow#18453 (comment) `1.5.0` was yanked so `>=1.5.1` is safe and we already have `1.7.5` in constraints-main (cherry picked from commit f99d0e7066d2c00ff6581fd566a4d0a9826a2fc3) GitOrigin-RevId: 3a39702e6fc98a3b43b6d9506c8e1c4a3f0913d1
Related to apache/airflow#18453 (comment) `1.5.0` was yanked so `>=1.5.1` is safe and we already have `1.7.5` in constraints-main (cherry picked from commit f99d0e7066d2c00ff6581fd566a4d0a9826a2fc3) GitOrigin-RevId: 3a39702e6fc98a3b43b6d9506c8e1c4a3f0913d1
Related to apache/airflow#18453 (comment) `1.5.0` was yanked so `>=1.5.1` is safe and we already have `1.7.5` in constraints-main GitOrigin-RevId: f99d0e7066d2c00ff6581fd566a4d0a9826a2fc3
Related to apache/airflow#18453 (comment) `1.5.0` was yanked so `>=1.5.1` is safe and we already have `1.7.5` in constraints-main GitOrigin-RevId: f99d0e7066d2c00ff6581fd566a4d0a9826a2fc3
Related to apache/airflow#18453 (comment) `1.5.0` was yanked so `>=1.5.1` is safe and we already have `1.7.5` in constraints-main GitOrigin-RevId: f99d0e7066d2c00ff6581fd566a4d0a9826a2fc3
Related to apache/airflow#18453 (comment) `1.5.0` was yanked so `>=1.5.1` is safe and we already have `1.7.5` in constraints-main GitOrigin-RevId: f99d0e7066d2c00ff6581fd566a4d0a9826a2fc3
Related to apache/airflow#18453 (comment) `1.5.0` was yanked so `>=1.5.1` is safe and we already have `1.7.5` in constraints-main GitOrigin-RevId: f99d0e7066d2c00ff6581fd566a4d0a9826a2fc3
Related to apache/airflow#18453 (comment) `1.5.0` was yanked so `>=1.5.1` is safe and we already have `1.7.5` in constraints-main GitOrigin-RevId: f99d0e7066d2c00ff6581fd566a4d0a9826a2fc3
session.commit()
is redundant as we won't have anything to commit.^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code change, 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 UPDATING.md.