Consider properties from @AutoConfigureTestDatabase
Both Flyway and Liquibase makes use of DataSourceProperties to get default properties. Previously, both used strictly the configuration properties and failed to consider embedded datasource properties autoconfigured by @AutoConfigureTestDatabase. In case a database layer test e.g. @JdbcTest relies on the autoconfigured embedded datasource, Flyway and Liquibase autoconfiguration fails as they are not aware of the embedded datasource properties. See gh-16814
This commit is contained in:
parent
ba85394e83
commit
92256c80d0
|
@ -79,6 +79,7 @@ import org.springframework.util.StringUtils;
|
||||||
* @author Eddú Meléndez
|
* @author Eddú Meléndez
|
||||||
* @author Dominic Gunn
|
* @author Dominic Gunn
|
||||||
* @author Dan Zheng
|
* @author Dan Zheng
|
||||||
|
* @author András Deák
|
||||||
* @since 1.1.0
|
* @since 1.1.0
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
|
@ -156,9 +157,10 @@ public class FlywayAutoConfiguration {
|
||||||
|
|
||||||
private DataSource configureDataSource(FluentConfiguration configuration) {
|
private DataSource configureDataSource(FluentConfiguration configuration) {
|
||||||
if (this.properties.isCreateDataSource()) {
|
if (this.properties.isCreateDataSource()) {
|
||||||
String url = getProperty(this.properties::getUrl, this.dataSourceProperties::getUrl);
|
String url = getProperty(this.properties::getUrl, this.dataSourceProperties::determineUrl);
|
||||||
String user = getProperty(this.properties::getUser, this.dataSourceProperties::getUsername);
|
String user = getProperty(this.properties::getUser, this.dataSourceProperties::determineUsername);
|
||||||
String password = getProperty(this.properties::getPassword, this.dataSourceProperties::getPassword);
|
String password = getProperty(this.properties::getPassword,
|
||||||
|
this.dataSourceProperties::determinePassword);
|
||||||
configuration.dataSource(url, user, password);
|
configuration.dataSource(url, user, password);
|
||||||
if (!CollectionUtils.isEmpty(this.properties.getInitSqls())) {
|
if (!CollectionUtils.isEmpty(this.properties.getInitSqls())) {
|
||||||
String initSql = StringUtils.collectionToDelimitedString(this.properties.getInitSqls(), "\n");
|
String initSql = StringUtils.collectionToDelimitedString(this.properties.getInitSqls(), "\n");
|
||||||
|
|
|
@ -61,6 +61,7 @@ import org.springframework.util.Assert;
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
* @author Dominic Gunn
|
* @author Dominic Gunn
|
||||||
* @author Dan Zheng
|
* @author Dan Zheng
|
||||||
|
* @author András Deák
|
||||||
* @since 1.1.0
|
* @since 1.1.0
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@ -153,9 +154,9 @@ public class LiquibaseAutoConfiguration {
|
||||||
}
|
}
|
||||||
|
|
||||||
private DataSource createNewDataSource() {
|
private DataSource createNewDataSource() {
|
||||||
String url = getProperty(this.properties::getUrl, this.dataSourceProperties::getUrl);
|
String url = getProperty(this.properties::getUrl, this.dataSourceProperties::determineUrl);
|
||||||
String user = getProperty(this.properties::getUser, this.dataSourceProperties::getUsername);
|
String user = getProperty(this.properties::getUser, this.dataSourceProperties::determineUsername);
|
||||||
String password = getProperty(this.properties::getPassword, this.dataSourceProperties::getPassword);
|
String password = getProperty(this.properties::getPassword, this.dataSourceProperties::determinePassword);
|
||||||
return DataSourceBuilder.create().url(url).username(user).password(password).build();
|
return DataSourceBuilder.create().url(url).username(user).password(password).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@ import org.flywaydb.core.api.callback.Callback;
|
||||||
import org.flywaydb.core.api.callback.Context;
|
import org.flywaydb.core.api.callback.Context;
|
||||||
import org.flywaydb.core.api.callback.Event;
|
import org.flywaydb.core.api.callback.Event;
|
||||||
import org.flywaydb.core.api.callback.FlywayCallback;
|
import org.flywaydb.core.api.callback.FlywayCallback;
|
||||||
|
import org.flywaydb.core.internal.jdbc.DriverDataSource;
|
||||||
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
|
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.InOrder;
|
import org.mockito.InOrder;
|
||||||
|
@ -66,6 +67,7 @@ import static org.mockito.Mockito.mock;
|
||||||
* @author Eddú Meléndez
|
* @author Eddú Meléndez
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Dominic Gunn
|
* @author Dominic Gunn
|
||||||
|
* @author András Deák
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
public class FlywayAutoConfigurationTests {
|
public class FlywayAutoConfigurationTests {
|
||||||
|
@ -98,6 +100,30 @@ public class FlywayAutoConfigurationTests {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createDataSourceFallbackToEmbeddedProperties() {
|
||||||
|
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
|
||||||
|
.withPropertyValues("spring.flyway.url:jdbc:hsqldb:mem:flywaytest").run((context) -> {
|
||||||
|
assertThat(context).hasSingleBean(Flyway.class);
|
||||||
|
assertThat(context.getBean(Flyway.class).getDataSource()).isNotNull();
|
||||||
|
assertThat(((DriverDataSource) context.getBean(Flyway.class).getDataSource()).getUser())
|
||||||
|
.isEqualTo("sa");
|
||||||
|
assertThat(((DriverDataSource) context.getBean(Flyway.class).getDataSource()).getPassword())
|
||||||
|
.isEqualTo("");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createDataSourceWithUserAndFallbackToEmbeddedProperties() {
|
||||||
|
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
|
||||||
|
.withPropertyValues("spring.flyway.user:sa").run((context) -> {
|
||||||
|
assertThat(context).hasSingleBean(Flyway.class);
|
||||||
|
assertThat(context.getBean(Flyway.class).getDataSource()).isNotNull();
|
||||||
|
assertThat(((DriverDataSource) context.getBean(Flyway.class).getDataSource()).getUrl())
|
||||||
|
.startsWith("jdbc:h2:mem:");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void flywayDataSource() {
|
public void flywayDataSource() {
|
||||||
this.contextRunner
|
this.contextRunner
|
||||||
|
|
|
@ -60,6 +60,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Dominic Gunn
|
* @author Dominic Gunn
|
||||||
|
* @author András Deák
|
||||||
*/
|
*/
|
||||||
public class LiquibaseAutoConfigurationTests {
|
public class LiquibaseAutoConfigurationTests {
|
||||||
|
|
||||||
|
@ -199,6 +200,28 @@ public class LiquibaseAutoConfigurationTests {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void overrideDataSourceAndFallbackToEmbeddedProperties() {
|
||||||
|
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
|
||||||
|
.withPropertyValues("spring.liquibase.url:jdbc:hsqldb:mem:liquibase")
|
||||||
|
.run(assertLiquibase((liquibase) -> {
|
||||||
|
DataSource dataSource = liquibase.getDataSource();
|
||||||
|
assertThat(((HikariDataSource) dataSource).isClosed()).isTrue();
|
||||||
|
assertThat(((HikariDataSource) dataSource).getUsername()).isEqualTo("sa");
|
||||||
|
assertThat(((HikariDataSource) dataSource).getPassword()).isEqualTo("");
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void overrideUserAndFallbackToEmbeddedProperties() {
|
||||||
|
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
|
||||||
|
.withPropertyValues("spring.liquibase.user:sa").run(assertLiquibase((liquibase) -> {
|
||||||
|
DataSource dataSource = liquibase.getDataSource();
|
||||||
|
assertThat(((HikariDataSource) dataSource).isClosed()).isTrue();
|
||||||
|
assertThat(((HikariDataSource) dataSource).getJdbcUrl()).startsWith("jdbc:h2:mem:");
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void overrideTestRollbackOnUpdate() {
|
public void overrideTestRollbackOnUpdate() {
|
||||||
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
|
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
|
||||||
|
|
|
@ -35,4 +35,5 @@
|
||||||
<suppress files="LogbackMetricsAutoConfiguration\.java" checks="IllegalImport" />
|
<suppress files="LogbackMetricsAutoConfiguration\.java" checks="IllegalImport" />
|
||||||
<suppress files="RemoteUrlPropertyExtractorTests\.java" checks="IllegalImport" />
|
<suppress files="RemoteUrlPropertyExtractorTests\.java" checks="IllegalImport" />
|
||||||
<suppress files="SampleLogbackApplication\.java" checks="IllegalImport" />
|
<suppress files="SampleLogbackApplication\.java" checks="IllegalImport" />
|
||||||
|
<suppress files="FlywayAutoConfigurationTests\.java" checks="IllegalImport" />
|
||||||
</suppressions>
|
</suppressions>
|
||||||
|
|
Loading…
Reference in New Issue