Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]:add oceanbase oracle mode support #60

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions flyway-database-oceanbase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*-
* ========================LICENSE_START=================================
* flyway-database-oceanbase
* ========================================================================
* Copyright (C) 2010 - 2024 Red Gate Software Ltd
* ========================================================================
* 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.
* =========================LICENSE_END==================================
*/
/*
* Copyright (C) Red Gate Software Ltd 2010-2024
*
Expand All @@ -13,15 +32,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flywaydb.community.database;
package org.flywaydb.community.database.oceanbase;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.flywaydb.core.api.FlywayException;
import org.flywaydb.core.extensibility.PluginMetadata;
import org.flywaydb.core.internal.util.FileUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class OceanBaseDatabaseExtension implements PluginMetadata {
public String getDescription() {
return "Community-contributed OceanBase database support extension " + readVersion() + " by Redgate";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*-
* ========================LICENSE_START=================================
* flyway-database-oceanbase
* ========================================================================
* Copyright (C) 2010 - 2024 Red Gate Software Ltd
* ========================================================================
* 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.
* =========================LICENSE_END==================================
*/
/*
* Copyright (C) Red Gate Software Ltd 2010-2024
*
Expand All @@ -15,27 +34,31 @@
*/
package org.flywaydb.community.database.oceanbase;

import org.flywaydb.community.database.OceanBaseDatabaseExtension;
import org.flywaydb.community.database.oceanbase.mysql.OceanBaseMysqlModeDatabase;
import org.flywaydb.community.database.oceanbase.oracle.OceanBaseOracleModeDatabase;
import org.flywaydb.community.database.oceanbase.oracle.OceanBaseOracleModeParser;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;

import org.flywaydb.core.api.ResourceProvider;
import org.flywaydb.core.api.configuration.Configuration;
import org.flywaydb.core.internal.database.base.BaseDatabaseType;
import org.flywaydb.core.internal.database.base.CommunityDatabaseType;
import org.flywaydb.core.internal.database.base.Database;
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 org.flywaydb.database.mysql.MySQLDatabaseType;
import org.flywaydb.database.mysql.MySQLParser;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;

