Polish "Use custom DataSource if Flyway or Liquibase has user or url"

Closes gh-11751
pull/11784/merge
Andy Wilkinson 7 years ago
parent 5d3cd23eed
commit 192fe929c7

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.Supplier;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -129,18 +130,12 @@ public class FlywayAutoConfiguration {
public Flyway flyway() { public Flyway flyway() {
Flyway flyway = new SpringBootFlyway(); Flyway flyway = new SpringBootFlyway();
if (this.properties.isCreateDataSource()) { if (this.properties.isCreateDataSource()) {
String url = this.properties.getUrl() == null String url = getProperty(this.properties::getUrl,
? this.dataSourceProperties.getUrl() this.dataSourceProperties::getUrl);
: this.properties.getUrl(); String user = getProperty(this.properties::getUser,
this.dataSourceProperties::getUsername);
String user = this.properties.getUser() == null String password = getProperty(this.properties::getPassword,
? this.dataSourceProperties.getUsername() this.dataSourceProperties::getPassword);
: this.properties.getUser();
String password = this.properties.getPassword() == null
? this.dataSourceProperties.getPassword()
: this.properties.getPassword();
flyway.setDataSource(url, user, password, flyway.setDataSource(url, user, password,
this.properties.getInitSqls().toArray(new String[0])); this.properties.getInitSqls().toArray(new String[0]));
} }
@ -159,13 +154,20 @@ public class FlywayAutoConfiguration {
return flyway; return flyway;
} }
private String getProperty(Supplier<String> property,
Supplier<String> defaultValue) {
String value = property.get();
return value == null ? defaultValue.get() : value;
}
private void checkLocationExists(String... locations) { private void checkLocationExists(String... locations) {
if (this.properties.isCheckLocation()) { if (this.properties.isCheckLocation()) {
Assert.state(locations.length != 0, Assert.state(locations.length != 0,
"Migration script locations not configured"); "Migration script locations not configured");
boolean exists = hasAtLeastOneLocation(locations); boolean exists = hasAtLeastOneLocation(locations);
Assert.state(exists, () -> "Cannot find migrations location in: " Assert.state(exists,
+ Arrays.asList(locations) () -> "Cannot find migrations location in: " + Arrays.asList(
locations)
+ " (please add migrations or check your Flyway configuration)"); + " (please add migrations or check your Flyway configuration)");
} }
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.liquibase;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Supplier;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
@ -153,22 +154,22 @@ public class LiquibaseAutoConfiguration {
} }
private DataSource createNewDataSource() { private DataSource createNewDataSource() {
String url = this.properties.getUrl() == null String url = getProperty(this.properties::getUrl,
? this.dataSourceProperties.getUrl() this.dataSourceProperties::getUrl);
: this.properties.getUrl(); String user = getProperty(this.properties::getUser,
this.dataSourceProperties::getUsername);
String user = this.properties.getUser() == null String password = getProperty(this.properties::getPassword,
? this.dataSourceProperties.getUsername() this.dataSourceProperties::getPassword);
: this.properties.getUser();
String password = this.properties.getPassword() == null
? this.dataSourceProperties.getPassword()
: this.properties.getPassword();
return DataSourceBuilder.create().url(url).username(user).password(password) return DataSourceBuilder.create().url(url).username(user).password(password)
.build(); .build();
} }
private String getProperty(Supplier<String> property,
Supplier<String> defaultValue) {
String value = property.get();
return value == null ? defaultValue.get() : value;
}
} }
/** /**

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

@ -2139,7 +2139,10 @@ uses that for migrations. If you like to use a different `DataSource`, you can c
one and mark its `@Bean` as `@FlywayDataSource`. If you do so and want two data sources, one and mark its `@Bean` as `@FlywayDataSource`. If you do so and want two data sources,
remember to create another one and mark it as `@Primary`. Alternatively, you can use remember to create another one and mark it as `@Primary`. Alternatively, you can use
Flyway's native `DataSource` by setting `spring.flyway.[url,user,password]` Flyway's native `DataSource` by setting `spring.flyway.[url,user,password]`
in external properties. in external properties. Setting either `spring.flyway.url` or `spring.flyway.user`
is sufficent to cause Flyway to use its own `DataSource`. If any of the three
properties has not be set, the value of its equivalent `spring.datasource` property will
be used.
There is a {github-code}/spring-boot-samples/spring-boot-sample-flyway[Flyway sample] so There is a {github-code}/spring-boot-samples/spring-boot-sample-flyway[Flyway sample] so
that you can see how to set things up. that you can see how to set things up.
@ -2175,7 +2178,10 @@ that for migrations. If you need to use a different `DataSource`, you can create
mark its `@Bean` as `@LiquibaseDataSource`. If you do so and you want two data sources, mark its `@Bean` as `@LiquibaseDataSource`. If you do so and you want two data sources,
remember to create another one and mark it as `@Primary`. Alternatively, you can use remember to create another one and mark it as `@Primary`. Alternatively, you can use
Liquibase's native `DataSource` by setting `spring.liquibase.[url,user,password]` in Liquibase's native `DataSource` by setting `spring.liquibase.[url,user,password]` in
external properties. external properties. Setting either `spring.liquibase.url` or `spring.liquibase.user`
is sufficent to cause Liquibase to use its own `DataSource`. If any of the three
properties has not be set, the value of its equivalent `spring.datasource` property will
be used.
See See
{sc-spring-boot-autoconfigure}/liquibase/LiquibaseProperties.{sc-ext}[`LiquibaseProperties`] {sc-spring-boot-autoconfigure}/liquibase/LiquibaseProperties.{sc-ext}[`LiquibaseProperties`]

Loading…
Cancel
Save