Skip to content

Commit a2f725a

Browse files
committed
Validate Spring Batch database initializer configuration
This commit adds Spring Batch configuration validation that disables database initializer in case custom table prefix is configured with default schema.
1 parent 2d1e85c commit a2f725a

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.autoconfigure.batch;
1818

19+
import javax.annotation.PostConstruct;
1920
import javax.persistence.EntityManagerFactory;
2021
import javax.sql.DataSource;
2122

@@ -56,6 +57,7 @@
5657
*
5758
* @author Dave Syer
5859
* @author Eddú Meléndez
60+
* @author Vedran Pavic
5961
*/
6062
@Configuration
6163
@ConditionalOnClass({ JobLauncher.class, DataSource.class, JdbcOperations.class })
@@ -74,6 +76,18 @@ public BatchAutoConfiguration(BatchProperties properties,
7476
this.jobParametersConverter = jobParametersConverterProvider.getIfAvailable();
7577
}
7678

79+
@PostConstruct
80+
public void checkInitializerConfig() {
81+
if (this.properties.getInitializer().isEnabled()) {
82+
if (BatchProperties.DEFAULT_SCHEMA_LOCATION.equals(
83+
this.properties.getSchema()) &&
84+
StringUtils.hasText(this.properties.getTablePrefix())) {
85+
throw new IllegalStateException("Database initializer cannot be " +
86+
"configured using custom table prefix and default schema");
87+
}
88+
}
89+
}
90+
7791
@Bean
7892
@ConditionalOnMissingBean
7993
@ConditionalOnBean(DataSource.class)

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@
2828
@ConfigurationProperties("spring.batch")
2929
public class BatchProperties {
3030

31-
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
31+
static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
3232
+ "batch/core/schema-@@platform@@.sql";
3333

3434
/**

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.batch.core.repository.JobRepository;
4646
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
4747
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
48+
import org.springframework.beans.factory.BeanCreationException;
4849
import org.springframework.beans.factory.annotation.Autowired;
4950
import org.springframework.boot.CommandLineRunner;
5051
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
@@ -61,12 +62,14 @@
6162
import org.springframework.transaction.PlatformTransactionManager;
6263

6364
import static org.assertj.core.api.Assertions.assertThat;
65+
import static org.hamcrest.CoreMatchers.containsString;
6466

6567
/**
6668
* Tests for {@link BatchAutoConfiguration}.
6769
*
6870
* @author Dave Syer
6971
* @author Stephane Nicoll
72+
* @author Vedran Pavic
7073
*/
7174
public class BatchAutoConfigurationTests {
7275

@@ -237,6 +240,22 @@ public void testRenamePrefix() throws Exception {
237240
.isNull();
238241
}
239242

243+
@Test
244+
public void testCustomTablePrefixWithDefaultSchemaFails() throws Exception {
245+
this.expected.expect(BeanCreationException.class);
246+
this.expected.expectMessage(containsString(
247+
"Database initializer cannot be configured"));
248+
this.context = new AnnotationConfigApplicationContext();
249+
EnvironmentTestUtils.addEnvironment(this.context,
250+
"spring.datasource.name:batchtest",
251+
"spring.batch.tablePrefix:PREFIX_");
252+
this.context.register(TestConfiguration.class,
253+
EmbeddedDataSourceConfiguration.class,
254+
HibernateJpaAutoConfiguration.class, BatchAutoConfiguration.class,
255+
PropertyPlaceholderAutoConfiguration.class);
256+
this.context.refresh();
257+
}
258+
240259
@Configuration
241260
protected static class EmptyConfiguration {
242261
}

0 commit comments

Comments
 (0)