Commit 7c54c3ff authored by Andy Wilkinson's avatar Andy Wilkinson

Ensure that @LiquibaseDataSource is preferred to “normal” DataSource

Closes gh-6604
parent 8424965a
...@@ -116,12 +116,12 @@ public class LiquibaseAutoConfiguration { ...@@ -116,12 +116,12 @@ public class LiquibaseAutoConfiguration {
} }
private DataSource getDataSource() { private DataSource getDataSource() {
if (this.properties.getUrl() == null) { if (this.liquibaseDataSource != null) {
return this.dataSource;
}
else if (this.liquibaseDataSource != null) {
return this.liquibaseDataSource; return this.liquibaseDataSource;
} }
else if (this.properties.getUrl() == null) {
return this.dataSource;
}
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();
......
...@@ -25,8 +25,8 @@ import java.lang.annotation.Target; ...@@ -25,8 +25,8 @@ import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
/** /**
* Qualifier annotation for a DataSource to be injected in to Liquibase. If used for a second * Qualifier annotation for a DataSource to be injected in to Liquibase. If used for a
* data source, the other (main) one would normally be marked as {@code @Primary}. * second data source, the other (main) one would normally be marked as {@code @Primary}.
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @since 1.4.1 * @since 1.4.1
......
...@@ -38,6 +38,7 @@ import org.springframework.boot.test.util.EnvironmentTestUtils; ...@@ -38,6 +38,7 @@ 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.Bean;
import org.springframework.context.annotation.Configuration; 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;
...@@ -254,12 +255,20 @@ public class LiquibaseAutoConfigurationTests { ...@@ -254,12 +255,20 @@ public class LiquibaseAutoConfigurationTests {
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class);
assertThat(liquibase.getDataSource()).isNotNull(); assertThat(liquibase.getDataSource())
.isEqualTo(this.context.getBean("liquibaseDataSource"));
} }
@Configuration @Configuration
static class LiquibaseDataSourceConfiguration { static class LiquibaseDataSourceConfiguration {
@Bean
@Primary
public DataSource normalDataSource() {
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:normal").username("sa")
.build();
}
@LiquibaseDataSource @LiquibaseDataSource
@Bean @Bean
public DataSource liquibaseDataSource() { public DataSource liquibaseDataSource() {
......
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