Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit 0aa0cff

Browse files
committed
Use MySQL when using Maria driver and MySQL scheme
1 parent 1055620 commit 0aa0cff

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

spring-cloud-dataflow-server-core/src/main/java/org/springframework/cloud/dataflow/server/db/migration/DataFlowFlywayConfigurationCustomizer.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
*/
1616
package org.springframework.cloud.dataflow.server.db.migration;
1717

18+
import java.lang.reflect.Method;
19+
import java.sql.DatabaseMetaData;
20+
1821
import javax.sql.DataSource;
1922

2023
import org.flywaydb.core.api.configuration.FluentConfiguration;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
2126

2227
import org.springframework.boot.autoconfigure.flyway.FlywayConfigurationCustomizer;
2328
import org.springframework.boot.jdbc.DatabaseDriver;
@@ -29,6 +34,7 @@
2934
import org.springframework.cloud.dataflow.server.db.migration.sqlserver.MsSqlBeforeBaseline;
3035
import org.springframework.jdbc.support.JdbcUtils;
3136
import org.springframework.jdbc.support.MetaDataAccessException;
37+
import org.springframework.util.ReflectionUtils;
3238

3339
/**
3440
* Flyway {@link FlywayConfigurationCustomizer} bean customizing callbacks per
@@ -39,6 +45,8 @@
3945
*/
4046
public class DataFlowFlywayConfigurationCustomizer implements FlywayConfigurationCustomizer {
4147

48+
private static final Logger logger = LoggerFactory.getLogger(DataFlowFlywayConfigurationCustomizer.class);
49+
4250
@Override
4351
public void customize(FluentConfiguration configuration) {
4452
// boot's flyway auto-config doesn't allow to define callbacks per
@@ -49,7 +57,13 @@ public void customize(FluentConfiguration configuration) {
4957
configuration.callbacks(new PostgresBeforeBaseline());
5058
}
5159
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+
}
5367
}
5468
else if (databaseDriver == DatabaseDriver.MYSQL) {
5569
configuration.callbacks(new MysqlBeforeBaseline());
@@ -66,13 +80,25 @@ else if (databaseDriver == DatabaseDriver.DB2) {
6680
}
6781

6882
private DatabaseDriver getDatabaseDriver(DataSource dataSource) {
69-
// copied from boot's flyway auto-config to get matching db vendor id
7083
try {
71-
String url = JdbcUtils.extractDatabaseMetaData(dataSource, "getURL");
84+
String url = JdbcUtils.extractDatabaseMetaData(dataSource, DatabaseMetaData::getURL);
7285
return DatabaseDriver.fromJdbcUrl(url);
7386
}
7487
catch (MetaDataAccessException ex) {
7588
throw new IllegalStateException(ex);
7689
}
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;
77103
}
78104
}

0 commit comments

Comments
 (0)