16
16
17
17
package org .springframework .boot .autoconfigure .flyway ;
18
18
19
- import java .util .HashSet ;
20
- import java .util .Set ;
21
-
22
19
import javax .annotation .PostConstruct ;
23
20
import javax .persistence .EntityManagerFactory ;
24
21
import javax .sql .DataSource ;
25
22
26
23
import org .flywaydb .core .Flyway ;
27
- import org .springframework .beans .factory .BeanFactory ;
28
- import org .springframework .beans .factory .ListableBeanFactory ;
29
- import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
30
24
import org .springframework .beans .factory .annotation .Autowired ;
31
- import org .springframework .beans .factory .config .BeanDefinition ;
32
- import org .springframework .beans .factory .config .BeanFactoryPostProcessor ;
33
- import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
34
25
import org .springframework .boot .autoconfigure .AutoConfigureAfter ;
35
26
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
36
27
import org .springframework .boot .autoconfigure .condition .ConditionalOnBean ;
37
28
import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
38
29
import org .springframework .boot .autoconfigure .condition .ConditionalOnExpression ;
39
30
import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
31
+ import org .springframework .boot .autoconfigure .data .jpa .EntityManagerFactoryDependsOnPostProcessor ;
40
32
import org .springframework .boot .autoconfigure .jdbc .DataSourceAutoConfiguration ;
41
33
import org .springframework .boot .context .properties .ConfigurationProperties ;
42
34
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
43
35
import org .springframework .context .annotation .Bean ;
44
36
import org .springframework .context .annotation .Configuration ;
45
37
import org .springframework .context .annotation .Import ;
46
38
import org .springframework .core .io .DefaultResourceLoader ;
47
- import org .springframework .core .io .Resource ;
48
39
import org .springframework .core .io .ResourceLoader ;
49
40
import org .springframework .orm .jpa .AbstractEntityManagerFactoryBean ;
50
41
import org .springframework .orm .jpa .LocalContainerEntityManagerFactoryBean ;
51
42
import org .springframework .util .Assert ;
52
- import org .springframework .util .StringUtils ;
53
-
54
- import static java .util .Arrays .asList ;
55
- import static org .springframework .beans .factory .BeanFactoryUtils .beanNamesForTypeIncludingAncestors ;
56
- import static org .springframework .beans .factory .BeanFactoryUtils .transformedBeanName ;
57
43
58
44
/**
59
45
* {@link EnableAutoConfiguration Auto-configuration} for Flyway database migrations.
60
46
*
61
47
* @author Dave Syer
48
+ * @author Phillip Webb
62
49
* @since 1.1.0
63
50
*/
64
51
@ Configuration
@@ -90,20 +77,24 @@ public static class FlywayConfiguration {
90
77
@ PostConstruct
91
78
public void checkLocationExists () {
92
79
if (this .properties .isCheckLocation ()) {
93
-
94
80
Assert .state (!this .properties .getLocations ().isEmpty (),
95
81
"Migration script locations not configured" );
96
- boolean exists = false ;
97
- for (String location : this .properties .getLocations ()) {
98
- Resource resource = this .resourceLoader .getResource (location );
99
- exists = (!exists && resource .exists ());
100
- }
82
+ boolean exists = hasAtLeastOneLocation ();
101
83
Assert .state (exists , "Cannot find migrations location in: "
102
84
+ this .properties .getLocations ()
103
85
+ " (please add migrations or check your Flyway configuration)" );
104
86
}
105
87
}
106
88
89
+ private boolean hasAtLeastOneLocation () {
90
+ for (String location : this .properties .getLocations ()) {
91
+ if (this .resourceLoader .getResource (location ).exists ()) {
92
+ return true ;
93
+ }
94
+ }
95
+ return false ;
96
+ }
97
+
107
98
@ Bean (initMethod = "migrate" )
108
99
@ ConfigurationProperties (prefix = "flyway" )
109
100
public Flyway flyway () {
@@ -124,55 +115,18 @@ else if (this.flywayDataSource != null) {
124
115
125
116
}
126
117
118
+ /**
119
+ * Additional configuration to ensure that {@link EntityManagerFactory} beans
120
+ * depend-on the liquibase bean.
121
+ */
127
122
@ Configuration
128
123
@ ConditionalOnClass (LocalContainerEntityManagerFactoryBean .class )
129
124
@ ConditionalOnBean (AbstractEntityManagerFactoryBean .class )
130
- protected static class FlywayJpaDependencyConfiguration implements
131
- BeanFactoryPostProcessor {
132
-
133
- public static final String FLYWAY_JPA_BEAN_NAME = "flyway" ;
134
-
135
- @ Override
136
- public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory ) {
137
-
138
- for (String beanName : getEntityManagerFactoryBeanNames (beanFactory )) {
139
- BeanDefinition definition = getBeanDefinition (beanName , beanFactory );
140
- definition .setDependsOn (StringUtils .addStringToArray (
141
- definition .getDependsOn (), FLYWAY_JPA_BEAN_NAME ));
142
- }
143
- }
144
-
145
- private static BeanDefinition getBeanDefinition (String beanName ,
146
- ConfigurableListableBeanFactory beanFactory ) {
147
- try {
148
- return beanFactory .getBeanDefinition (beanName );
149
- }
150
- catch (NoSuchBeanDefinitionException e ) {
151
-
152
- BeanFactory parentBeanFactory = beanFactory .getParentBeanFactory ();
153
-
154
- if (parentBeanFactory instanceof ConfigurableListableBeanFactory ) {
155
- return getBeanDefinition (beanName ,
156
- (ConfigurableListableBeanFactory ) parentBeanFactory );
157
- }
158
-
159
- throw e ;
160
- }
161
- }
162
-
163
- private static Iterable <String > getEntityManagerFactoryBeanNames (
164
- ListableBeanFactory beanFactory ) {
165
-
166
- Set <String > names = new HashSet <String >();
167
- names .addAll (asList (beanNamesForTypeIncludingAncestors (beanFactory ,
168
- EntityManagerFactory .class , true , false )));
169
-
170
- for (String factoryBeanName : beanNamesForTypeIncludingAncestors (beanFactory ,
171
- AbstractEntityManagerFactoryBean .class , true , false )) {
172
- names .add (transformedBeanName (factoryBeanName ));
173
- }
125
+ protected static class FlywayJpaDependencyConfiguration extends
126
+ EntityManagerFactoryDependsOnPostProcessor {
174
127
175
- return names ;
128
+ public FlywayJpaDependencyConfiguration () {
129
+ super ("flyway" );
176
130
}
177
131
178
132
}
0 commit comments