Skip to content
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

[4.x] use mongodb driver to check for dirtiness #1990

Closed
wants to merge 1 commit into from

Conversation

halaei
Copy link
Contributor

@halaei halaei commented Mar 6, 2020

Reopening of #1664

To check if a field is dirty we can simply use fromPHP() function of the mongodb driver. Besides, we shouldn't use the SQL-specific logic of Illuminate\Eloquent\Model::originalIsEquivalent() for mongodb. In sql, fields keep their schema data types and changing integer 1 to string '1' actually does nothing. However, in mongodb fields accept their types as they are inserted/updated. Therefore, if we change value of a field from (int)1 to (string)'1' we actually must regard it as dirty. This change is backward incompatible. Don't know if anyone likes the current behaviour more.
--

@jenssegers
Copy link
Contributor

Codecov Report

Merging #1990 into develop will decrease coverage by 0.08%.
The diff coverage is 33.33%.

Impacted file tree graph

@@              Coverage Diff              @@
##             develop    #1990      +/-   ##
=============================================
- Coverage      82.80%   82.72%   -0.09%     
+ Complexity       531      526       -5     
=============================================
  Files             27       27              
  Lines           1210     1204       -6     
=============================================
- Hits            1002      996       -6     
  Misses           208      208              

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b379bd3...9e7866a. Read the comment docs.

@codecov-io
Copy link

codecov-io commented Mar 6, 2020

Codecov Report

Merging #1990 into develop will decrease coverage by 0.08%.
The diff coverage is 33.33%.

Impacted file tree graph

@@              Coverage Diff              @@
##             develop    #1990      +/-   ##
=============================================
- Coverage       82.8%   82.72%   -0.09%     
+ Complexity       531      526       -5     
=============================================
  Files             27       27              
  Lines           1210     1204       -6     
=============================================
- Hits            1002      996       -6     
  Misses           208      208
Impacted Files Coverage Δ Complexity Δ
src/Jenssegers/Mongodb/Eloquent/Model.php 80.98% <33.33%> (-0.78%) 75 <0> (-5)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b379bd3...9e7866a. Read the comment docs.

Copy link
Contributor

@divine divine left a comment

Choose a reason for hiding this comment

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

@halaei first of all, good work! I think we need to add a test with casted attribute to check if it's working correctly as expected.

return $current == $original;
}

if ($this->hasCast($key)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The only problem I see is a casted attributes, probably we need to check after keys was casted back to their values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@divine Looking at the code, I don't see it is required to check for casts. The input values passed by getDirty() are uncasted:

    public function getDirty()
    {
        $dirty = [];

        foreach ($this->getAttributes() as $key => $value) {
            if (! $this->originalIsEquivalent($key, $value)) {
                $dirty[$key] = $value;
            }
        }

        return $dirty;
    }
    public function getAttributes()
    {
        return $this->attributes;
    }

Besides, I don't see why one should use Laravel <=6 casting system for MongoDB, I don't see a test for it in the repository either. Laravel 7 custom casts are a different story. I hope they won't make things go wrong. I've already had my custom cast implementation along side this PR in Larave 5.8 production without an issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, we're dropping support for laravel 6 (no other way to drop object id auto-casting) and this branch will be focused for versions > 7.0, so that's the reason why I've asked this question.

This might be an issue with Laravel 7 when it wouldn't check dirtiness for casted objects.

Yes, there are no tests, I think a lot of tests are missing, might take a look myself (for tests).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@divine Then I should send this PR on top of your own PR #1988 ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably no, it's not ready yet, still have something to do. I will let you know, ok?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@divine I have already done some checks and coding for Laravel 7. Sounds like casting in Laravel 7 also doesn't break things.
I'll send you that PR anyway. I'll wait for you to let me work on my unset-field PR #1667 after this is done.

@halaei
Copy link
Contributor Author

halaei commented Mar 6, 2020

@jenssegers Nice coverage tool. But I feel I decreased the source code complexity in terms of both lines of code and branches. I also added new tests. How come the coverage is decreased? I think the report is somehow wrong.

@halaei
Copy link
Contributor Author

halaei commented Mar 6, 2020

I see the report now. I didn't test for when exception is thrown. I don't quite remember. Maybe it has to do with my next pull request regarding unset field #1667

public function testGetDirty(): void
{
$ids = [
new ObjectId(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Add value for ObjectId please

$item->nullable = null;
$item->new_val = 'new';
$item->number = '4';
$item->ids = [
Copy link
Contributor

@Smolevich Smolevich Mar 6, 2020

Choose a reason for hiding this comment

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

Looks like strange, i suggest to replace on

$item->ids = $ids;

or

$item->ids = [
    new ObjectId('576c25db6118fd406e6e6471'),
    new ObjectId('576c25db6118fd406e6e6472')
];

@divine
Copy link
Contributor

divine commented Feb 19, 2023

Closing in favor of #2515 after 3 years...

Thank you @halaei !

@divine divine closed this Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants