fix(index): remove trailing underscore from auto-generated index names - #19
fix(index): remove trailing underscore from auto-generated index names#19Bardioc1977 wants to merge 1 commit into
Conversation
IndexDescription.fromMap() built auto-generated index names by appending
"_" after EVERY key entry instead of only BETWEEN entries, producing
"campaignNumber_1_" for a single-field index and
"campaignNumber_1_fileName_1_" for a two-field one -- both violate MongoDB's
own "<field>_<direction>" naming convention (joined by "_", no trailing
separator).
The practical consequence: on any database where the correctly-named index
already exists (e.g. created by an older Morphium version, or by MongoDB's
own auto-naming when no name was given), Morphium tries to create a
same-definition index under a different, wrongly-suffixed name. MongoDB
rejects that with "Error 85 - Index already exists with a different name",
Morphium only logs it as a warning and moves on, and the index -- including
any unique constraint from @Index(options = {"unique:true"}) -- is silently
never created. Writes that relied on that uniqueness then fail with
E11000 duplicate key errors referencing the never-created, wrongly-named
index.
Regression status: this is not a 6.3.0 regression. `git blame` traces the
trailing-underscore code to 2022-06-30 (commit f9e84d2), and it is
byte-for-byte identical in the v6.2.5 release tag. It has been silently
present for years; it only surfaces now because the reproduction needs an
existing database with an index that was already correctly named, which a
fresh database never has. So this is a plain bugfix, not a behavior change
-- no migration path is needed, since MongoDB will accept the fixed name
going forward and index CREATION was always the operation that failed, not
an existing index's definition or usage.
Checked for a second occurrence of the same name-building logic (item 3 in
the report): none found. Morphium#ensureIndicesFor and every other call site
(including the quarkus-morphium migration path) go through
IndexDescription.fromMaps()/fromMap(), so there is exactly one place that
needed fixing. Notably, InMemoryDriver's OWN index-name builder
(InMemoryDriver.java ~3227) already joins correctly ("if (b.length() > 0)
b.append('_')") and was never affected -- which is also why no existing
unit test caught this: every test exercising auto-naming through the
InMemDriver path saw correct names from that separate builder, never
IndexDescription's.
Regression tests added for the exact scenario from the report (single-field
"campaignNumber_1", multi-field "campaignNumber_1_fileName_1", and an
explicit-name case proving the auto-naming branch is still skipped when a
name is supplied). Mutation-proofed: temporarily restoring the old
unconditional trailing-underscore append reddens exactly the two new
auto-naming tests with the reported symptom ("expected: <...1_fileName_1>
but was: <...1_fileName_1_>"), leaving the pre-existing explicit-name tests
green; reverted after confirming.
Verified: morphium-core module installs clean, IndexDescriptionTest 5/5
green, all index-related suites in morphium-core (IndexMaintenanceTest,
InMemoryDriverIndexPlanningTest, CollectionIndexStoreTest, IndexKeyTest,
IndexPlannerTest, UniqueIndexTest, InMemUniqueIndexTest,
ListIndexesFidelityTest, DropIndexesCommandTest, ConnectionIndexTest) green,
morphium-jakarta-data 82/82 green.
Targeting the 6.3.0 line per the report, since 6.3.0-SNAPSHOT is already in
use by downstream consumers (e.g. datona-ota-authority, which is where this
was caught: 14/1794 tests failing with E11000 on an upgraded database).
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR fixes Morphium’s auto-generated MongoDB index name formatting by removing the trailing underscore that was previously appended after the last <field>_<direction> segment, aligning generated names with MongoDB’s standard naming convention and avoiding name-mismatch failures during index creation.
Changes:
- Adjust index name generation in
IndexDescription.fromMap(...)to only place"_"separators between key segments, not after the final segment. - Add regression tests covering single-field, multi-field, and explicit-name behaviors for index name generation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| morphium-core/src/main/java/de/caluga/morphium/IndexDescription.java | Fixes the auto-name builder to avoid trailing underscores in generated index names. |
| morphium-core/src/test/java/de/caluga/test/mongo/suite/base/IndexDescriptionTest.java | Adds regression tests to ensure generated names match MongoDB’s standard format and that explicit names are preserved. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Review PR ahead of the upstream fix against sboesebeck/morphium#develop. See commit message for full analysis (root cause, regression status, verification).