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
72862b56
Commit
72862b56
authored
Nov 19, 2017
by
Phillip Webb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish location check with vendor placeholder
Closes gh-10387
parent
1ee47cec
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
39 deletions
+68
-39
FlywayAutoConfiguration.java
...rk/boot/autoconfigure/flyway/FlywayAutoConfiguration.java
+64
-37
FlywayAutoConfigurationTests.java
...ot/autoconfigure/flyway/FlywayAutoConfigurationTests.java
+4
-2
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java
View file @
72862b56
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
flyway
;
package
org
.
springframework
.
boot
.
autoconfigure
.
flyway
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.HashSet
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.List
;
...
@@ -104,8 +105,6 @@ public class FlywayAutoConfiguration {
...
@@ -104,8 +105,6 @@ public class FlywayAutoConfiguration {
private
List
<
FlywayCallback
>
flywayCallbacks
;
private
List
<
FlywayCallback
>
flywayCallbacks
;
private
static
final
String
VENDOR_PLACEHOLDER
=
"{vendor}"
;
public
FlywayConfiguration
(
FlywayProperties
properties
,
public
FlywayConfiguration
(
FlywayProperties
properties
,
ResourceLoader
resourceLoader
,
ObjectProvider
<
DataSource
>
dataSource
,
ResourceLoader
resourceLoader
,
ObjectProvider
<
DataSource
>
dataSource
,
@FlywayDataSource
ObjectProvider
<
DataSource
>
flywayDataSource
,
@FlywayDataSource
ObjectProvider
<
DataSource
>
flywayDataSource
,
...
@@ -136,49 +135,22 @@ public class FlywayAutoConfiguration {
...
@@ -136,49 +135,22 @@ public class FlywayAutoConfiguration {
}
}
flyway
.
setCallbacks
(
this
.
flywayCallbacks
flyway
.
setCallbacks
(
this
.
flywayCallbacks
.
toArray
(
new
FlywayCallback
[
this
.
flywayCallbacks
.
size
()]));
.
toArray
(
new
FlywayCallback
[
this
.
flywayCallbacks
.
size
()]));
String
[]
locations
=
resolveLocations
(
this
.
properties
.
getLocations
().
toArray
(
new
String
[
0
]),
flyway
.
getDataSource
());
String
[]
locations
=
new
LocationResolver
(
flyway
.
getDataSource
())
.
resolveLocations
(
this
.
properties
.
getLocations
());
checkLocationExists
(
locations
);
checkLocationExists
(
locations
);
flyway
.
setLocations
(
locations
);
flyway
.
setLocations
(
locations
);
return
flyway
;
return
flyway
;
}
}
private
static
String
[]
resolveLocations
(
String
[]
locations
,
DataSource
dataSource
)
{
if
(
usesVendorLocation
(
locations
))
{
try
{
String
url
=
(
String
)
JdbcUtils
.
extractDatabaseMetaData
(
dataSource
,
"getURL"
);
DatabaseDriver
vendor
=
DatabaseDriver
.
fromJdbcUrl
(
url
);
if
(
vendor
!=
DatabaseDriver
.
UNKNOWN
)
{
for
(
int
i
=
0
;
i
<
locations
.
length
;
i
++)
{
locations
[
i
]
=
locations
[
i
].
replace
(
VENDOR_PLACEHOLDER
,
vendor
.
getId
());
}
}
}
catch
(
MetaDataAccessException
ex
)
{
throw
new
IllegalStateException
(
ex
);
}
}
return
locations
;
}
private
static
boolean
usesVendorLocation
(
String
...
locations
)
{
for
(
String
location
:
locations
)
{
if
(
location
.
contains
(
VENDOR_PLACEHOLDER
))
{
return
true
;
}
}
return
false
;
}
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
,
Assert
.
state
(
exists
,
()
->
"Cannot find migrations location in: "
+
Arrays
.
asList
(
locations
)
()
->
"Cannot find migrations location in: "
+
Arrays
.
asList
(
+
" (please add migrations or check your Flyway configuration)"
);
locations
)
+
" (please add migrations or check your Flyway configuration)"
);
}
}
}
}
...
@@ -235,9 +207,64 @@ public class FlywayAutoConfiguration {
...
@@ -235,9 +207,64 @@ public class FlywayAutoConfiguration {
@Override
@Override
public
void
setLocations
(
String
...
locations
)
{
public
void
setLocations
(
String
...
locations
)
{
locations
=
FlywayConfiguration
.
resolveLocations
(
locations
,
super
.
setLocations
(
getDataSource
());
new
LocationResolver
(
getDataSource
()).
resolveLocations
(
locations
));
super
.
setLocations
(
locations
);
}
}
private
static
class
LocationResolver
{
private
static
final
String
VENDOR_PLACEHOLDER
=
"{vendor}"
;
private
final
DataSource
dataSource
;
public
LocationResolver
(
DataSource
dataSource
)
{
this
.
dataSource
=
dataSource
;
}
public
String
[]
resolveLocations
(
Collection
<
String
>
locations
)
{
return
resolveLocations
(
locations
.
toArray
(
new
String
[
locations
.
size
()]));
}
public
String
[]
resolveLocations
(
String
[]
locations
)
{
if
(
usesVendorLocation
(
locations
))
{
DatabaseDriver
databaseDriver
=
getDatabaseDriver
();
return
replaceVendorLocations
(
locations
,
databaseDriver
);
}
return
locations
;
}
private
String
[]
replaceVendorLocations
(
String
[]
locations
,
DatabaseDriver
databaseDriver
)
{
if
(
databaseDriver
==
DatabaseDriver
.
UNKNOWN
)
{
return
locations
;
}
String
vendor
=
databaseDriver
.
getId
();
return
Arrays
.
stream
(
locations
)
.
map
((
location
)
->
location
.
replace
(
VENDOR_PLACEHOLDER
,
vendor
))
.
toArray
(
String
[]::
new
);
}
private
DatabaseDriver
getDatabaseDriver
()
{
try
{
String
url
=
(
String
)
JdbcUtils
.
extractDatabaseMetaData
(
this
.
dataSource
,
"getURL"
);
return
DatabaseDriver
.
fromJdbcUrl
(
url
);
}
catch
(
MetaDataAccessException
ex
)
{
throw
new
IllegalStateException
(
ex
);
}
}
private
boolean
usesVendorLocation
(
String
...
locations
)
{
for
(
String
location
:
locations
)
{
if
(
location
.
contains
(
VENDOR_PLACEHOLDER
))
{
return
true
;
}
}
return
false
;
}
}
}
}
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java
View file @
72862b56
...
@@ -258,11 +258,13 @@ public class FlywayAutoConfigurationTests {
...
@@ -258,11 +258,13 @@ public class FlywayAutoConfigurationTests {
@Test
@Test
public
void
useOneLocationWithVendorDirectory
()
{
public
void
useOneLocationWithVendorDirectory
()
{
this
.
contextRunner
.
withUserConfiguration
(
EmbeddedDataSourceConfiguration
.
class
)
this
.
contextRunner
.
withUserConfiguration
(
EmbeddedDataSourceConfiguration
.
class
)
.
withPropertyValues
(
"spring.flyway.locations=classpath:db/vendors/{vendor}"
)
.
withPropertyValues
(
"spring.flyway.locations=classpath:db/vendors/{vendor}"
)
.
run
((
context
)
->
{
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
Flyway
.
class
);
assertThat
(
context
).
hasSingleBean
(
Flyway
.
class
);
Flyway
flyway
=
context
.
getBean
(
Flyway
.
class
);
Flyway
flyway
=
context
.
getBean
(
Flyway
.
class
);
assertThat
(
flyway
.
getLocations
()).
containsExactly
(
"classpath:db/vendors/h2"
);
assertThat
(
flyway
.
getLocations
())
.
containsExactly
(
"classpath:db/vendors/h2"
);
});
});
}
}
...
...
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