-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Fix SQLite date arithmetic for dynamic intervals #5073
Fix SQLite date arithmetic for dynamic intervals #5073
Conversation
Fixes #4426, #4462. I am really only personally concerned about handling seconds/minutes/hours correctly, but figured it made sense to try and fix weeks and quarters as well since I'm in the area. I'm open to reverting those changes or instead simply asserting if `$unit` is WEEK or QUARTER and the $interval is not numeric, so at least clear error messages are produced. It looks like #3019 added a test case to `DataAccessTest` to check that date arithmetic with a column reference worked. That change also enabled the interval to be parameterized, so I also added test cases to ensure that behavior keeps working, for all intervals.
PHPStan/Psalm are complaining that Looking through the test failures --
I'd like to see dynamic intervals supported going forward, ideally across all backends (and I believe that is reasonably straightforward; most backends seem like they could be reworked to move the multiplication out of PHP and into SQL). I'm also open to scoping the tests to just SQLite/MySQL/MariaDB, since those are the platforms I use and which work (with this PR, at least). I'm not sure what to do about the type annotation in this case, though. |
@Russ-ctl, thanks for the contribution. As far as I can tell, this is not a bug, and the Platform API up until ~4.0.0 expects the interval to be a number rather than an SQL expression. For instance, dbal/src/Platforms/AbstractPlatform.php Lines 1263 to 1273 in e88ce6d
This issue may have been partially or fully resolved in
It is fine if you want to get it addressed in a version sooner than I have a few comments about the tests and the implementation but we need to address the above question first. |
Yes, this was confusing to me, since #3019 was accepted despite that API contract; I figured since that was accepted a fix for the other intervals might also be :)
🎉 Aha, I had not checked the 4.x branch to see if the issue was fixed there, just checked 2.x and 3.x. Skimming the 4.x branch now, it looks like the approach there is much more robust, as are the tests, which I'm glad to see. Unfortunately, my team is still on PHP 7 and we're still using doctrine/dbal 2.10 at the moment, so it will likely be a while before we are able to get up to 4.0. How much work do you expect would be involved in putting a fix together for 3.x, given the API contract expects the interval to be an int? |
There's no plan to release 4.0 yet, so you'll have to upgrade at least to
No idea. Code-wise, it should be straightforward but guarding against potential BC breaks may require extra caution. |
We're still on 7.2 unfortunately.
Alright, so it sounds like it's not completely trivial. We're using a workaround in our own codebase, so we're not blocked for now, and it seems like it's probably fixed already in the 4.x branch. So, I'm good with closing this if you are. |
I am. Closing it. |
Summary
Changes
SqlitePlatform::getDateArithmeticIntervalExpression
to make it consistently support dynamic intervals for any supportedDateIntervalUnit
. Also adds a test similar toDataAccessTest::testDateArithmetics
that tests all of thegetDate(Add|Sub)(Unit)Expression
with a named parameter instead of a static integer.To support weeks and quarters with dynamic intervals, I had to put the multiplication into the generated SQL. If this feels like maybe too much, I could instead have those cases throw if the value
!is_numeric
; that's still BC (since it doesn't work today) while providing a much clearer error message. (I only need seconds to work, for my project!)First time contributing to a Doctrine project, so let me know if I've based the PR on the right branch!