Commit 8424965a authored by Eddú Meléndez's avatar Eddú Meléndez Committed by Andy Wilkinson

Allow a DataSource to be marked as being specifically for Liquibase

This commit allows to configure a special DataSource to be used by
Liquibase by annotating it with @LiquibaseDataSource.

Closes gh-6614
parent 11ccc578
...@@ -23,6 +23,7 @@ import javax.sql.DataSource; ...@@ -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; ...@@ -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 { ...@@ -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
...@@ -113,6 +119,9 @@ public class LiquibaseAutoConfiguration { ...@@ -113,6 +119,9 @@ public class LiquibaseAutoConfiguration {
if (this.properties.getUrl() == null) { if (this.properties.getUrl() == null) {
return this.dataSource; return this.dataSource;
} }
else if (this.liquibaseDataSource != null) {
return this.liquibaseDataSource;
}
return DataSourceBuilder.create().url(this.properties.getUrl()) return DataSourceBuilder.create().url(this.properties.getUrl())
.username(this.properties.getUser()) .username(this.properties.getUser())
.password(this.properties.getPassword()).build(); .password(this.properties.getPassword()).build();
......
/*
* 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; ...@@ -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,13 @@ import org.junit.rules.TemporaryFolder; ...@@ -29,10 +31,13 @@ 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.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
...@@ -43,6 +48,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -43,6 +48,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 +247,26 @@ public class LiquibaseAutoConfigurationTests { ...@@ -241,4 +247,26 @@ 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()).isNotNull();
}
@Configuration
static class LiquibaseDataSourceConfiguration {
@LiquibaseDataSource
@Bean
public DataSource liquibaseDataSource() {
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:liquibasetest")
.username("sa").build();
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment