Skip to content

PHPORM-60 Fix Query on whereDate, whereDay, whereMonth, whereYear #2572

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

Merged
merged 7 commits into from
Aug 25, 2023

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Aug 22, 2023

Fix PHPORM-60

Created from #2376

So basically, current whereDate, whereDay, whereMonth and whereYear is query on different column with specific need (like whereDate you need column that has value YYYY-MM-DD etc) using basic comparison.

As we all know that isnt how whereDate works on sql, this PR will generate query using built in $dayOfMonth, $month, $year on whereDay, whereMonth, and whereYear, while whereDate basically generate range date(since mongodb doesnt have date() equivalent like on sql).

  • whereDate uses $lt & $gt comparison with UTCDateTime objects
  • whereDay uses $dayOfWeek
  • whereMonth uses $month
  • whereYear uses $year
  • whereTime uses $dateToString to format into HH:MM:SS, HH:MM or HH (not performance optimized)

@codecov-commenter
Copy link

codecov-commenter commented Aug 22, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.26% 🎉

Comparison is base (af13eda) 90.57% compared to head (0c525c4) 90.83%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #2572      +/-   ##
============================================
+ Coverage     90.57%   90.83%   +0.26%     
- Complexity      748      752       +4     
============================================
  Files            34       34              
  Lines          1783     1834      +51     
============================================
+ Hits           1615     1666      +51     
  Misses          168      168              
Files Changed Coverage Δ
src/Query/Builder.php 94.51% <100.00%> (+0.58%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

CHANGELOG.md Outdated
@@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file.
- Remove `Query\Builder::whereAll($column, $values)`. Use `Query\Builder::where($column, 'all', $values)` instead. [#16](https://github.com/GromNaN/laravel-mongodb-private/pull/16) by [@GromNaN](https://github.com/GromNaN).
- Fix validation of unique values when the validated value is found as part of an existing value. [#21](https://github.com/GromNaN/laravel-mongodb-private/pull/21) by [@GromNaN](https://github.com/GromNaN).
- Support `%` and `_` in `like` expression [#17](https://github.com/GromNaN/laravel-mongodb-private/pull/17) by [@GromNaN](https://github.com/GromNaN).
- Fix Query on `whereDate`, `whereDay`, `whereMonth`, `whereYear`, `whereTime` to use MongoDB operators [#2376](https://github.com/jenssegers/laravel-mongodb/pull/2376) by [@Davpyu](https://github.com/Davpyu)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed some of the earlier changelog entries refer to your old URL. For instance, https://github.com/GromNaN/laravel-mongodb-private/pull/16 redirects to GromNaN/laravel-mongodb#16. I assume your archived fork will remain public for the forseeable future but would it make more sense to replace most of these links with references to PHPORM tickets, which might be more permanent?

This is outside the scope of this PR, so I'm happy to defer this until we're closer to tagging a release with this changelog.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

],
],
'ne' => [
'$or' => [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we continue to conversation about segfaults from GromNaN/laravel-mongodb#28 (comment)?

You wrote:

I tested this alternative, but got segfaults.

'ne' => ['$not' => [
    $column => [
        '$gte' => $startOfDay,
        '$lte' => $endOfDay,
    ],
]],

My response:

Segfaults in mongod (executing the query) or PHP (executing this match construct)?

I'm trying to think about how this would trigger a segfault in PHP. The only thing I can think of is that $column is referenced at multiple levels of the same return value. If so, that seems like something you might be able to isolate in a PHPT test. I'd be happy to dive into it further if so.

If this is actually a mongod segfault, I assume it'd be reproducible in the shell and would warrant a new SERVER ticket.

Over video, you mentioned that the segfault was in PHP after receiving an error message from the server. So if you can extract this to a reproducible failing PHPUnit test that'd be a good starting point for me to look into.

Copy link
Member Author

@GromNaN GromNaN Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use the $not format proposed (and fixed).

The segfault occurs when the query in invalid and the server return an error message.
Checkout this branch : https://github.com/gromnan/laravel-mongodb-fork/tree/PHPORM-60-segfault

./vendor/bin/phpunit tests/QueryTest.php --filter testWhereDate
PHPUnit 9.6.11 by Sebastian Bergmann and contributors.

segmentation fault  ./vendor/bin/phpunit tests/QueryTest.php --filter testWhereDate

The error event was:

unknown top level operator: $not. If you are trying to negate an entire expression, use $nor.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GromNaN: What version of PHP, ext-mongodb, and MongoDB were you running when you observed a segfault? I'm not seeing it with PHP 8.2.3, ext-mongodb 1.16.2, and MongoDB 6.0.5:

$ ./vendor/bin/phpunit tests/QueryTest.php --filter testWhereDate
PHPUnit 9.6.11 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 00:00.228, Memory: 24.00 MB

There was 1 error:

1) Jenssegers\Mongodb\Tests\QueryTest::testWhereDate
MongoDB\Driver\Exception\CommandException: unknown top level operator: $not. If you are trying to negate an entire expression, use $nor.

/home/jmikola/workspace/mongodb/laravel-mongodb-fork/vendor/mongodb/mongodb/src/Operation/Find.php:314
/home/jmikola/workspace/mongodb/laravel-mongodb-fork/vendor/mongodb/mongodb/src/Collection.php:644
/home/jmikola/workspace/mongodb/laravel-mongodb-fork/src/Collection.php:48
/home/jmikola/workspace/mongodb/laravel-mongodb-fork/src/Query/Builder.php:420
/home/jmikola/workspace/mongodb/laravel-mongodb-fork/src/Query/Builder.php:211
/home/jmikola/workspace/mongodb/laravel-mongodb-fork/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:735
/home/jmikola/workspace/mongodb/laravel-mongodb-fork/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:719
/home/jmikola/workspace/mongodb/laravel-mongodb-fork/tests/QueryTest.php:204
/home/jmikola/workspace/mongodb/laravel-mongodb-fork/vendor/orchestra/testbench-core/src/PHPUnit/TestCase.php:42

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

[
'$dateToString' => ['date' => '$'.$where['column'], 'format' => '%H:%M:%S'],
],
$where['value'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We talked about validating user input. Should we enforce a string in the format HH:MM:SS and permit trailing components to be omitted (e.g. allow HH:MM or even just HH)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with support for various time formats.

@GromNaN GromNaN force-pushed the PHPORM-60 branch 2 times, most recently from 4f1e8b2 to 317ba9f Compare August 22, 2023 18:09
@GromNaN GromNaN requested a review from jmikola August 22, 2023 18:28
Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code changes LGTM.

We can take the segfault discussion over to Slack if you like, since it seems unrelated to this PR.


$where['operator'] = $operator;
$where['value'] = $value;
$format = match (count($matches)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever use of count($matches).

@alcaeus alcaeus merged commit f330412 into mongodb:master Aug 25, 2023
@GromNaN GromNaN deleted the PHPORM-60 branch August 25, 2023 13:01
@GromNaN GromNaN added this to the 4.0.0 milestone Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants