diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceScriptDatabaseInitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceScriptDatabaseInitializer.java index 8fca4af475..23f46d677c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceScriptDatabaseInitializer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceScriptDatabaseInitializer.java @@ -24,6 +24,8 @@ import org.springframework.boot.jdbc.DatabaseDriver; import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer; import org.springframework.boot.jdbc.init.PlatformPlaceholderDatabaseDriverResolver; import org.springframework.boot.sql.init.DatabaseInitializationSettings; +import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; +import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; /** @@ -37,6 +39,8 @@ import org.springframework.util.StringUtils; */ public class QuartzDataSourceScriptDatabaseInitializer extends DataSourceScriptDatabaseInitializer { + private final List commentPrefixes; + /** * Create a new {@link QuartzDataSourceScriptDatabaseInitializer} instance. * @param dataSource the Quartz Scheduler data source @@ -44,7 +48,7 @@ public class QuartzDataSourceScriptDatabaseInitializer extends DataSourceScriptD * @see #getSettings */ public QuartzDataSourceScriptDatabaseInitializer(DataSource dataSource, QuartzProperties properties) { - this(dataSource, getSettings(dataSource, properties)); + this(dataSource, getSettings(dataSource, properties), properties.getJdbc().getCommentPrefix()); } /** @@ -54,7 +58,20 @@ public class QuartzDataSourceScriptDatabaseInitializer extends DataSourceScriptD * @see #getSettings */ public QuartzDataSourceScriptDatabaseInitializer(DataSource dataSource, DatabaseInitializationSettings settings) { + this(dataSource, settings, null); + } + + private QuartzDataSourceScriptDatabaseInitializer(DataSource dataSource, DatabaseInitializationSettings settings, + List commentPrefixes) { super(dataSource, settings); + this.commentPrefixes = commentPrefixes; + } + + @Override + protected void customize(ResourceDatabasePopulator populator) { + if (!ObjectUtils.isEmpty(this.commentPrefixes)) { + populator.setCommentPrefixes(this.commentPrefixes.toArray(new String[0])); + } } /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceScriptDatabaseInitializerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceScriptDatabaseInitializerTests.java index 16b2d56ff9..dcfc5db18e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceScriptDatabaseInitializerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceScriptDatabaseInitializerTests.java @@ -16,14 +16,18 @@ package org.springframework.boot.autoconfigure.quartz; +import java.util.Arrays; + import javax.sql.DataSource; import org.junit.jupiter.api.Test; import org.springframework.boot.sql.init.DatabaseInitializationSettings; +import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; /** @@ -45,4 +49,16 @@ class QuartzDataSourceScriptDatabaseInitializerTests { verifyNoInteractions(dataSource); } + @Test + void customizeSetCommentPrefixes() { + QuartzProperties properties = new QuartzProperties(); + properties.getJdbc().setPlatform("test"); + properties.getJdbc().setCommentPrefix(Arrays.asList("##", "--")); + QuartzDataSourceScriptDatabaseInitializer initializer = new QuartzDataSourceScriptDatabaseInitializer( + mock(DataSource.class), properties); + ResourceDatabasePopulator populator = mock(ResourceDatabasePopulator.class); + initializer.customize(populator); + verify(populator).setCommentPrefixes("##", "--"); + } + } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializer.java index 043bae2575..4ca160c470 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializer.java @@ -86,7 +86,17 @@ public class DataSourceScriptDatabaseInitializer extends AbstractScriptDatabaseI for (Resource resource : resources) { populator.addScript(resource); } + customize(populator); DatabasePopulatorUtils.execute(populator, this.dataSource); } + /** + * Customize the {@link ResourceDatabasePopulator}. + * @param populator the configured database populator + * @since 2.6.2 + */ + protected void customize(ResourceDatabasePopulator populator) { + + } + } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializerTests.java index 38b8f3694c..475a846b1d 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializerTests.java @@ -16,6 +16,7 @@ package org.springframework.boot.jdbc.init; +import java.util.Collections; import java.util.UUID; import javax.sql.DataSource; @@ -29,8 +30,11 @@ import org.springframework.boot.sql.init.AbstractScriptDatabaseInitializerTests; import org.springframework.boot.sql.init.DatabaseInitializationSettings; import org.springframework.boot.testsupport.BuildOutput; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; +import org.springframework.jdbc.datasource.init.ScriptStatementFailedException; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * Tests for {@link DataSourceScriptDatabaseInitializer}. @@ -61,6 +65,22 @@ class DataSourceScriptDatabaseInitializerTests assertThat(initializer.isEmbeddedDatabase()).isFalse(); } + @Test + void whenCustomizeIsOverriddenThenDatabasePopulatorIsConfiguredAccordingly() { + DatabaseInitializationSettings settings = new DatabaseInitializationSettings(); + settings.setContinueOnError(true); + settings.setDataLocations(Collections.singletonList("data.sql")); + DataSourceScriptDatabaseInitializer initializer = new DataSourceScriptDatabaseInitializer( + this.embeddedDataSource, settings) { + @Override + protected void customize(ResourceDatabasePopulator populator) { + assertThat(populator).hasFieldOrPropertyWithValue("continueOnError", true); + populator.setContinueOnError(false); + } + }; + assertThatThrownBy(initializer::initializeDatabase).isInstanceOf(ScriptStatementFailedException.class); + } + @Override protected DataSourceScriptDatabaseInitializer createEmbeddedDatabaseInitializer( DatabaseInitializationSettings settings) {