Skip to content

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Mar 5, 2019

Q A
Type bug
BC Break no
Fixed issues fixes #1344

Summary

This fixes an issue when embedding a document containing a named index multiple times. Since the index had a name, this caused an exception with MongoDB since the index would already exist when creating it for the second embed use (see test case). With this change we're prefixing indexes pulled up from embedded documents with the name of the property that's embedding them. While this can cause exceptions when an index name becomes too long, the current solution gives more control to the user.

One option would be to add an indexPrefix option to embedded relationships and using that instead of the field name. Since that would be a new feature, I will not include it in the bugfix for 1.2.x.

@alcaeus alcaeus force-pushed the fix-duplicate-embedded-index-name branch from ddb9c1b to 13f8015 Compare March 5, 2019 07:12
@alcaeus alcaeus requested a review from malarzm March 5, 2019 07:12
Copy link
Member

@malarzm malarzm left a comment

Choose a reason for hiding this comment

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

I've just realized we're not mentioning name at all in https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/1.2/reference/annotations-reference.html#index - could you please add it and note that indexes on embedded documents will have a prefix?

@alcaeus
Copy link
Member Author

alcaeus commented Mar 5, 2019

could you please add it and note that indexes on embedded documents will have a prefix?

Yeah, prefix should be documented regardless of previous docs. I'll add a chapter later today.

@alcaeus
Copy link
Member Author

alcaeus commented Mar 5, 2019

Documentation added, mind taking another look @malarzm?

@alcaeus alcaeus requested a review from malarzm March 5, 2019 15:20
Copy link
Member

@malarzm malarzm left a comment

Choose a reason for hiding this comment

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

It'd be great to add name to the list of optional attributes, but if you don't feel like it feel free to merge, it was missing before anyway 👍

@Steveb-p
Copy link
Contributor

Steveb-p commented Mar 5, 2019

From what I remember maximum index name length in MongoDB should be 125 characters. It isn't specified in MongoDB docs, but is mentioned multiple times in MongoDB issue tracker (https://jira.mongodb.org/browse/DOCS-1820 for example) and on some other sites. I'd say mention of this limit might be added to that documentation note?

ODM might also check for index name length in the future?

@alcaeus
Copy link
Member Author

alcaeus commented Mar 5, 2019

From what I remember maximum index name length in MongoDB should be 125 characters. It isn't specified in MongoDB docs

The index name length documentation explains this in more detail:

Fully qualified index names, which includes the namespace and the dot separators (i.e. ..$), cannot be longer than 128 characters.

We can check this at schema creation, but I believe any attempt to truncate index names can just cause issues down the road, so I prefer if the original exception was shown to the user. As mentioned previously, I believe it’s more sensible allowing users to configure this index prefix in future versions than trying to add more magic to the prefix.

@alcaeus
Copy link
Member Author

alcaeus commented Mar 5, 2019

It'd be great to add name to the list of optional attributes

I somehow completely missed the fact that the name wasn’t in the options. This should indeed be properly documented. 👍

@Steveb-p
Copy link
Contributor

Steveb-p commented Mar 5, 2019

My intention would not be to truncate or otherwise transform the index name, but rather prevent the attempt to create such an index or wrap exception with Doctrine's own one with some helpful message about how it can be resolved, and maybe what field it was about.

But that is wayyy out of scope :) Thank you @alcaeus

@alcaeus alcaeus force-pushed the fix-duplicate-embedded-index-name branch from 561a668 to 7d8e93e Compare March 6, 2019 06:05
@alcaeus
Copy link
Member Author

alcaeus commented Mar 6, 2019

@malarzm added a link to the indexes chapter to the annotation mapping, as the options are explained in more detail there. I've also added the note about index prefixing there so it won't get lost.

@Steveb-p let's fix the first issue - the reason I'm not really keen on doing too much with the exception is because either way, the user will be presented with a difficult problem: they have to either rename the index, the fields contained in the index path (as they are prefixed to the name), the collection name or even the database name.

Even without our exception, the error message provided by MongoDB will contain all necessary information:

namespace name generated from index name "doctrine_odm_tests.GH1344TooLongIndexName.$embedded1_this_is_a_really_long_name_that_will_cause_problems_for_whoever_tries_to_use_it_whether_in_an_embedded_field_or_not" is too long (127 byte max)

Regardless, I've added a test case to cover this case to make sure we're always giving users at least the index name.

@alcaeus alcaeus added this to the 1.2.7 milestone Mar 6, 2019
@alcaeus alcaeus force-pushed the fix-duplicate-embedded-index-name branch from 196c280 to ebfc7bb Compare March 6, 2019 06:39
@alcaeus alcaeus force-pushed the fix-duplicate-embedded-index-name branch from ebfc7bb to 52d47d3 Compare March 6, 2019 07:01
@alcaeus alcaeus merged commit 0cd3c37 into doctrine:1.2.x Mar 6, 2019
@alcaeus alcaeus deleted the fix-duplicate-embedded-index-name branch March 6, 2019 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants