DATAMONGO-2477 - Disable auto index creation by default.
Original pull request: #845.
This commit is contained in:
committed by
Mark Paluch
parent
7bac739146
commit
29f05af733
@@ -96,6 +96,9 @@ public class MappingMongoConverterParser implements BeanDefinitionParser {
|
||||
String id = element.getAttribute(AbstractBeanDefinitionParser.ID_ATTRIBUTE);
|
||||
id = StringUtils.hasText(id) ? id : DEFAULT_CONVERTER_BEAN_NAME;
|
||||
|
||||
String autoIndexCreation = element.getAttribute("auto-index-creation");
|
||||
boolean autoIndexCreationEnabled = StringUtils.hasText(autoIndexCreation) && Boolean.valueOf(autoIndexCreation);
|
||||
|
||||
parserContext.pushContainingComponent(new CompositeComponentDefinition("Mapping Mongo Converter", element));
|
||||
|
||||
BeanDefinition conversionsDefinition = getCustomConversions(element, parserContext);
|
||||
@@ -199,6 +202,11 @@ public class MappingMongoConverterParser implements BeanDefinitionParser {
|
||||
|
||||
public static String potentiallyCreateMappingContext(Element element, ParserContext parserContext,
|
||||
@Nullable BeanDefinition conversionsDefinition, @Nullable String converterId) {
|
||||
return potentiallyCreateMappingContext(element, parserContext, conversionsDefinition, converterId, false);
|
||||
}
|
||||
|
||||
public static String potentiallyCreateMappingContext(Element element, ParserContext parserContext,
|
||||
@Nullable BeanDefinition conversionsDefinition, @Nullable String converterId, boolean autoIndexCreation) {
|
||||
|
||||
String ctxRef = element.getAttribute("mapping-context-ref");
|
||||
|
||||
@@ -226,6 +234,8 @@ public class MappingMongoConverterParser implements BeanDefinitionParser {
|
||||
mappingContextBuilder.addPropertyValue("simpleTypeHolder", simpleTypesDefinition);
|
||||
}
|
||||
|
||||
mappingContextBuilder.addPropertyValue("autoIndexCreation", autoIndexCreation);
|
||||
|
||||
parseFieldNamingStrategy(element, parserContext.getReaderContext(), mappingContextBuilder);
|
||||
|
||||
ctxRef = converterId == null || DEFAULT_CONVERTER_BEAN_NAME.equals(converterId) ? MAPPING_CONTEXT_BEAN_NAME
|
||||
|
||||
@@ -199,12 +199,12 @@ public abstract class MongoConfigurationSupport {
|
||||
* Configure whether to automatically create indices for domain types by deriving the
|
||||
* {@link org.springframework.data.mongodb.core.index.IndexDefinition} from the entity or not.
|
||||
*
|
||||
* @return {@literal true} by default. <br />
|
||||
* <strong>INFO</strong>: As of 3.x the default will be set to {@literal false}.
|
||||
* @return {@literal false} by default. <br />
|
||||
* <strong>INFO</strong>: As of 3.x the default will is set to {@literal false} was {@literal true} in 2.x.
|
||||
* @since 2.2
|
||||
*/
|
||||
protected boolean autoIndexCreation() {
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -43,7 +43,7 @@ public class MongoMappingContext extends AbstractMappingContext<BasicMongoPersis
|
||||
|
||||
private FieldNamingStrategy fieldNamingStrategy = DEFAULT_NAMING_STRATEGY;
|
||||
private @Nullable ApplicationContext context;
|
||||
private boolean autoIndexCreation = true;
|
||||
private boolean autoIndexCreation = false;
|
||||
|
||||
/**
|
||||
* Creates a new {@link MongoMappingContext}.
|
||||
@@ -108,7 +108,8 @@ public class MongoMappingContext extends AbstractMappingContext<BasicMongoPersis
|
||||
* <strong>NOTE:</strong>Index creation should happen at a well-defined time that is ideally controlled by the
|
||||
* application itself.
|
||||
*
|
||||
* @return {@literal true} when auto-index creation is enabled; {@literal false} otherwise.
|
||||
* @return {@literal true} when auto-index creation is enabled; {@literal false} otherwise. <br />
|
||||
* <strong>INFO</strong>: As of 3.x the default will is set to {@literal false} was {@literal true} in 2.x.
|
||||
* @since 2.2
|
||||
* @see org.springframework.data.mongodb.core.index.Indexed
|
||||
*/
|
||||
@@ -121,7 +122,7 @@ public class MongoMappingContext extends AbstractMappingContext<BasicMongoPersis
|
||||
* <strong>NOTE:</strong>Index creation should happen at a well-defined time that is ideally controlled by the
|
||||
* application itself.
|
||||
*
|
||||
* @param autoCreateIndexes set to {@literal false} to disable auto-index creation.
|
||||
* @param autoCreateIndexes set to {@literal true} to enable auto-index creation.
|
||||
* @since 2.2
|
||||
* @see org.springframework.data.mongodb.core.index.Indexed
|
||||
*/
|
||||
|
||||
@@ -224,6 +224,16 @@ The base package in which to scan for entities annotated with @Document
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="auto-index-creation" use="optional">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Enable/Disable index creation for annotated properties/entities.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:simpleType>
|
||||
<xsd:union memberTypes="xsd:boolean xsd:string"/>
|
||||
</xsd:simpleType>
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
|
||||
@@ -62,6 +62,11 @@ public abstract class AbstractIntegrationTests {
|
||||
protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean autoIndexCreation() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired MongoOperations operations;
|
||||
|
||||
@@ -206,6 +206,7 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests {
|
||||
when(distinctIterable.into(any())).thenReturn(Collections.emptyList());
|
||||
|
||||
this.mappingContext = new MongoMappingContext();
|
||||
mappingContext.setAutoIndexCreation(true);
|
||||
mappingContext.afterPropertiesSet();
|
||||
|
||||
this.converter = spy(new MappingMongoConverter(new DefaultDbRefResolver(factory), mappingContext));
|
||||
|
||||
@@ -19,6 +19,9 @@ import static org.assertj.core.data.Index.atIndex;
|
||||
import static org.springframework.data.mongodb.test.util.Assertions.*;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
|
||||
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
@@ -66,7 +69,10 @@ public class ReactiveMongoTemplateIndexTests {
|
||||
void setUp() {
|
||||
|
||||
factory = new SimpleReactiveMongoDatabaseFactory(client, "reactive-template-index-tests");
|
||||
template = new ReactiveMongoTemplate(factory);
|
||||
MongoMappingContext mappingContext = new MongoMappingContext();
|
||||
mappingContext.setAutoIndexCreation(true);
|
||||
template = new ReactiveMongoTemplate(factory, new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext));
|
||||
|
||||
|
||||
MongoTestUtils.dropCollectionNow(template.getMongoDatabase().getName(), "person", client);
|
||||
MongoTestUtils.dropCollectionNow(template.getMongoDatabase().getName(), "indexfail", client);
|
||||
|
||||
@@ -95,6 +95,11 @@ public class IndexingIntegrationTests {
|
||||
protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean autoIndexCreation() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
|
||||
@@ -55,4 +55,9 @@ public class GeoIndexedAppConfig extends AbstractMongoClientConfiguration {
|
||||
protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean autoIndexCreation() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,9 @@ import org.springframework.data.domain.Sort.Direction;
|
||||
import org.springframework.data.mongodb.MongoCollectionUtils;
|
||||
import org.springframework.data.mongodb.core.CollectionCallback;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
|
||||
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
|
||||
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.mongodb.test.util.Client;
|
||||
@@ -186,7 +189,10 @@ public class MappingTests {
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void testUniqueIndex() {
|
||||
|
||||
MongoTemplate template = new MongoTemplate(client, DB_NAME);
|
||||
MongoMappingContext mappingContext = new MongoMappingContext();
|
||||
mappingContext.setAutoIndexCreation(true);
|
||||
|
||||
MongoTemplate template = new MongoTemplate(new SimpleMongoClientDatabaseFactory(client, DB_NAME), new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext));
|
||||
|
||||
Address addr = new Address();
|
||||
addr.setLines(new String[] { "1234 W. 1st Street", "Apt. 12" });
|
||||
@@ -227,7 +233,11 @@ public class MappingTests {
|
||||
@Test
|
||||
public void testIndexesCreatedInRightCollection() {
|
||||
|
||||
MongoTemplate template = new MongoTemplate(client, DB_NAME);
|
||||
MongoMappingContext mappingContext = new MongoMappingContext();
|
||||
mappingContext.setAutoIndexCreation(true);
|
||||
|
||||
MongoTemplate template = new MongoTemplate(new SimpleMongoClientDatabaseFactory(client, DB_NAME), new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext));
|
||||
|
||||
|
||||
CustomCollectionWithIndex ccwi = new CustomCollectionWithIndex("test");
|
||||
template.insert(ccwi);
|
||||
|
||||
@@ -131,6 +131,11 @@ public class ReactiveMongoRepositoryTests {
|
||||
ReactiveCappedCollectionRepository reactiveCappedCollectionRepository(ReactiveMongoRepositoryFactory factory) {
|
||||
return factory.getRepository(ReactiveCappedCollectionRepository.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean autoIndexCreation() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<mongo:mapping-converter auto-index-creation="true" />
|
||||
|
||||
<mongo:repositories base-package="org.springframework.data.mongodb.repository">
|
||||
<repository:exclude-filter type="regex" expression=".*MongoRepository"/>
|
||||
</mongo:repositories>
|
||||
|
||||
@@ -392,7 +392,7 @@ Automatic index creation is only done for types annotated with `@Document`.
|
||||
|
||||
[NOTE]
|
||||
====
|
||||
To turn automatic index creation _OFF_ please override `autoIndexCreation()` in your configuration.
|
||||
To turn automatic index creation _ON_ please override `autoIndexCreation()` in your configuration.
|
||||
[source,java]
|
||||
----
|
||||
@Configuration
|
||||
@@ -400,7 +400,7 @@ public class Config extends AbstractMongoClientConfiguration {
|
||||
|
||||
@Override
|
||||
public boolean autoIndexCreation() {
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// ...
|
||||
@@ -408,7 +408,7 @@ public class Config extends AbstractMongoClientConfiguration {
|
||||
----
|
||||
====
|
||||
|
||||
IMPORTANT: Automatic index creation will be turned _OFF_ by default with the release of 3.x.
|
||||
IMPORTANT: Automatic index creation is turned _OFF_ by default as of the release of 3.x.
|
||||
We recommend index creation to happen either out of band or as part of the application startup using
|
||||
`IndexOperations`.
|
||||
|
||||
|
||||
@@ -148,6 +148,52 @@ Element | Comment
|
||||
|
||||
== Other Changes
|
||||
|
||||
=== Auto Index Creation
|
||||
|
||||
Annotation based index creation is now turned **OFF** by default and needs to be enabled eg. when relying on `@GeoSpatialIndexed`.
|
||||
|
||||
.Enable Auto Index Creation
|
||||
====
|
||||
|
||||
.XML Namespace
|
||||
[source,xml]
|
||||
----
|
||||
<mongo:mapping-converter auto-index-creation="true" /> <1>
|
||||
----
|
||||
|
||||
.Java Config
|
||||
[source,java]
|
||||
----
|
||||
@Configuration
|
||||
public class Config extends AbstractMongoClientConfiguration {
|
||||
|
||||
@Override
|
||||
protected boolean autoIndexCreation() { <2>
|
||||
return true;
|
||||
}
|
||||
|
||||
// ...
|
||||
}
|
||||
----
|
||||
|
||||
.Programmatic
|
||||
[source,java]
|
||||
----
|
||||
MongoDatabaseFactory dbFactory = new SimpleMongoClientDatabaseFactory(...);
|
||||
DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(dbFactory);
|
||||
|
||||
MongoMappingContext mappingContext = new MongoMappingContext();
|
||||
mappingContext.setAutoIndexCreation(true); <3>
|
||||
// ...
|
||||
mappingContext.afterPropertiesSet();
|
||||
|
||||
MongoTemplate template = new MongoTemplate(dbFactory, new MappingMongoConverter(dbRefResolver, mappingContext));
|
||||
----
|
||||
<1> Use the XML namespace attribute `auto-index-creation` on `mapping-converter`.
|
||||
<2> Overrride `autoIndexCreation` via `AbstractMongoClientConfiguration` or `AbstractReactiveMongoClientConfiguration`.
|
||||
<3> Set the flag on `MongoMappingContext`.
|
||||
====
|
||||
|
||||
=== UUID Types
|
||||
|
||||
The MongoDB UUID representation can now be configured with different formats.
|
||||
@@ -157,8 +203,8 @@ This has to be done via `MongoClientSettings` as shown in the snippet below.
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
|
||||
static class Config extends AbstractMongoClientConfiguration {
|
||||
@Configuration
|
||||
public class Config extends AbstractMongoClientConfiguration {
|
||||
|
||||
@Override
|
||||
public void configureClientSettings(MongoClientSettings.Builder builder) {
|
||||
|
||||
Reference in New Issue
Block a user