diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java index c08435449afb..6e4132c14151 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java @@ -39,6 +39,9 @@ @Component public class BatchDatabaseInitializer implements EnvironmentAware { + private static final String DEFAULT_SCHEMA_DROP_LOCATION = "classpath:org/springframework/" + + "batch/core/schema-drop-@@platform@@.sql"; + private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/" + "batch/core/schema-@@platform@@.sql"; @@ -51,6 +54,9 @@ public class BatchDatabaseInitializer implements EnvironmentAware { @Value("${spring.batch.initializer.enabled:true}") private boolean enabled = true; + @Value("${spring.jpa.hibernate.ddl-auto:create}") + private String ddl_auto = "create"; + private RelaxedPropertyResolver environment; @Override @@ -71,7 +77,16 @@ protected void initialize() { if ("oracle".equals(platform)) { platform = "oracle10g"; } - ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); + + ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); + + if(shouldDrop()) + { + String schemaDropLocation = this.environment.getProperty("schema-drop", DEFAULT_SCHEMA_DROP_LOCATION); + schemaDropLocation = schemaDropLocation.replace("@@platform@@", platform); + populator.addScript(this.resourceLoader.getResource(schemaDropLocation)); + } + String schemaLocation = this.environment.getProperty("schema", DEFAULT_SCHEMA_LOCATION); schemaLocation = schemaLocation.replace("@@platform@@", platform); @@ -81,7 +96,18 @@ protected void initialize() { } } - private String getDatabaseType() { + /*** + * Should drop batch tables + * @return true if hibernate.ddl-auto is true or false + */ + public boolean shouldDrop() + { + if(ddl_auto == null) + return true; + return ddl_auto.contains("create"); + } + + private String getDatabaseType() { try { return DatabaseType.fromMetaData(this.dataSource).toString().toLowerCase(); } 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 02c8e92eb2f7..a275ce2623d0 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 @@ -63,6 +63,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; /** * Tests for {@link BatchAutoConfiguration}. @@ -176,6 +177,59 @@ public void testDisableLaunchesJob() throws Exception { assertEquals(0, this.context.getBeanNamesForType(CommandLineRunner.class).length); } + @Test + public void testTableShouldDropt() throws Exception + { + this.context = new AnnotationConfigApplicationContext(); + //No setting for spring.jpa.hibernate.ddl-auto + + this.context.register(JobConfiguration.class, + EmbeddedDataSourceConfiguration.class, + BatchAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + + this.context.refresh(); + + BatchDatabaseInitializer initializer = this.context.getBean(BatchDatabaseInitializer.class); + assertTrue(initializer.shouldDrop()); + } + + @Test + public void testTableNotDrop() throws Exception + { + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.jpa.hibernate.ddl-auto:update"); + + this.context.register(JobConfiguration.class, + EmbeddedDataSourceConfiguration.class, + BatchAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + + this.context.refresh(); + + BatchDatabaseInitializer initializer = this.context.getBean(BatchDatabaseInitializer.class); + assertFalse(initializer.shouldDrop()); + } + + @Test + public void testTableShouldDropByDefault() throws Exception + { + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.jpa.hibernate.ddl-auto:create"); + + this.context.register(JobConfiguration.class, + EmbeddedDataSourceConfiguration.class, + BatchAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + + this.context.refresh(); + + BatchDatabaseInitializer initializer = this.context.getBean(BatchDatabaseInitializer.class); + assertTrue(initializer.shouldDrop()); + } + @Test public void testDisableSchemaLoader() throws Exception { this.context = new AnnotationConfigApplicationContext();