Merge pull request #6604 from Eddú Meléndez
* gh-6604: Ensure that @LiquibaseDataSource is preferred to “normal” DataSource Allow a DataSource to be marked as being specifically for Liquibase
This commit is contained in:
commit
d41d732172
|
|
@ -23,6 +23,7 @@ import javax.sql.DataSource;
|
||||||
import liquibase.integration.spring.SpringLiquibase;
|
import liquibase.integration.spring.SpringLiquibase;
|
||||||
import liquibase.servicelocator.ServiceLocator;
|
import liquibase.servicelocator.ServiceLocator;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
|
|
@ -50,6 +51,7 @@ import org.springframework.util.Assert;
|
||||||
* @author Marcel Overdijk
|
* @author Marcel Overdijk
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Eddú Meléndez
|
||||||
* @since 1.1.0
|
* @since 1.1.0
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
|
|
@ -72,11 +74,15 @@ public class LiquibaseAutoConfiguration {
|
||||||
|
|
||||||
private final DataSource dataSource;
|
private final DataSource dataSource;
|
||||||
|
|
||||||
|
private final DataSource liquibaseDataSource;
|
||||||
|
|
||||||
public LiquibaseConfiguration(LiquibaseProperties properties,
|
public LiquibaseConfiguration(LiquibaseProperties properties,
|
||||||
ResourceLoader resourceLoader, DataSource dataSource) {
|
ResourceLoader resourceLoader, DataSource dataSource,
|
||||||
|
@LiquibaseDataSource ObjectProvider<DataSource> liquibaseDataSourceProvider) {
|
||||||
this.properties = properties;
|
this.properties = properties;
|
||||||
this.resourceLoader = resourceLoader;
|
this.resourceLoader = resourceLoader;
|
||||||
this.dataSource = dataSource;
|
this.dataSource = dataSource;
|
||||||
|
this.liquibaseDataSource = liquibaseDataSourceProvider.getIfAvailable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
|
|
@ -110,7 +116,10 @@ public class LiquibaseAutoConfiguration {
|
||||||
}
|
}
|
||||||
|
|
||||||
private DataSource getDataSource() {
|
private DataSource getDataSource() {
|
||||||
if (this.properties.getUrl() == null) {
|
if (this.liquibaseDataSource != null) {
|
||||||
|
return this.liquibaseDataSource;
|
||||||
|
}
|
||||||
|
else if (this.properties.getUrl() == null) {
|
||||||
return this.dataSource;
|
return this.dataSource;
|
||||||
}
|
}
|
||||||
return DataSourceBuilder.create().url(this.properties.getUrl())
|
return DataSourceBuilder.create().url(this.properties.getUrl())
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2016 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.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Qualifier annotation for a DataSource to be injected in to Liquibase. If used for a
|
||||||
|
* second data source, the other (main) one would normally be marked as {@code @Primary}.
|
||||||
|
*
|
||||||
|
* @author Eddú Meléndez
|
||||||
|
* @since 1.4.1
|
||||||
|
*/
|
||||||
|
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE,
|
||||||
|
ElementType.ANNOTATION_TYPE })
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
@Qualifier
|
||||||
|
public @interface LiquibaseDataSource {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -19,6 +19,8 @@ package org.springframework.boot.autoconfigure.liquibase;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import liquibase.integration.spring.SpringLiquibase;
|
import liquibase.integration.spring.SpringLiquibase;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
|
@ -29,10 +31,14 @@ import org.junit.rules.TemporaryFolder;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanCreationException;
|
import org.springframework.beans.factory.BeanCreationException;
|
||||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
||||||
import org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger;
|
import org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger;
|
||||||
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
import org.springframework.test.util.ReflectionTestUtils;
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
import org.springframework.util.FileCopyUtils;
|
import org.springframework.util.FileCopyUtils;
|
||||||
|
|
||||||
|
|
@ -43,6 +49,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
*
|
*
|
||||||
* @author Marcel Overdijk
|
* @author Marcel Overdijk
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Eddú Meléndez
|
||||||
*/
|
*/
|
||||||
public class LiquibaseAutoConfigurationTests {
|
public class LiquibaseAutoConfigurationTests {
|
||||||
|
|
||||||
|
|
@ -241,4 +248,34 @@ public class LiquibaseAutoConfigurationTests {
|
||||||
assertThat(content).contains("DROP TABLE PUBLIC.customer;");
|
assertThat(content).contains("DROP TABLE PUBLIC.customer;");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLiquibaseDateSource() {
|
||||||
|
this.context.register(LiquibaseDataSourceConfiguration.class,
|
||||||
|
EmbeddedDataSourceConfiguration.class, LiquibaseAutoConfiguration.class,
|
||||||
|
PropertyPlaceholderAutoConfiguration.class);
|
||||||
|
this.context.refresh();
|
||||||
|
SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class);
|
||||||
|
assertThat(liquibase.getDataSource())
|
||||||
|
.isEqualTo(this.context.getBean("liquibaseDataSource"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class LiquibaseDataSourceConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Primary
|
||||||
|
public DataSource normalDataSource() {
|
||||||
|
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:normal").username("sa")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@LiquibaseDataSource
|
||||||
|
@Bean
|
||||||
|
public DataSource liquibaseDataSource() {
|
||||||
|
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:liquibasetest")
|
||||||
|
.username("sa").build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue