Support TTL indexes in the GORM-Mongo mapping DSL and reconcile index option changes#15747
Open
codeconsole wants to merge 2 commits into
Open
Support TTL indexes in the GORM-Mongo mapping DSL and reconcile index option changes#15747codeconsole wants to merge 2 commits into
codeconsole wants to merge 2 commits into
Conversation
… option changes indexAttributes: [expireAfterSeconds: N] now materialises a MongoDB TTL index. The driver only exposes the two-argument IndexOptions.expireAfter, which MongoConstants.mapToObject cannot reach (it only invokes single-argument setters), so expireAfterSeconds is pulled out of the attributes and applied explicitly. All three index paths in MongoDatastore.initializeIndices (declared, compound, and per-property) now route through a single helper. On IndexOptionsConflict (error 85) it reconciles instead of only logging: a TTL change is applied in place with collMod (no drop, no rebuild, no gap); any other conflicting change is dropped and recreated only when indexAttributes: [recreateOnConflict: true] is declared, otherwise logged with guidance. Adds TtlIndexSpec covering TTL creation from the mapping declaration and in-place TTL reconciliation on re-initialisation.
6bcbd44 to
de596d0
Compare
…hetic _fts key
A declared text index has key {field: 'text'}, but MongoDB reports an existing one with a
synthetic {_fts: 'text', _ftsx: 1} key, so the IndexOptionsConflict reconcile never matched it
and recreateOnConflict couldn't absorb a differently-named text index. Since MongoDB allows at
most one text index per collection, an existing text index is unambiguously the conflicting one
— findIndexByKeyPattern now matches any existing text index when the declared index is itself a
text index, regardless of key shape or name.
Adds TextIndexViaAttributesSpec covering conventional text-index declaration via
indexAttributes: [type: 'text'] and absorption of a legacy differently-named text index.
✅ All tests passed ✅🏷️ Commit: b939e01 Learn more about TestLens at testlens.app. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Two related improvements to MongoDB index initialisation in
MongoDatastore.initializeIndices.1. TTL indexes can be declared in the mapping DSL
A single-field index declared with
expireAfterSecondsnow materialises a MongoDB TTL index:Previously this was impossible.
MongoConstants.mapToObjectbuildsIndexOptionsby reflectively invoking same-named single-argument setters, but the driver's only TTL setter is the two-argumentIndexOptions.expireAfter(Long, TimeUnit), which the mapper can't reach. SoexpireAfterSecondsis now pulled out of the index attributes and applied explicitly.TTL is single-field only — MongoDB rejects
expireAfterSecondson a compound index withCannotCreateIndex; that error surfaces unchanged.2. Index-option conflicts are reconciled, not just logged
All three index paths (declared
getIndices(),getCompoundIndices(), per-property) now route through one helper. OnIndexOptionsConflict(server error 85 — an index already exists on these keys with different options) it reconciles instead of only logging the stack trace:collMod— no drop, no rebuild, no window without an index.indexAttributes: [recreateOnConflict: true]is declared; otherwise it logs a clear, actionable message. A unique index is never silently dropped.Why
Without DSL support, a TTL index has to be created imperatively (raw-driver
createIndexin a bootstrap hook), and changing its retention needs hand-rolled drop-and-recreate logic. That is easy to get wrong: a stale plain index on the same key blocks the TTL create, and the TTL then silently never applies. Declaring it in the mapping, with in-place reconciliation handled by the framework, removes both the boilerplate and that footgun.Tests
TtlIndexSpec(runs against the Mongo testcontainer) covers:expireAfterSecondsin the mapping produces a TTL index with the declared value.collModreconcile path.Existing
IndexAttributesAndCompoundKeySpec(unique / compound) still passes.