From 06a1f44128078cd4142a591363d7fbefa04805eb Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Sun, 14 Aug 2016 11:48:47 +0200 Subject: [PATCH 1/2] 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. See gh-6651 --- .../autoconfigure/batch/BatchProperties.java | 10 ++++--- .../batch/BatchAutoConfigurationTests.java | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java index 0da9917e364..17e0e7b38ef 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; * * @author Stephane Nicoll * @author EddĂș MelĂ©ndez + * @author Vedran Pavic * @since 1.2.0 */ @ConfigurationProperties("spring.batch") @@ -69,7 +70,7 @@ public class BatchProperties { return this.tablePrefix; } - public static class Initializer { + public class Initializer { /** * Create the required batch tables on startup if necessary. @@ -77,7 +78,10 @@ public class BatchProperties { private boolean enabled = true; public boolean isEnabled() { - return this.enabled; + boolean isDefaultTablePrefix = BatchProperties.this.getTablePrefix() == null; + boolean isDefaultSchema = DEFAULT_SCHEMA_LOCATION.equals( + BatchProperties.this.getSchema()); + return this.enabled && (isDefaultTablePrefix || !isDefaultSchema); } public void setEnabled(boolean enabled) { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java index 75199d71629..8a91c0c0ec1 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java @@ -67,6 +67,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Dave Syer * @author Stephane Nicoll + * @author Vedran Pavic */ public class BatchAutoConfigurationTests { @@ -91,6 +92,8 @@ public class BatchAutoConfigurationTests { this.context.refresh(); assertThat(this.context.getBean(JobLauncher.class)).isNotNull(); assertThat(this.context.getBean(JobExplorer.class)).isNotNull(); + assertThat(this.context.getBean(BatchProperties.class) + .getInitializer().isEnabled()).isTrue(); assertThat(new JdbcTemplate(this.context.getBean(DataSource.class)) .queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty(); } @@ -190,6 +193,8 @@ public class BatchAutoConfigurationTests { PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(JobLauncher.class)).isNotNull(); + assertThat(this.context.getBean(BatchProperties.class) + .getInitializer().isEnabled()).isFalse(); this.expected.expect(BadSqlGrammarException.class); new JdbcTemplate(this.context.getBean(DataSource.class)) .queryForList("select * from BATCH_JOB_EXECUTION"); @@ -228,6 +233,8 @@ public class BatchAutoConfigurationTests { PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(JobLauncher.class)).isNotNull(); + assertThat(this.context.getBean(BatchProperties.class) + .getInitializer().isEnabled()).isTrue(); assertThat(new JdbcTemplate(this.context.getBean(DataSource.class)) .queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty(); JobExplorer jobExplorer = this.context.getBean(JobExplorer.class); @@ -237,6 +244,25 @@ public class BatchAutoConfigurationTests { .isNull(); } + @Test + public void testCustomTablePrefixWithDefaultSchemaDisablesInitializer() throws Exception { + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.datasource.name:batchtest", + "spring.batch.tablePrefix:PREFIX_"); + this.context.register(TestConfiguration.class, + EmbeddedDataSourceConfiguration.class, + HibernateJpaAutoConfiguration.class, BatchAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + assertThat(this.context.getBean(JobLauncher.class)).isNotNull(); + assertThat(this.context.getBean(BatchProperties.class) + .getInitializer().isEnabled()).isFalse(); + this.expected.expect(BadSqlGrammarException.class); + new JdbcTemplate(this.context.getBean(DataSource.class)) + .queryForList("select * from BATCH_JOB_EXECUTION"); + } + @Configuration protected static class EmptyConfiguration { } From ee668e6782eb877cf38f2bcf617eb7e61bda7dbe Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 12 Sep 2016 14:53:02 +0200 Subject: [PATCH 2/2] Polish contribution Closes gh-6651 --- .../boot/autoconfigure/batch/BatchProperties.java | 15 ++++++++++----- .../asciidoc/appendix-application-properties.adoc | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java index 17e0e7b38ef..ae32dec5a6d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java @@ -73,15 +73,20 @@ public class BatchProperties { public class Initializer { /** - * Create the required batch tables on startup if necessary. + * Create the required batch tables on startup if necessary. Enabled + * automatically if no custom table prefix is set or if a custom schema is + * configured. */ - private boolean enabled = true; + private Boolean enabled; public boolean isEnabled() { - boolean isDefaultTablePrefix = BatchProperties.this.getTablePrefix() == null; - boolean isDefaultSchema = DEFAULT_SCHEMA_LOCATION.equals( + if (this.enabled != null) { + return this.enabled; + } + boolean defaultTablePrefix = BatchProperties.this.getTablePrefix() == null; + boolean customSchema = !DEFAULT_SCHEMA_LOCATION.equals( BatchProperties.this.getSchema()); - return this.enabled && (isDefaultTablePrefix || !isDefaultSchema); + return (defaultTablePrefix || customSchema); } public void setEnabled(boolean enabled) { diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 8b29e94dd3b..346056fb2cc 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -847,7 +847,7 @@ content into your application; rather pick only the properties that you need. spring.artemis.user= # Login user of the broker. # SPRING BATCH ({sc-spring-boot-autoconfigure}/batch/BatchProperties.{sc-ext}[BatchProperties]) - spring.batch.initializer.enabled=true # Create the required batch tables on startup if necessary. + spring.batch.initializer.enabled= # Create the required batch tables on startup if necessary. Enabled automatically if no custom table prefix is set or if a custom schema is configured. spring.batch.job.enabled=true # Execute all Spring Batch jobs in the context on startup. spring.batch.job.names= # Comma-separated list of job names to execute on startup (For instance `job1,job2`). By default, all Jobs found in the context are executed. spring.batch.schema=classpath:org/springframework/batch/core/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema.