codeconsole opened a new pull request, #15747:
URL: https://github.com/apache/grails-core/pull/15747

   ## 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 `expireAfterSeconds` now materialises a 
MongoDB TTL index:
   
   ```groovy
   static mapping = {
       created index: true, indexAttributes: [expireAfterSeconds: 7776000]   // 
90 days
   }
   ```
   
   Previously this was impossible. `MongoConstants.mapToObject` builds 
`IndexOptions` by reflectively invoking same-named **single-argument** setters, 
but the driver's only TTL setter is the **two-argument** 
`IndexOptions.expireAfter(Long, TimeUnit)`, which the mapper can't reach. So 
`expireAfterSeconds` is now pulled out of the index attributes and applied 
explicitly.
   
   TTL is single-field only — MongoDB rejects `expireAfterSeconds` on a 
compound index with `CannotCreateIndex`; 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. On `IndexOptionsConflict` (server 
error 85 — an index already exists on these keys with different options) it 
reconciles instead of only logging the stack trace:
   
   - **TTL value change** (e.g. a configurable retention changed between 
restarts) is applied in place via `collMod` — no drop, no rebuild, no window 
without an index.
   - **Any other conflicting change** is dropped and recreated only when 
`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 
`createIndex` in 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:
   - `expireAfterSeconds` in the mapping produces a TTL index with the declared 
value.
   - Mutating the live TTL and re-initialising restores the declared value on 
the same index (one index, no conflict) — exercising the `collMod` reconcile 
path.
   
   Existing `IndexAttributesAndCompoundKeySpec` (unique / compound) still 
passes.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to