Merge branch '2.0.x'
This commit is contained in:
commit
e53e8bf79f
|
@ -18,6 +18,8 @@ package org.springframework.boot.actuate.autoconfigure.liquibase;
|
||||||
|
|
||||||
import liquibase.integration.spring.SpringLiquibase;
|
import liquibase.integration.spring.SpringLiquibase;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
|
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
|
||||||
import org.springframework.boot.actuate.liquibase.LiquibaseEndpoint;
|
import org.springframework.boot.actuate.liquibase.LiquibaseEndpoint;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
|
@ -25,6 +27,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.boot.autoconfigure.liquibase.DataSourceClosingSpringLiquibase;
|
||||||
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
|
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
@ -49,4 +52,29 @@ public class LiquibaseEndpointAutoConfiguration {
|
||||||
return new LiquibaseEndpoint(context);
|
return new LiquibaseEndpoint(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnBean(SpringLiquibase.class)
|
||||||
|
@ConditionalOnEnabledEndpoint(endpoint = LiquibaseEndpoint.class)
|
||||||
|
public static BeanPostProcessor preventDataSourceCloseBeanPostProcessor() {
|
||||||
|
return new BeanPostProcessor() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||||
|
throws BeansException {
|
||||||
|
if (bean instanceof DataSourceClosingSpringLiquibase) {
|
||||||
|
((DataSourceClosingSpringLiquibase) bean)
|
||||||
|
.setCloseDataSourceOnceMigrated(false);
|
||||||
|
}
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object postProcessAfterInitialization(Object bean, String beanName)
|
||||||
|
throws BeansException {
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,11 +16,13 @@
|
||||||
|
|
||||||
package org.springframework.boot.actuate.autoconfigure.liquibase;
|
package org.springframework.boot.actuate.autoconfigure.liquibase;
|
||||||
|
|
||||||
|
import liquibase.exception.LiquibaseException;
|
||||||
import liquibase.integration.spring.SpringLiquibase;
|
import liquibase.integration.spring.SpringLiquibase;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.boot.actuate.liquibase.LiquibaseEndpoint;
|
import org.springframework.boot.actuate.liquibase.LiquibaseEndpoint;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
|
import org.springframework.boot.autoconfigure.liquibase.DataSourceClosingSpringLiquibase;
|
||||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
@ -37,23 +39,48 @@ public class LiquibaseEndpointAutoConfigurationTests {
|
||||||
|
|
||||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
.withConfiguration(
|
.withConfiguration(
|
||||||
AutoConfigurations.of(LiquibaseEndpointAutoConfiguration.class))
|
AutoConfigurations.of(LiquibaseEndpointAutoConfiguration.class));
|
||||||
.withUserConfiguration(LiquibaseConfiguration.class);
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void runShouldHaveEndpointBean() {
|
public void runShouldHaveEndpointBean() {
|
||||||
this.contextRunner.run(
|
this.contextRunner.withUserConfiguration(LiquibaseConfiguration.class).run(
|
||||||
(context) -> assertThat(context).hasSingleBean(LiquibaseEndpoint.class));
|
(context) -> assertThat(context).hasSingleBean(LiquibaseEndpoint.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void runWhenEnabledPropertyIsFalseShouldNotHaveEndpointBean() {
|
public void runWhenEnabledPropertyIsFalseShouldNotHaveEndpointBean() {
|
||||||
this.contextRunner
|
this.contextRunner.withUserConfiguration(LiquibaseConfiguration.class)
|
||||||
.withPropertyValues("management.endpoint.liquibase.enabled:false")
|
.withPropertyValues("management.endpoint.liquibase.enabled:false")
|
||||||
.run((context) -> assertThat(context)
|
.run((context) -> assertThat(context)
|
||||||
.doesNotHaveBean(LiquibaseEndpoint.class));
|
.doesNotHaveBean(LiquibaseEndpoint.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void disablesCloseOfDataSourceWhenEndpointIsEnabled() {
|
||||||
|
this.contextRunner
|
||||||
|
.withUserConfiguration(DataSourceClosingLiquibaseConfiguration.class)
|
||||||
|
.run((context) -> {
|
||||||
|
assertThat(context).hasSingleBean(LiquibaseEndpoint.class);
|
||||||
|
assertThat(context.getBean(DataSourceClosingSpringLiquibase.class))
|
||||||
|
.hasFieldOrPropertyWithValue("closeDataSourceOnceMigrated",
|
||||||
|
false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void doesNotDisableCloseOfDataSourceWhenEndpointIsDisabled() {
|
||||||
|
this.contextRunner
|
||||||
|
.withUserConfiguration(DataSourceClosingLiquibaseConfiguration.class)
|
||||||
|
.withPropertyValues("management.endpoint.liquibase.enabled:false")
|
||||||
|
.run((context) -> {
|
||||||
|
assertThat(context).doesNotHaveBean(LiquibaseEndpoint.class);
|
||||||
|
DataSourceClosingSpringLiquibase bean = context
|
||||||
|
.getBean(DataSourceClosingSpringLiquibase.class);
|
||||||
|
assertThat(bean).hasFieldOrPropertyWithValue(
|
||||||
|
"closeDataSourceOnceMigrated", true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
static class LiquibaseConfiguration {
|
static class LiquibaseConfiguration {
|
||||||
|
|
||||||
|
@ -64,4 +91,33 @@ public class LiquibaseEndpointAutoConfigurationTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class DataSourceClosingLiquibaseConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public SpringLiquibase liquibase() {
|
||||||
|
return new DataSourceClosingSpringLiquibase() {
|
||||||
|
|
||||||
|
private boolean propertiesSet = false;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setCloseDataSourceOnceMigrated(
|
||||||
|
boolean closeDataSourceOnceMigrated) {
|
||||||
|
if (this.propertiesSet) {
|
||||||
|
throw new IllegalStateException("setCloseDataSourceOnceMigrated "
|
||||||
|
+ "invoked after afterPropertiesSet");
|
||||||
|
}
|
||||||
|
super.setCloseDataSourceOnceMigrated(closeDataSourceOnceMigrated);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() throws LiquibaseException {
|
||||||
|
this.propertiesSet = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2018 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
|
||||||
|
*
|
||||||
|
* http://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 java.lang.reflect.Method;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import liquibase.exception.LiquibaseException;
|
||||||
|
import liquibase.integration.spring.SpringLiquibase;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.DisposableBean;
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A custom {@link SpringLiquibase} extension that closes the underlying
|
||||||
|
* {@link DataSource} once the database has been migrated.
|
||||||
|
*
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
* @since 2.0.6
|
||||||
|
*/
|
||||||
|
public class DataSourceClosingSpringLiquibase extends SpringLiquibase
|
||||||
|
implements DisposableBean {
|
||||||
|
|
||||||
|
private volatile boolean closeDataSourceOnceMigrated = true;
|
||||||
|
|
||||||
|
public void setCloseDataSourceOnceMigrated(boolean closeDataSourceOnceMigrated) {
|
||||||
|
this.closeDataSourceOnceMigrated = closeDataSourceOnceMigrated;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() throws LiquibaseException {
|
||||||
|
super.afterPropertiesSet();
|
||||||
|
if (this.closeDataSourceOnceMigrated) {
|
||||||
|
closeDataSource();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void closeDataSource() {
|
||||||
|
Class<?> dataSourceClass = getDataSource().getClass();
|
||||||
|
Method closeMethod = ReflectionUtils.findMethod(dataSourceClass, "close");
|
||||||
|
if (closeMethod != null) {
|
||||||
|
ReflectionUtils.invokeMethod(closeMethod, getDataSource());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() throws Exception {
|
||||||
|
if (!this.closeDataSourceOnceMigrated) {
|
||||||
|
closeDataSource();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.liquibase;
|
package org.springframework.boot.autoconfigure.liquibase;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
|
@ -24,7 +23,6 @@ import javax.persistence.EntityManagerFactory;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import liquibase.change.DatabaseChange;
|
import liquibase.change.DatabaseChange;
|
||||||
import liquibase.exception.LiquibaseException;
|
|
||||||
import liquibase.integration.spring.SpringLiquibase;
|
import liquibase.integration.spring.SpringLiquibase;
|
||||||
|
|
||||||
import org.springframework.beans.factory.ObjectProvider;
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
|
@ -50,7 +48,6 @@ import org.springframework.jdbc.core.JdbcOperations;
|
||||||
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
|
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
|
||||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ReflectionUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link EnableAutoConfiguration Auto-configuration} for Liquibase.
|
* {@link EnableAutoConfiguration Auto-configuration} for Liquibase.
|
||||||
|
@ -211,26 +208,4 @@ public class LiquibaseAutoConfiguration {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* A custom {@link SpringLiquibase} extension that closes the underlying
|
|
||||||
* {@link DataSource} once the database has been migrated.
|
|
||||||
*/
|
|
||||||
private static final class DataSourceClosingSpringLiquibase extends SpringLiquibase {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterPropertiesSet() throws LiquibaseException {
|
|
||||||
super.afterPropertiesSet();
|
|
||||||
closeDataSource();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void closeDataSource() {
|
|
||||||
Class<?> dataSourceClass = getDataSource().getClass();
|
|
||||||
Method closeMethod = ReflectionUtils.findMethod(dataSourceClass, "close");
|
|
||||||
if (closeMethod != null) {
|
|
||||||
ReflectionUtils.invokeMethod(closeMethod, getDataSource());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue