Skip to content

Commit e43c6ed

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 6069756 commit e43c6ed

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

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

Lines changed: 7 additions & 3 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.
@@ -23,6 +23,7 @@
2323
*
2424
* @author Stephane Nicoll
2525
* @author Eddú Meléndez
26+
* @author Vedran Pavic
2627
* @since 1.2.0
2728
*/
2829
@ConfigurationProperties("spring.batch")
@@ -69,15 +70,18 @@ public String getTablePrefix() {
6970
return this.tablePrefix;
7071
}
7172

72-
public static class Initializer {
73+
public class Initializer {
7374

7475
/**
7576
* Create the required batch tables on startup if necessary.
7677
*/
7778
private boolean enabled = true;
7879

7980
public boolean isEnabled() {
80-
return this.enabled;
81+
boolean isDefaultTablePrefix = BatchProperties.this.getTablePrefix() == null;
82+
boolean isDefaultSchema = DEFAULT_SCHEMA_LOCATION.equals(
83+
BatchProperties.this.getSchema());
84+
return this.enabled && (isDefaultTablePrefix || !isDefaultSchema);
8185
}
8286

8387
public void setEnabled(boolean enabled) {

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
*
6868
* @author Dave Syer
6969
* @author Stephane Nicoll
70+
* @author Vedran Pavic
7071
*/
7172
public class BatchAutoConfigurationTests {
7273

@@ -91,6 +92,8 @@ public void testDefaultContext() throws Exception {
9192
this.context.refresh();
9293
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
9394
assertThat(this.context.getBean(JobExplorer.class)).isNotNull();
95+
assertThat(this.context.getBean(BatchProperties.class)
96+
.getInitializer().isEnabled()).isTrue();
9497
assertThat(new JdbcTemplate(this.context.getBean(DataSource.class))
9598
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
9699
}
@@ -190,6 +193,8 @@ public void testDisableSchemaLoader() throws Exception {
190193
PropertyPlaceholderAutoConfiguration.class);
191194
this.context.refresh();
192195
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
196+
assertThat(this.context.getBean(BatchProperties.class)
197+
.getInitializer().isEnabled()).isFalse();
193198
this.expected.expect(BadSqlGrammarException.class);
194199
new JdbcTemplate(this.context.getBean(DataSource.class))
195200
.queryForList("select * from BATCH_JOB_EXECUTION");
@@ -228,6 +233,8 @@ public void testRenamePrefix() throws Exception {
228233
PropertyPlaceholderAutoConfiguration.class);
229234
this.context.refresh();
230235
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
236+
assertThat(this.context.getBean(BatchProperties.class)
237+
.getInitializer().isEnabled()).isTrue();
231238
assertThat(new JdbcTemplate(this.context.getBean(DataSource.class))
232239
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
233240
JobExplorer jobExplorer = this.context.getBean(JobExplorer.class);
@@ -237,6 +244,25 @@ public void testRenamePrefix() throws Exception {
237244
.isNull();
238245
}
239246

247+
@Test
248+
public void testCustomTablePrefixWithDefaultSchemaDisablesInitializer() throws Exception {
249+
this.context = new AnnotationConfigApplicationContext();
250+
EnvironmentTestUtils.addEnvironment(this.context,
251+
"spring.datasource.name:batchtest",
252+
"spring.batch.tablePrefix:PREFIX_");
253+
this.context.register(TestConfiguration.class,
254+
EmbeddedDataSourceConfiguration.class,
255+
HibernateJpaAutoConfiguration.class, BatchAutoConfiguration.class,
256+
PropertyPlaceholderAutoConfiguration.class);
257+
this.context.refresh();
258+
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
259+
assertThat(this.context.getBean(BatchProperties.class)
260+
.getInitializer().isEnabled()).isFalse();
261+
this.expected.expect(BadSqlGrammarException.class);
262+
new JdbcTemplate(this.context.getBean(DataSource.class))
263+
.queryForList("select * from BATCH_JOB_EXECUTION");
264+
}
265+
240266
@Configuration
241267
protected static class EmptyConfiguration {
242268
}

0 commit comments

Comments
 (0)