Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
192fe929
Commit
192fe929
authored
Jan 26, 2018
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish "Use custom DataSource if Flyway or Liquibase has user or url"
Closes gh-11751
parent
5d3cd23e
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
40 additions
and
31 deletions
+40
-31
FlywayAutoConfiguration.java
...rk/boot/autoconfigure/flyway/FlywayAutoConfiguration.java
+17
-15
FlywayProperties.java
...framework/boot/autoconfigure/flyway/FlywayProperties.java
+1
-1
LiquibaseAutoConfiguration.java
...t/autoconfigure/liquibase/LiquibaseAutoConfiguration.java
+13
-12
FlywayAutoConfigurationTests.java
...ot/autoconfigure/flyway/FlywayAutoConfigurationTests.java
+1
-1
howto.adoc
...oot-project/spring-boot-docs/src/main/asciidoc/howto.adoc
+8
-2
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java
View file @
192fe929
/*
* Copyright 2012-201
7
the original author or authors.
* Copyright 2012-201
8
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
<
String
>
property
,
Supplier
<
String
>
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)"
);
}
}
...
...
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java
View file @
192fe929
/*
* Copyright 2012-201
7
the original author or authors.
* Copyright 2012-201
8
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.
...
...
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java
View file @
192fe929
...
...
@@ -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
<
String
>
property
,
Supplier
<
String
>
defaultValue
)
{
String
value
=
property
.
get
();
return
value
==
null
?
defaultValue
.
get
()
:
value
;
}
}
/**
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java
View file @
192fe929
/*
* Copyright 2012-201
7
the original author or authors.
* Copyright 2012-201
8
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.
...
...
spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc
View file @
192fe929
...
...
@@ -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`]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment