From 3ca73bf00d0af8b76f938479d578fdfd72a3c7bf Mon Sep 17 00:00:00 2001 From: "ilya.lukyanovich@dataart.com" Date: Tue, 14 May 2019 18:15:00 +0300 Subject: [PATCH 1/2] Auto-configure Flyway and Liquibase when there's a URL but no DataSource See gh-16850 --- .../flyway/FlywayAutoConfiguration.java | 9 +++-- .../FlywayDataSourceMissingException.java | 34 ++++++++++++++++ ...lywayDataSourceMissingFailureAnalyzer.java | 39 +++++++++++++++++++ .../flyway/FlywayProperties.java | 4 +- .../liquibase/LiquibaseAutoConfiguration.java | 14 ++++--- .../LiquibaseDataSourceMissingException.java | 34 ++++++++++++++++ ...ibaseDataSourceMissingFailureAnalyzer.java | 39 +++++++++++++++++++ .../liquibase/LiquibaseProperties.java | 9 ++++- .../main/resources/META-INF/spring.factories | 2 + .../flyway/FlywayAutoConfigurationTests.java | 19 ++++++++- .../LiquibaseAutoConfigurationTests.java | 22 ++++++++++- ...generateConfigurationPropertyTables.groovy | 4 +- 12 files changed, 214 insertions(+), 15 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayDataSourceMissingException.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayDataSourceMissingFailureAnalyzer.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSourceMissingException.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSourceMissingFailureAnalyzer.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java index 8ee68d3e1c..4d585064f6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java @@ -82,8 +82,8 @@ import org.springframework.util.StringUtils; @SuppressWarnings("deprecation") @Configuration(proxyBeanMethods = false) @ConditionalOnClass(Flyway.class) -@ConditionalOnBean(DataSource.class) -@ConditionalOnProperty(prefix = "spring.flyway", name = "enabled", matchIfMissing = true) +@ConditionalOnProperty(prefix = FlywayProperties.PROPERTIES_PREFIX, name = "enabled", + matchIfMissing = true) @AutoConfigureAfter({ DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class, HibernateJpaAutoConfiguration.class }) public class FlywayAutoConfiguration { @@ -152,9 +152,12 @@ public class FlywayAutoConfiguration { else if (flywayDataSource != null) { configuration.dataSource(flywayDataSource); } - else { + else if (dataSource != null) { configuration.dataSource(dataSource); } + else { + throw new FlywayDataSourceMissingException(); + } return configuration.getDataSource(); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayDataSourceMissingException.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayDataSourceMissingException.java new file mode 100644 index 0000000000..75801a5420 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayDataSourceMissingException.java @@ -0,0 +1,34 @@ +/* + * Copyright 2012-2019 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.flyway; + +/** + * Exception thrown when no Flyway is enabled and present in classpath but no datasource + * was available or configured for it. + * + * @author Ilya Lukyanovich + */ +public class FlywayDataSourceMissingException extends RuntimeException { + + FlywayDataSourceMissingException() { + super("Flyway is present in classpath and enabled but " + + "no DataSource bean neither " + FlywayProperties.PROPERTIES_PREFIX + + ".url and " + FlywayProperties.PROPERTIES_PREFIX + + ".user configuration was provided"); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayDataSourceMissingFailureAnalyzer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayDataSourceMissingFailureAnalyzer.java new file mode 100644 index 0000000000..f0ac5a4969 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayDataSourceMissingFailureAnalyzer.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2019 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.flyway; + +import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; +import org.springframework.boot.diagnostics.FailureAnalysis; + +/** + * A {@code FailureAnalyzer} that performs analysis of failures caused by a + * {@link FlywayDataSourceMissingException}. + * + * @author Ilya Lukyanovich + */ +class FlywayDataSourceMissingFailureAnalyzer + extends AbstractFailureAnalyzer { + + @Override + protected FailureAnalysis analyze(Throwable rootFailure, + FlywayDataSourceMissingException cause) { + return new FailureAnalysis("No DataSource found for flyway", + "Please provide a javax.sql.DataSource bean or flyway datasource configuration", + cause); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java index 0acd2171b7..7a8b1b5d00 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java @@ -35,9 +35,11 @@ import org.springframework.boot.context.properties.ConfigurationProperties; * @author Stephane Nicoll * @since 1.1.0 */ -@ConfigurationProperties(prefix = "spring.flyway") +@ConfigurationProperties(prefix = FlywayProperties.PROPERTIES_PREFIX) public class FlywayProperties { + public static final String PROPERTIES_PREFIX = "spring.flyway"; + /** * Whether to enable flyway. */ diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java index 23127c3ede..4bd6116d13 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java @@ -65,8 +65,7 @@ import org.springframework.util.Assert; */ @Configuration(proxyBeanMethods = false) @ConditionalOnClass({ SpringLiquibase.class, DatabaseChange.class }) -@ConditionalOnBean(DataSource.class) -@ConditionalOnProperty(prefix = "spring.liquibase", name = "enabled", +@ConditionalOnProperty(prefix = LiquibaseProperties.PROPERTIES_PREFIX, name = "enabled", matchIfMissing = true) @AutoConfigureAfter({ DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class }) @@ -141,9 +140,12 @@ public class LiquibaseAutoConfiguration { liquibase.setDataSource(liquibaseDataSource); return liquibase; } - SpringLiquibase liquibase = new DataSourceClosingSpringLiquibase(); - liquibase.setDataSource(createNewDataSource(dataSourceProperties)); - return liquibase; + else if (this.properties.isCreateDataSource()) { + SpringLiquibase liquibase = new DataSourceClosingSpringLiquibase(); + liquibase.setDataSource(createNewDataSource(dataSourceProperties)); + return liquibase; + } + throw new LiquibaseDataSourceMissingException(); } private DataSource getDataSource(DataSource liquibaseDataSource, @@ -151,7 +153,7 @@ public class LiquibaseAutoConfiguration { if (liquibaseDataSource != null) { return liquibaseDataSource; } - if (this.properties.getUrl() == null && this.properties.getUser() == null) { + if (!this.properties.isCreateDataSource()) { return dataSource; } return null; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSourceMissingException.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSourceMissingException.java new file mode 100644 index 0000000000..bcd08fe01e --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSourceMissingException.java @@ -0,0 +1,34 @@ +/* + * Copyright 2012-2019 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.liquibase; + +/** + * Exception thrown when Liquibase is enabled and present in classpath but no datasource + * was available or configured for it. + * + * @author Ilya Lukyanovich + */ +public class LiquibaseDataSourceMissingException extends RuntimeException { + + LiquibaseDataSourceMissingException() { + super("Liquibase is present in classpath and enabled but " + + "no DataSource bean neither " + LiquibaseProperties.PROPERTIES_PREFIX + + ".url and " + LiquibaseProperties.PROPERTIES_PREFIX + + ".user configuration was provided"); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSourceMissingFailureAnalyzer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSourceMissingFailureAnalyzer.java new file mode 100644 index 0000000000..0114a4373c --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSourceMissingFailureAnalyzer.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2019 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.liquibase; + +import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; +import org.springframework.boot.diagnostics.FailureAnalysis; + +/** + * A {@code FailureAnalyzer} that performs analysis of failures caused by a + * {@link LiquibaseDataSourceMissingException}. + * + * @author Ilya Lukyanovich + */ +class LiquibaseDataSourceMissingFailureAnalyzer + extends AbstractFailureAnalyzer { + + @Override + protected FailureAnalysis analyze(Throwable rootFailure, + LiquibaseDataSourceMissingException cause) { + return new FailureAnalysis("No DataSource found for liquibsae", + "Please provide a javax.sql.DataSource bean or liquibase datasource configuration", + cause); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseProperties.java index edf20d7d61..a4087b0f32 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseProperties.java @@ -30,9 +30,12 @@ import org.springframework.util.Assert; * @author Marcel Overdijk * @since 1.1.0 */ -@ConfigurationProperties(prefix = "spring.liquibase", ignoreUnknownFields = false) +@ConfigurationProperties(prefix = LiquibaseProperties.PROPERTIES_PREFIX, + ignoreUnknownFields = false) public class LiquibaseProperties { + public static final String PROPERTIES_PREFIX = "spring.liquibase"; + /** * Change log configuration path. */ @@ -216,6 +219,10 @@ public class LiquibaseProperties { this.password = password; } + public boolean isCreateDataSource() { + return this.url != null || this.user != null; + } + public String getUrl() { return this.url; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index 5516525298..df62d6abb4 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -146,6 +146,8 @@ org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAuto org.springframework.boot.diagnostics.FailureAnalyzer=\ org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzer,\ org.springframework.boot.autoconfigure.flyway.FlywayMigrationScriptMissingFailureAnalyzer,\ +org.springframework.boot.autoconfigure.flyway.FlywayDataSourceMissingFailureAnalyzer,\ +org.springframework.boot.autoconfigure.liquibase.LiquibaseDataSourceMissingFailureAnalyzer,\ org.springframework.boot.autoconfigure.jdbc.DataSourceBeanCreationFailureAnalyzer,\ org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer,\ org.springframework.boot.autoconfigure.session.NonUniqueSessionRepositoryFailureAnalyzer diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java index 374484c975..ba1f1dd953 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java @@ -80,8 +80,25 @@ public class FlywayAutoConfigurationTests { @Test public void noDataSource() { + this.contextRunner.run((context) -> { + assertThat(context).hasFailed(); + assertThat(context).getFailure().isInstanceOf(BeanCreationException.class); + assertThat(context).getFailure() + .hasRootCauseInstanceOf(FlywayDataSourceMissingException.class); + assertThat(context).getFailure() + .hasMessageContaining("Flyway is present in classpath and enabled"); + }); + } + + @Test + public void noDataSourceCreateOneWithUrl() { this.contextRunner - .run((context) -> assertThat(context).doesNotHaveBean(Flyway.class)); + .withPropertyValues( + "spring.flyway.url:jdbc:hsqldb:mem:" + UUID.randomUUID()) + .run((context) -> { + assertThat(context).hasSingleBean(Flyway.class); + assertThat(context.getBean(Flyway.class).getDataSource()).isNotNull(); + }); } @Test diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java index 86c264c30a..a47cd39288 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java @@ -79,8 +79,26 @@ public class LiquibaseAutoConfigurationTests { @Test public void noDataSource() { - this.contextRunner.run( - (context) -> assertThat(context).doesNotHaveBean(SpringLiquibase.class)); + this.contextRunner.run((context) -> { + assertThat(context).hasFailed(); + assertThat(context).getFailure().isInstanceOf(BeanCreationException.class); + assertThat(context).getFailure() + .hasRootCauseInstanceOf(LiquibaseDataSourceMissingException.class); + assertThat(context).getFailure().hasMessageContaining( + "Liquibase is present in classpath and enabled"); + }); + } + + @Test + public void noDataSourceCreateOneWithUrl() { + this.contextRunner + .withPropertyValues("spring.liquibase.url:jdbc:hsqldb:mem:liquibase") + .run(assertLiquibase((liquibase) -> { + DataSource dataSource = liquibase.getDataSource(); + assertThat(((HikariDataSource) dataSource).isClosed()).isTrue(); + assertThat(((HikariDataSource) dataSource).getJdbcUrl()) + .isEqualTo("jdbc:hsqldb:mem:liquibase"); + })); } @Test diff --git a/spring-boot-project/spring-boot-docs/src/main/groovy/generateConfigurationPropertyTables.groovy b/spring-boot-project/spring-boot-docs/src/main/groovy/generateConfigurationPropertyTables.groovy index 56313059b3..5aaac998fd 100644 --- a/spring-boot-project/spring-boot-docs/src/main/groovy/generateConfigurationPropertyTables.groovy +++ b/spring-boot-project/spring-boot-docs/src/main/groovy/generateConfigurationPropertyTables.groovy @@ -1,3 +1,5 @@ +import org.springframework.boot.autoconfigure.flyway.FlywayProperties +import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties import org.springframework.boot.configurationdocs.ConfigurationMetadataDocumentWriter import org.springframework.boot.configurationdocs.DocumentOptions import org.springframework.core.io.UrlResource @@ -48,7 +50,7 @@ def generateConfigMetadataDocumentation() { .addSection("security") .withKeyPrefixes("spring.security", "spring.ldap", "spring.session") .addSection("data-migration") - .withKeyPrefixes("spring.flyway", "spring.liquibase") + .withKeyPrefixes(FlywayProperties.PROPERTIES_PREFIX, LiquibaseProperties.PROPERTIES_PREFIX) .addSection("data") .withKeyPrefixes("spring.couchbase", "spring.elasticsearch", "spring.h2", "spring.influx", "spring.mongodb", "spring.redis", From 880721557bf92e4b230f5f2cfff71e23bbfbfcc9 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 29 May 2019 10:46:55 +0100 Subject: [PATCH 2/2] Polish "Auto-configure Flyway and Liquibase when there's a URL but no DataSource" See gh-16850 --- .../flyway/FlywayAutoConfiguration.java | 31 ++++++++++++--- .../FlywayDataSourceMissingException.java | 34 ---------------- ...lywayDataSourceMissingFailureAnalyzer.java | 39 ------------------- .../flyway/FlywayProperties.java | 4 +- .../liquibase/LiquibaseAutoConfiguration.java | 36 +++++++++++++---- .../LiquibaseDataSourceMissingException.java | 34 ---------------- ...ibaseDataSourceMissingFailureAnalyzer.java | 39 ------------------- .../liquibase/LiquibaseProperties.java | 9 +---- .../main/resources/META-INF/spring.factories | 2 - .../flyway/FlywayAutoConfigurationTests.java | 14 ++----- .../LiquibaseAutoConfigurationTests.java | 14 ++----- ...generateConfigurationPropertyTables.groovy | 4 +- 12 files changed, 64 insertions(+), 196 deletions(-) delete mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayDataSourceMissingException.java delete mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayDataSourceMissingFailureAnalyzer.java delete mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSourceMissingException.java delete mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSourceMissingFailureAnalyzer.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java index 4d585064f6..160097dc0d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java @@ -36,11 +36,13 @@ import org.flywaydb.core.api.configuration.FluentConfiguration; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.data.jpa.EntityManagerFactoryDependsOnPostProcessor; +import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration.FlywayDataSourceCondition; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.autoconfigure.jdbc.JdbcOperationsDependsOnPostProcessor; @@ -52,6 +54,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.boot.jdbc.DatabaseDriver; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.GenericConverter; @@ -82,8 +85,8 @@ import org.springframework.util.StringUtils; @SuppressWarnings("deprecation") @Configuration(proxyBeanMethods = false) @ConditionalOnClass(Flyway.class) -@ConditionalOnProperty(prefix = FlywayProperties.PROPERTIES_PREFIX, name = "enabled", - matchIfMissing = true) +@Conditional(FlywayDataSourceCondition.class) +@ConditionalOnProperty(prefix = "spring.flyway", name = "enabled", matchIfMissing = true) @AutoConfigureAfter({ DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class, HibernateJpaAutoConfiguration.class }) public class FlywayAutoConfiguration { @@ -152,11 +155,8 @@ public class FlywayAutoConfiguration { else if (flywayDataSource != null) { configuration.dataSource(flywayDataSource); } - else if (dataSource != null) { - configuration.dataSource(dataSource); - } else { - throw new FlywayDataSourceMissingException(); + configuration.dataSource(dataSource); } return configuration.getDataSource(); } @@ -465,4 +465,23 @@ public class FlywayAutoConfiguration { } + static final class FlywayDataSourceCondition extends AnyNestedCondition { + + FlywayDataSourceCondition() { + super(ConfigurationPhase.REGISTER_BEAN); + } + + @ConditionalOnBean(DataSource.class) + private static final class DataSourceBeanCondition { + + } + + @ConditionalOnProperty(prefix = "spring.flyway", name = "url", + matchIfMissing = false) + private static final class FlywayUrlCondition { + + } + + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayDataSourceMissingException.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayDataSourceMissingException.java deleted file mode 100644 index 75801a5420..0000000000 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayDataSourceMissingException.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2012-2019 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. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.autoconfigure.flyway; - -/** - * Exception thrown when no Flyway is enabled and present in classpath but no datasource - * was available or configured for it. - * - * @author Ilya Lukyanovich - */ -public class FlywayDataSourceMissingException extends RuntimeException { - - FlywayDataSourceMissingException() { - super("Flyway is present in classpath and enabled but " - + "no DataSource bean neither " + FlywayProperties.PROPERTIES_PREFIX - + ".url and " + FlywayProperties.PROPERTIES_PREFIX - + ".user configuration was provided"); - } - -} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayDataSourceMissingFailureAnalyzer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayDataSourceMissingFailureAnalyzer.java deleted file mode 100644 index f0ac5a4969..0000000000 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayDataSourceMissingFailureAnalyzer.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2012-2019 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. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.autoconfigure.flyway; - -import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; -import org.springframework.boot.diagnostics.FailureAnalysis; - -/** - * A {@code FailureAnalyzer} that performs analysis of failures caused by a - * {@link FlywayDataSourceMissingException}. - * - * @author Ilya Lukyanovich - */ -class FlywayDataSourceMissingFailureAnalyzer - extends AbstractFailureAnalyzer { - - @Override - protected FailureAnalysis analyze(Throwable rootFailure, - FlywayDataSourceMissingException cause) { - return new FailureAnalysis("No DataSource found for flyway", - "Please provide a javax.sql.DataSource bean or flyway datasource configuration", - cause); - } - -} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java index 7a8b1b5d00..0acd2171b7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java @@ -35,11 +35,9 @@ import org.springframework.boot.context.properties.ConfigurationProperties; * @author Stephane Nicoll * @since 1.1.0 */ -@ConfigurationProperties(prefix = FlywayProperties.PROPERTIES_PREFIX) +@ConfigurationProperties(prefix = "spring.flyway") public class FlywayProperties { - public static final String PROPERTIES_PREFIX = "spring.flyway"; - /** * Whether to enable flyway. */ diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java index 4bd6116d13..d97c566cd3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java @@ -28,6 +28,7 @@ import liquibase.integration.spring.SpringLiquibase; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -37,10 +38,12 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.autoconfigure.jdbc.JdbcOperationsDependsOnPostProcessor; import org.springframework.boot.autoconfigure.jdbc.NamedParameterJdbcOperationsDependsOnPostProcessor; +import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration.LiquibaseDataSourceCondition; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.io.Resource; @@ -65,8 +68,9 @@ import org.springframework.util.Assert; */ @Configuration(proxyBeanMethods = false) @ConditionalOnClass({ SpringLiquibase.class, DatabaseChange.class }) -@ConditionalOnProperty(prefix = LiquibaseProperties.PROPERTIES_PREFIX, name = "enabled", +@ConditionalOnProperty(prefix = "spring.liquibase", name = "enabled", matchIfMissing = true) +@Conditional(LiquibaseDataSourceCondition.class) @AutoConfigureAfter({ DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class }) public class LiquibaseAutoConfiguration { @@ -140,12 +144,9 @@ public class LiquibaseAutoConfiguration { liquibase.setDataSource(liquibaseDataSource); return liquibase; } - else if (this.properties.isCreateDataSource()) { - SpringLiquibase liquibase = new DataSourceClosingSpringLiquibase(); - liquibase.setDataSource(createNewDataSource(dataSourceProperties)); - return liquibase; - } - throw new LiquibaseDataSourceMissingException(); + SpringLiquibase liquibase = new DataSourceClosingSpringLiquibase(); + liquibase.setDataSource(createNewDataSource(dataSourceProperties)); + return liquibase; } private DataSource getDataSource(DataSource liquibaseDataSource, @@ -153,7 +154,7 @@ public class LiquibaseAutoConfiguration { if (liquibaseDataSource != null) { return liquibaseDataSource; } - if (!this.properties.isCreateDataSource()) { + if (this.properties.getUrl() == null && this.properties.getUser() == null) { return dataSource; } return null; @@ -227,4 +228,23 @@ public class LiquibaseAutoConfiguration { } + static final class LiquibaseDataSourceCondition extends AnyNestedCondition { + + LiquibaseDataSourceCondition() { + super(ConfigurationPhase.REGISTER_BEAN); + } + + @ConditionalOnBean(DataSource.class) + private static final class DataSourceBeanCondition { + + } + + @ConditionalOnProperty(prefix = "spring.liquibase", name = "url", + matchIfMissing = false) + private static final class LiquibaseUrlCondition { + + } + + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSourceMissingException.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSourceMissingException.java deleted file mode 100644 index bcd08fe01e..0000000000 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSourceMissingException.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2012-2019 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. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.autoconfigure.liquibase; - -/** - * Exception thrown when Liquibase is enabled and present in classpath but no datasource - * was available or configured for it. - * - * @author Ilya Lukyanovich - */ -public class LiquibaseDataSourceMissingException extends RuntimeException { - - LiquibaseDataSourceMissingException() { - super("Liquibase is present in classpath and enabled but " - + "no DataSource bean neither " + LiquibaseProperties.PROPERTIES_PREFIX - + ".url and " + LiquibaseProperties.PROPERTIES_PREFIX - + ".user configuration was provided"); - } - -} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSourceMissingFailureAnalyzer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSourceMissingFailureAnalyzer.java deleted file mode 100644 index 0114a4373c..0000000000 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSourceMissingFailureAnalyzer.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2012-2019 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. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.autoconfigure.liquibase; - -import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; -import org.springframework.boot.diagnostics.FailureAnalysis; - -/** - * A {@code FailureAnalyzer} that performs analysis of failures caused by a - * {@link LiquibaseDataSourceMissingException}. - * - * @author Ilya Lukyanovich - */ -class LiquibaseDataSourceMissingFailureAnalyzer - extends AbstractFailureAnalyzer { - - @Override - protected FailureAnalysis analyze(Throwable rootFailure, - LiquibaseDataSourceMissingException cause) { - return new FailureAnalysis("No DataSource found for liquibsae", - "Please provide a javax.sql.DataSource bean or liquibase datasource configuration", - cause); - } - -} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseProperties.java index a4087b0f32..edf20d7d61 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseProperties.java @@ -30,12 +30,9 @@ import org.springframework.util.Assert; * @author Marcel Overdijk * @since 1.1.0 */ -@ConfigurationProperties(prefix = LiquibaseProperties.PROPERTIES_PREFIX, - ignoreUnknownFields = false) +@ConfigurationProperties(prefix = "spring.liquibase", ignoreUnknownFields = false) public class LiquibaseProperties { - public static final String PROPERTIES_PREFIX = "spring.liquibase"; - /** * Change log configuration path. */ @@ -219,10 +216,6 @@ public class LiquibaseProperties { this.password = password; } - public boolean isCreateDataSource() { - return this.url != null || this.user != null; - } - public String getUrl() { return this.url; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index df62d6abb4..5516525298 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -146,8 +146,6 @@ org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAuto org.springframework.boot.diagnostics.FailureAnalyzer=\ org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzer,\ org.springframework.boot.autoconfigure.flyway.FlywayMigrationScriptMissingFailureAnalyzer,\ -org.springframework.boot.autoconfigure.flyway.FlywayDataSourceMissingFailureAnalyzer,\ -org.springframework.boot.autoconfigure.liquibase.LiquibaseDataSourceMissingFailureAnalyzer,\ org.springframework.boot.autoconfigure.jdbc.DataSourceBeanCreationFailureAnalyzer,\ org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer,\ org.springframework.boot.autoconfigure.session.NonUniqueSessionRepositoryFailureAnalyzer diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java index ba1f1dd953..2140ee6cdc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java @@ -79,19 +79,13 @@ public class FlywayAutoConfigurationTests { .withPropertyValues("spring.datasource.generate-unique-name=true"); @Test - public void noDataSource() { - this.contextRunner.run((context) -> { - assertThat(context).hasFailed(); - assertThat(context).getFailure().isInstanceOf(BeanCreationException.class); - assertThat(context).getFailure() - .hasRootCauseInstanceOf(FlywayDataSourceMissingException.class); - assertThat(context).getFailure() - .hasMessageContaining("Flyway is present in classpath and enabled"); - }); + public void backsOffWithNoDataSourceBeanAndNoFlywayUrl() { + this.contextRunner + .run((context) -> assertThat(context).doesNotHaveBean(Flyway.class)); } @Test - public void noDataSourceCreateOneWithUrl() { + public void createsDataSourceWithNoDataSourceBeanAndFlywayUrl() { this.contextRunner .withPropertyValues( "spring.flyway.url:jdbc:hsqldb:mem:" + UUID.randomUUID()) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java index a47cd39288..3619b25c9e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java @@ -78,19 +78,13 @@ public class LiquibaseAutoConfigurationTests { .withPropertyValues("spring.datasource.generate-unique-name=true"); @Test - public void noDataSource() { - this.contextRunner.run((context) -> { - assertThat(context).hasFailed(); - assertThat(context).getFailure().isInstanceOf(BeanCreationException.class); - assertThat(context).getFailure() - .hasRootCauseInstanceOf(LiquibaseDataSourceMissingException.class); - assertThat(context).getFailure().hasMessageContaining( - "Liquibase is present in classpath and enabled"); - }); + public void backsOffWithNoDataSourceBeanAndNoLiquibaseUrl() { + this.contextRunner.run( + (context) -> assertThat(context).doesNotHaveBean(SpringLiquibase.class)); } @Test - public void noDataSourceCreateOneWithUrl() { + public void createsDataSourceWithNoDataSourceBeanAndLiquibaseUrl() { this.contextRunner .withPropertyValues("spring.liquibase.url:jdbc:hsqldb:mem:liquibase") .run(assertLiquibase((liquibase) -> { diff --git a/spring-boot-project/spring-boot-docs/src/main/groovy/generateConfigurationPropertyTables.groovy b/spring-boot-project/spring-boot-docs/src/main/groovy/generateConfigurationPropertyTables.groovy index 5aaac998fd..56313059b3 100644 --- a/spring-boot-project/spring-boot-docs/src/main/groovy/generateConfigurationPropertyTables.groovy +++ b/spring-boot-project/spring-boot-docs/src/main/groovy/generateConfigurationPropertyTables.groovy @@ -1,5 +1,3 @@ -import org.springframework.boot.autoconfigure.flyway.FlywayProperties -import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties import org.springframework.boot.configurationdocs.ConfigurationMetadataDocumentWriter import org.springframework.boot.configurationdocs.DocumentOptions import org.springframework.core.io.UrlResource @@ -50,7 +48,7 @@ def generateConfigMetadataDocumentation() { .addSection("security") .withKeyPrefixes("spring.security", "spring.ldap", "spring.session") .addSection("data-migration") - .withKeyPrefixes(FlywayProperties.PROPERTIES_PREFIX, LiquibaseProperties.PROPERTIES_PREFIX) + .withKeyPrefixes("spring.flyway", "spring.liquibase") .addSection("data") .withKeyPrefixes("spring.couchbase", "spring.elasticsearch", "spring.h2", "spring.influx", "spring.mongodb", "spring.redis",