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

Clarify how associative arrays as document values should be updated. #17

Closed
internalsystemerror opened this issue Sep 27, 2020 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@internalsystemerror
Copy link
Contributor

internalsystemerror commented Sep 27, 2020

Assuming the following:

        $this->documentStore->updateDoc(
            $collection,
            $event->get(Payload::USER_ID),
            [
                Payload::USER_VALIDATIONS   => [
                    $event->get(Payload::USER_EMAIL_ADDRESS) => $event->createdAt(),
                ],
            ],
        );

The InMemoryDocumentStore would merge the Payload::USER_VALIDATIONS arrays recursively, adding additional values. This provides no way of removing a nested key. There is even a test to enforce this behaviour: https://github.com/event-engine/php-document-store/blob/1e980c8e8e3f7b48428740e109692440dffa3a2b/tests/InMemoryDocumentStoreTest.php#L88-112

Further, the event-engine/php-postgres-document-store handles this differently, where merges are NOT recursive: https://github.com/event-engine/php-postgres-document-store/blob/e9721e21f42a648a79eb9865b5656c6949bf1c42/src/PostgresDocumentStore.php#L366

For more on how the || operator is used: https://www.postgresql.org/docs/13/functions-json.html

I have submitted the following PRs for changes:

@codeliner
Copy link
Contributor

@sandrokeil @martin-schilling @heiglandreas @jsor @sblum I consider this a bugfix and would release a v0.6.1 on next Tuesday. What do you think? It might effect some test cases, but that's even a good thing, because it won't work with Postgres anyway.

@codeliner
Copy link
Contributor

Thank you for investigation and the PRs @internalsystemerror 👏

@martin-schilling
Copy link
Contributor

martin-schilling commented Sep 27, 2020

Just for completeness sake, I think I still remember the reason arrayReplaceRecursiveAssocOnly was added. In a previous commit, array_merge had been replaced by array_replace_recursive which led to changed behavior and failing tests. The arrayReplaceRecursiveAssocOnly introduced here was a compromise between the two methods. Working with PHP arrays can really be a mess.
Anyway, I'm not entirely sure why we didn't notice that Postgres behaves differently here but I feel like the InMemoryDocumentStore should behave similarly to the PostgresDocumentStore.

Just out of curiosity though, how do other DBs behave? It might be worth checking how this works in other popular DBs like MongoDB to make sure that the most suitable procedure can be chosen. Afterwards this behavior should probably be defined and documented so that we don't have this problem in the future anymore.

Thanks for the PR!

@codeliner
Copy link
Contributor

Good points @martin-schilling . I ask @sandrokeil tomorrow regarding MongoDB.

@codeliner
Copy link
Contributor

MongoDB has a $set operator and it behaves similar to Postgres. I'm going to merge the PRs now.

@codeliner
Copy link
Contributor

Fixed with #18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants