1212import org .springframework .beans .factory .annotation .Autowired ;
1313import org .springframework .boot .autoconfigure .flyway .FlywayDataSource ;
1414import org .springframework .boot .autoconfigure .flyway .FlywayProperties ;
15+ import org .springframework .boot .autoconfigure .jdbc .DataSourceProperties ;
1516
1617import java .sql .Connection ;
1718import java .util .List ;
19+ import java .util .Optional ;
1820
1921public class FlywayMigrator {
2022 @ Autowired (required = false )
@@ -31,19 +33,24 @@ public class FlywayMigrator {
3133 @ Autowired (required = false )
3234 private List <NakadiProducerFlywayCallback > callbacks ;
3335
34- @ Autowired ( required = false )
36+ @ Autowired
3537 private FlywayProperties flywayProperties ;
3638
39+ @ Autowired
40+ private DataSourceProperties dataSourceProperties ;
41+
3742 @ PostConstruct
3843 public void migrateFlyway () {
3944 Flyway flyway = new Flyway ();
4045
4146 if (this .nakadiProducerFlywayDataSource != null ) {
4247 flyway .setDataSource (nakadiProducerFlywayDataSource );
4348 } else if (this .flywayProperties != null && this .flywayProperties .isCreateDataSource ()) {
44- flyway .setDataSource (this .flywayProperties .getUrl (), this .flywayProperties .getUser (),
45- this .flywayProperties .getPassword (),
46- this .flywayProperties .getInitSqls ().toArray (new String [0 ]));
49+ flyway .setDataSource (
50+ Optional .ofNullable (this .flywayProperties .getUrl ()).orElse (dataSourceProperties .getUrl ()),
51+ Optional .ofNullable (this .flywayProperties .getUser ()).orElse (dataSourceProperties .getUsername ()),
52+ Optional .ofNullable (this .flywayProperties .getPassword ()).orElse (dataSourceProperties .getPassword ()),
53+ this .flywayProperties .getInitSqls ().toArray (new String [0 ]));
4754 } else if (this .flywayDataSource != null ) {
4855 flyway .setDataSource (this .flywayDataSource );
4956 } else {
0 commit comments