-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
MySQLDatabaseType.java
139 lines (90 loc) · 3.96 KB
/
MySQLDatabaseType.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Copyright © Red Gate Software Ltd 2010-2020
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flywaydb.core.internal.database.mysql;
import org.flywaydb.core.api.ResourceProvider;
import org.flywaydb.core.api.configuration.Configuration;
import org.flywaydb.core.internal.database.base.Database;
import org.flywaydb.core.internal.database.base.DatabaseType;
import org.flywaydb.core.internal.jdbc.JdbcConnectionFactory;
import org.flywaydb.core.internal.jdbc.StatementInterceptor;
import org.flywaydb.core.internal.parser.Parser;
import org.flywaydb.core.internal.parser.ParsingContext;
import org.flywaydb.core.internal.util.ClassUtils;
import java.sql.Connection;
import java.sql.Types;
import java.util.Properties;
public class MySQLDatabaseType extends DatabaseType {
private static final String MYSQL_LEGACY_JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String MARIADB_JDBC_DRIVER = "org.mariadb.jdbc.Driver";
@Override
public String getName() {
return "MySQL";
}
@Override
public int getNullType() {
return Types.VARCHAR;
}
@Override
public boolean handlesJDBCUrl(String url) {
if (url.startsWith("jdbc-secretsmanager:mysql:")) {
throw new org.flywaydb.core.internal.license.FlywayTeamsUpgradeRequiredException("jdbc-secretsmanager");
}
return url.startsWith("jdbc:mysql:") || url.startsWith("jdbc:google:");
}
@Override
public String getDriverClass(String url, ClassLoader classLoader) {
if (url.startsWith("jdbc:mysql:")) {
return "com.mysql.cj.jdbc.Driver";
} else {
return "com.mysql.jdbc.GoogleDriver";
}
}
@Override
public String getBackupDriverClass(String url, ClassLoader classLoader) {
if (ClassUtils.isPresent(MYSQL_LEGACY_JDBC_DRIVER, classLoader)) {
return MYSQL_LEGACY_JDBC_DRIVER;
}
if (ClassUtils.isPresent(MARIADB_JDBC_DRIVER, classLoader)) {
LOG.warn("You are attempting to connect to a MySQL database using the MariaDB driver." +
" This is known to cause issues." +
" An upgrade to Oracle's MySQL JDBC driver is highly recommended.");
return MARIADB_JDBC_DRIVER;
}
return null;
}
@Override
public boolean handlesDatabaseProductNameAndVersion(String databaseProductName, String databaseProductVersion, Connection connection) {
// Google Cloud SQL returns different names depending on the environment and the SDK version.
// ex.: Google SQL Service/MySQL
return databaseProductName.contains("MySQL");
}
@Override
public Database createDatabase(Configuration configuration, JdbcConnectionFactory jdbcConnectionFactory, StatementInterceptor statementInterceptor) {
return new MySQLDatabase(configuration, jdbcConnectionFactory, statementInterceptor);
}
@Override
public Parser createParser(Configuration configuration, ResourceProvider resourceProvider, ParsingContext parsingContext) {
return new MySQLParser(configuration, parsingContext);
}
@Override
public void setDefaultConnectionProps(String url, Properties props, ClassLoader classLoader) {
props.put("connectionAttributes", "program_name:" + APPLICATION_NAME);
}
@Override
public boolean detectPasswordRequiredByUrl(String url) {
return super.detectPasswordRequiredByUrl(url);
}
}