public class OceanBaseDatabaseType extends MySQLDatabaseType implements CommunityDatabaseType {
public class OceanBaseDatabaseType extends BaseDatabaseType implements CommunityDatabaseType {

private static final String OB_JDBC_DRIVER = "com.oceanbase.jdbc.Driver";
private static final String OB_LEGACY_JDBC_DRIVER = "com.alipay.oceanbase.jdbc.Driver";
private static JdbcConnectionFactory jdbcConnectionFactory;

public String getName() {
return "OceanBase";
Expand All @@ -59,24 +82,19 @@ public boolean handlesJDBCUrl(String url) {

@Override
public String getDriverClass(String url, ClassLoader classLoader) {
return url.startsWith("jdbc:oceanbase:")?
OB_JDBC_DRIVER :
super.getDriverClass(url, classLoader);
return url.startsWith("jdbc:oceanbase:") ? OB_JDBC_DRIVER : OceanBaseJdbcUtils.getDriverClass(url, classLoader);
}

@Override
public String getBackupDriverClass(String url, ClassLoader classLoader) {
return (url.startsWith("jdbc:oceanbase:") && ClassUtils.isPresent(OB_LEGACY_JDBC_DRIVER, classLoader)?
OB_LEGACY_JDBC_DRIVER :
super.getBackupDriverClass(url, classLoader));
return (url.startsWith("jdbc:oceanbase:") && ClassUtils.isPresent(OB_LEGACY_JDBC_DRIVER, classLoader) ? OB_LEGACY_JDBC_DRIVER : OceanBaseJdbcUtils.getBackupDriverClass(url, classLoader));
}

@Override
public boolean handlesDatabaseProductNameAndVersion(String databaseProductName, String databaseProductVersion, Connection connection) {
if (!databaseProductName.contains("MySQL") && !databaseProductName.contains("OceanBase")) {
if (!databaseProductName.contains("MySQL") && !databaseProductName.contains("Oracle") && !databaseProductName.contains("OceanBase")) {
return false;
}

String versionComment;
try {
versionComment = OceanBaseJdbcUtils.getVersionComment(connection);
Expand All @@ -88,7 +106,31 @@ public boolean handlesDatabaseProductNameAndVersion(String databaseProductName,

@Override
public Database createDatabase(Configuration configuration, JdbcConnectionFactory jdbcConnectionFactory, StatementInterceptor statementInterceptor) {
return new OceanBaseDatabase(configuration, jdbcConnectionFactory, statementInterceptor);
OceanBaseDatabaseType.jdbcConnectionFactory = jdbcConnectionFactory;
String compatibleMode = getCompatibleMode(jdbcConnectionFactory);
if (isMySQLMode(compatibleMode)) {
return new OceanBaseMysqlModeDatabase(configuration, jdbcConnectionFactory, statementInterceptor);
} else {
return new OceanBaseOracleModeDatabase(configuration, jdbcConnectionFactory, statementInterceptor);
}
}

@Override
public Parser createParser(Configuration configuration, ResourceProvider resourceProvider, ParsingContext parsingContext) {
String compatibleMode = getCompatibleMode(jdbcConnectionFactory);
if (isMySQLMode(compatibleMode)) {
return new MySQLParser(configuration, parsingContext);
} else {
return new OceanBaseOracleModeParser(configuration, parsingContext);
}
}

private String getCompatibleMode(JdbcConnectionFactory jdbcConnectionFactory) {
return OceanBaseJdbcUtils.getCompatibleMode(jdbcConnectionFactory.openConnection());
}

private boolean isMySQLMode(String compatibleMode) {
return "MySQL".equalsIgnoreCase(compatibleMode);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*-
* ========================LICENSE_START=================================
* flyway-database-oceanbase
* ========================================================================
* Copyright (C) 2010 - 2024 Red Gate Software Ltd
* ========================================================================
* 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.
* =========================LICENSE_END==================================
*/
/*
* Copyright (C) Red Gate Software Ltd 2010-2024
*
Expand All @@ -15,13 +34,14 @@
*/
package org.flywaydb.community.database.oceanbase;

import org.flywaydb.core.internal.util.StringUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.flywaydb.core.internal.util.ClassUtils;
import org.flywaydb.core.internal.util.StringUtils;

public class OceanBaseJdbcUtils {

public static String getVersionComment(Connection connection) throws SQLException {
Expand Down Expand Up @@ -50,4 +70,31 @@ private static String queryVariable(Connection connection, String variable) thro
}
return null;
}

public static String getCompatibleMode(Connection connection) {
String mode;
try (Connection conn = connection; Statement statement = conn.createStatement()) {
ResultSet rs = statement.executeQuery("SHOW VARIABLES LIKE 'ob_compatibility_mode'");
mode = rs.next() ? rs.getString("VALUE") : null;
} catch (SQLException e) {
throw new RuntimeException("Failed to get oceanbase compatible mode", e);
}
return mode;
}

public static String getDriverClass(String url, ClassLoader classLoader) {
if (!url.startsWith("jdbc:p6spy:mysql:") && !url.startsWith("jdbc:p6spy:google:")) {
return url.startsWith("jdbc:mysql:") ? "com.mysql.cj.jdbc.Driver" : "com.mysql.jdbc.GoogleDriver";
} else {
return "com.p6spy.engine.spy.P6SpyDriver";
}
}

public static String getBackupDriverClass(String url, ClassLoader classLoader) {
if (ClassUtils.isPresent("com.mysql.jdbc.Driver", classLoader)) {
return "com.mysql.jdbc.Driver";
} else {
return ClassUtils.isPresent("org.mariadb.jdbc.Driver", classLoader) && !url.contains("disableMariaDbDriver") ? "org.mariadb.jdbc.Driver" : null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*-
* ========================LICENSE_START=================================
* flyway-database-oceanbase
* ========================================================================
* Copyright (C) 2010 - 2024 Red Gate Software Ltd
* ========================================================================
* 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.
* =========================LICENSE_END==================================
*/
/*
* Copyright (C) Red Gate Software Ltd 2010-2024
*
* 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.community.database.oceanbase.mysql;

import java.sql.Connection;

import org.flywaydb.database.mysql.MySQLConnection;
import org.flywaydb.database.mysql.MySQLDatabase;

public class OceanBaseMysqlModeConnection extends MySQLConnection {

public OceanBaseMysqlModeConnection(MySQLDatabase database, Connection connection) {
super(database, connection);
}

@Override
protected boolean canUseNamedLockTemplate() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*-
* ========================LICENSE_START=================================
* flyway-database-oceanbase
* ========================================================================
* Copyright (C) 2010 - 2024 Red Gate Software Ltd
* ========================================================================
* 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.
* =========================LICENSE_END==================================
*/
/*
* Copyright (C) Red Gate Software Ltd 2010-2024
*
Expand All @@ -13,7 +32,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flywaydb.community.database.oceanbase;
package org.flywaydb.community.database.oceanbase.mysql;

import org.flywaydb.community.database.oceanbase.OceanBaseJdbcUtils;

import java.sql.Connection;
import java.sql.SQLException;

import org.flywaydb.core.api.FlywayException;
import org.flywaydb.core.api.MigrationVersion;
Expand All @@ -23,18 +47,15 @@
import org.flywaydb.database.mysql.MySQLConnection;
import org.flywaydb.database.mysql.MySQLDatabase;

import java.sql.Connection;
import java.sql.SQLException;

public class OceanBaseDatabase extends MySQLDatabase {
public class OceanBaseMysqlModeDatabase extends MySQLDatabase {

public OceanBaseDatabase(Configuration configuration, JdbcConnectionFactory jdbcConnectionFactory, StatementInterceptor statementInterceptor) {
public OceanBaseMysqlModeDatabase(Configuration configuration, JdbcConnectionFactory jdbcConnectionFactory, StatementInterceptor statementInterceptor) {
super(configuration, jdbcConnectionFactory, statementInterceptor);
}

@Override
protected MySQLConnection doGetConnection(Connection connection) {
return new OceanBaseConnection(this, connection);
return new OceanBaseMysqlModeConnection(this, connection);
}

@Override
Expand Down
Loading