Merge branch '2.7.x'

pull/29157/head
Stephane Nicoll 3 years ago
commit d271e76b48

@ -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<String> 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<String> 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]));
}
}
/**

@ -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("##", "--");
}
}

@ -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) {
}
}

@ -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) {

Loading…
Cancel
Save