ensureIndexes() throws unhandleable "Index with name: xyz_1" already exists with different options"
When opening a datastore, we call the ensureIndexes() on it so be sure all indexes defined by morphia annotations are created. If annotations get updated in the software over time, e.g. a sparse=true attribute is set, this call will fail on each existing datastore. We would have to delete the old indexes by hand. It would be great to have an additional parameter in the ensureIndexes() method to define if existing indexes should be overwritten, like already existing in other methods like
ensureIndex(Class<T> clazz, String name, String fields, boolean unique, boolean dropDupsOnCreate);
for single collections.
The tricky part about that feature is you'd either be recreating indexes every time (terribly expensive) or you'd have to deploy one version passing in true for that boolean and then immediately change it back to not pass in true.
Such things could maybe be mitigated but the implementation would have to be careful about how it did such things.
Would it be possible to check whether an index exists and compare the properties to the annotated index options?
- if an index with same parameters exists, do nothing (adds a roundtrip for reading indexes, but cheaper than reindexing every time)
- if an index with different parameters exists, drop it and create with annotated options (expensive only once, and imho better than an exception)
- if no index exists, create index (same as current behaviour)