From 192fe929c7612a7fb1c5a5de74d62d64d1a76438 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 26 Jan 2018 14:35:28 +0000 Subject: [PATCH] Polish "Use custom DataSource if Flyway or Liquibase has user or url" Closes gh-11751 --- .../flyway/FlywayAutoConfiguration.java | 32 ++++++++++--------- .../flyway/FlywayProperties.java | 2 +- .../liquibase/LiquibaseAutoConfiguration.java | 25 ++++++++------- .../flyway/FlywayAutoConfigurationTests.java | 2 +- .../src/main/asciidoc/howto.adoc | 10 ++++-- 5 files changed, 40 insertions(+), 31 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java index 46a8f698d8..5ce8b0b5d5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java @@ -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"); * 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.List; import java.util.Set; +import java.util.function.Supplier; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; @@ -129,18 +130,12 @@ public class FlywayAutoConfiguration { public Flyway flyway() { Flyway flyway = new SpringBootFlyway(); if (this.properties.isCreateDataSource()) { - String url = this.properties.getUrl() == null - ? this.dataSourceProperties.getUrl() - : this.properties.getUrl(); - - String user = this.properties.getUser() == null - ? this.dataSourceProperties.getUsername() - : this.properties.getUser(); - - String password = this.properties.getPassword() == null - ? this.dataSourceProperties.getPassword() - : this.properties.getPassword(); - + String url = getProperty(this.properties::getUrl, + this.dataSourceProperties::getUrl); + String user = getProperty(this.properties::getUser, + this.dataSourceProperties::getUsername); + String password = getProperty(this.properties::getPassword, + this.dataSourceProperties::getPassword); flyway.setDataSource(url, user, password, this.properties.getInitSqls().toArray(new String[0])); } @@ -159,13 +154,20 @@ public class FlywayAutoConfiguration { return flyway; } + private String getProperty(Supplier property, + Supplier defaultValue) { + String value = property.get(); + return value == null ? defaultValue.get() : value; + } + private void checkLocationExists(String... locations) { if (this.properties.isCheckLocation()) { Assert.state(locations.length != 0, "Migration script locations not configured"); boolean exists = hasAtLeastOneLocation(locations); - Assert.state(exists, () -> "Cannot find migrations location in: " - + Arrays.asList(locations) + Assert.state(exists, + () -> "Cannot find migrations location in: " + Arrays.asList( + locations) + " (please add migrations or check your Flyway configuration)"); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java index 5eef9b8020..c5a460cc23 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java @@ -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"); * you may not use this file except in compliance with the License. diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java index 82fa8c741b..40ad4e4b9a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java @@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.liquibase; import java.lang.reflect.Method; import java.util.Collections; import java.util.List; +import java.util.function.Supplier; import javax.annotation.PostConstruct; import javax.persistence.EntityManagerFactory; @@ -153,22 +154,22 @@ public class LiquibaseAutoConfiguration { } private DataSource createNewDataSource() { - String url = this.properties.getUrl() == null - ? this.dataSourceProperties.getUrl() - : this.properties.getUrl(); - - String user = this.properties.getUser() == null - ? this.dataSourceProperties.getUsername() - : this.properties.getUser(); - - String password = this.properties.getPassword() == null - ? this.dataSourceProperties.getPassword() - : this.properties.getPassword(); - + String url = getProperty(this.properties::getUrl, + this.dataSourceProperties::getUrl); + String user = getProperty(this.properties::getUser, + this.dataSourceProperties::getUsername); + String password = getProperty(this.properties::getPassword, + this.dataSourceProperties::getPassword); return DataSourceBuilder.create().url(url).username(user).password(password) .build(); } + private String getProperty(Supplier property, + Supplier defaultValue) { + String value = property.get(); + return value == null ? defaultValue.get() : value; + } + } /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java index a205d1fd35..a082b776a3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java @@ -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"); * you may not use this file except in compliance with the License. diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc index 4bbb58325b..a007464bf6 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -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, 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]` -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 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, 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 -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 {sc-spring-boot-autoconfigure}/liquibase/LiquibaseProperties.{sc-ext}[`LiquibaseProperties`]