15
15
*/
16
16
package org .springframework .cloud .dataflow .server .db .migration ;
17
17
18
+ import java .lang .reflect .Method ;
19
+ import java .sql .DatabaseMetaData ;
20
+
18
21
import javax .sql .DataSource ;
19
22
20
23
import org .flywaydb .core .api .configuration .FluentConfiguration ;
24
+ import org .slf4j .Logger ;
25
+ import org .slf4j .LoggerFactory ;
21
26
22
27
import org .springframework .boot .autoconfigure .flyway .FlywayConfigurationCustomizer ;
23
28
import org .springframework .boot .jdbc .DatabaseDriver ;
29
34
import org .springframework .cloud .dataflow .server .db .migration .sqlserver .MsSqlBeforeBaseline ;
30
35
import org .springframework .jdbc .support .JdbcUtils ;
31
36
import org .springframework .jdbc .support .MetaDataAccessException ;
37
+ import org .springframework .util .ReflectionUtils ;
32
38
33
39
/**
34
40
* Flyway {@link FlywayConfigurationCustomizer} bean customizing callbacks per
39
45
*/
40
46
public class DataFlowFlywayConfigurationCustomizer implements FlywayConfigurationCustomizer {
41
47
48
+ private static final Logger logger = LoggerFactory .getLogger (DataFlowFlywayConfigurationCustomizer .class );
49
+
42
50
@ Override
43
51
public void customize (FluentConfiguration configuration ) {
44
52
// boot's flyway auto-config doesn't allow to define callbacks per
@@ -49,7 +57,13 @@ public void customize(FluentConfiguration configuration) {
49
57
configuration .callbacks (new PostgresBeforeBaseline ());
50
58
}
51
59
else if (databaseDriver == DatabaseDriver .MARIADB ) {
52
- configuration .callbacks (new MariadbBeforeBaseline ());
60
+ // if the driver is being used for MySQL server then use MySQL baseline
61
+ if (vendorIsMySql (dataSource )) {
62
+ configuration .callbacks (new MysqlBeforeBaseline ());
63
+ }
64
+ else {
65
+ configuration .callbacks (new MariadbBeforeBaseline ());
66
+ }
53
67
}
54
68
else if (databaseDriver == DatabaseDriver .MYSQL ) {
55
69
configuration .callbacks (new MysqlBeforeBaseline ());
@@ -66,13 +80,25 @@ else if (databaseDriver == DatabaseDriver.DB2) {
66
80
}
67
81
68
82
private DatabaseDriver getDatabaseDriver (DataSource dataSource ) {
69
- // copied from boot's flyway auto-config to get matching db vendor id
70
83
try {
71
- String url = JdbcUtils .extractDatabaseMetaData (dataSource , " getURL" );
84
+ String url = JdbcUtils .extractDatabaseMetaData (dataSource , DatabaseMetaData :: getURL );
72
85
return DatabaseDriver .fromJdbcUrl (url );
73
86
}
74
87
catch (MetaDataAccessException ex ) {
75
88
throw new IllegalStateException (ex );
76
89
}
90
+
91
+ }
92
+
93
+ private boolean vendorIsMySql (DataSource dataSource ) {
94
+ try {
95
+ Method method = ReflectionUtils .findMethod (dataSource .getClass (), "getJdbcUrl" );
96
+ String jdbcUrl = (String ) ReflectionUtils .invokeMethod (method , dataSource );
97
+ return jdbcUrl != null && jdbcUrl .startsWith ("jdbc:mysql:" );
98
+ }
99
+ catch (Exception ex ) {
100
+ logger .error ("Unable to get jdbcUrl on datasource and hence unable to determine if DB is MySQL: " + ex .getMessage (), ex );
101
+ }
102
+ return false ;
77
103
}
78
104
}
0 commit comments