Skip to content

flyway 适配达梦数据库

License

Apache-2.0, Unknown licenses found

Licenses found

Apache-2.0
LICENSE
Unknown
LICENSE.txt
Notifications You must be signed in to change notification settings

zszdevelop/flyway-dm

Repository files navigation

Flyway兼容达梦数据库

1. 背景

公司项目需要兼容国产数据库达梦,达梦和oracle 相似度还是非常高的。绝大部分的sql语句都能共用。但是flyway 本身是不支持达梦的,但是不着急,flyway 本身是开源的,那么我们是否能将达梦适配到 oracle的flyway呢

2. 源码下载导入

2.1 flyway 源码

flyway源码地址

因公司项目之前是用6.3.3 版本,现在版本已经变化挺大了,所以还是使用tag 为6.3.3 的

image-20210925101634465

2.2 flyway 打包测试

我们直接打包是不能直接打包成功的

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project flyway-core: Fatal error compiling

image-20210925102032444

网上的解释是,版本不一致,但按照他们的方法一个个设置,怎么都没用。

后面怀疑是idea 的问题,用mvn clean package 打包做测试

image-20210925102307886

结果发现mvn 的提示清晰多了

无效的标记: --release 

2.2.1 解决办法

  • 第一种: jdk 升级到 JDK9

    不推荐,很多公司还是用jdk8

  • pom.xml 注释掉 <release>8</release>

    image-20210925102554160

这样打包就可以正常使用了

4. 适配达梦

4.1 修改DatabaseType

org.flywaydb.core.internal.jdbc.DatabaseType类中

 if (databaseProductName.startsWith("Oracle"))) {
            return ORACLE;
        }

增加||databaseProductName.startsWith("DM")

 if (databaseProductName.startsWith("Oracle")||databaseProductName.startsWith("DM")) {
            return ORACLE;
        }

4.2 DriverType中添加dmDriverType

public class DriverDataSource implements DataSource {
 public enum DriverType {
	... 
  ORACLE("jdbc:oracle", "oracle.jdbc.OracleDriver"),
	DM("jdbc:dm", "dm.jdbc.driver.DmDriver"),

4.3 不校验版本

我们要兼容达梦,达梦的版本是8.X 而 Oracle 是需要10.x 以上的,所以我们不校验版本

Exception in thread "main" org.flywaydb.core.internal.exception.FlywayDbUpgradeRequiredException: Oracle upgrade required: Oracle 8.1 is outdated and no longer supported by Flyway. Flyway currently supports Oracle 10 and newer.

image-20210921173026851

因为我们达梦直接使用oracle 的语法,但是版本还是达梦的。所以我们直接注释掉。有两处

  • 第一处为:
   protected final void ensureDatabaseIsRecentEnough(String oldestSupportedVersion) {
//        if (!getVersion().isAtLeast(oldestSupportedVersion)) {
//            throw new FlywayDbUpgradeRequiredException(databaseType, computeVersionDisplayName(getVersion()),
//                    computeVersionDisplayName(MigrationVersion.fromVersion(oldestSupportedVersion)));
//        }
    }
  • 第二处为:
    /**
     * Ensures this database it at least at recent as this version otherwise suggest upgrade to this higher edition of
     * Flyway.
     *
     * @param oldestSupportedVersionInThisEdition The oldest supported version of the database by this edition of Flyway.
     * @param editionWhereStillSupported          The edition of Flyway that still supports this version of the database,
     *                                            in case it's too old.
     */
    protected final void ensureDatabaseNotOlderThanOtherwiseRecommendUpgradeToFlywayEdition(String oldestSupportedVersionInThisEdition,
                                                                                            Edition editionWhereStillSupported) {
//        if (!getVersion().isAtLeast(oldestSupportedVersionInThisEdition)) {
//            throw new FlywayEditionUpgradeRequiredException(
//                    editionWhereStillSupported,
//                    databaseType,
//                    computeVersionDisplayName(getVersion()));
//        }
    }

5. 达梦测试

5.1 添加dm依赖

<dependency>
    <groupId>com.dm</groupId>
    <artifactId>DmJdbcDriver</artifactId>
    <version>1.8.0</version>
</dependency>

5.2 添加达梦测试类

public class MainTest {

    public static void main(String[] args) {
        Flyway flyway = Flyway.configure().dataSource("jdbc:dm://192.168.0.1:5236",
                "root", "123456").load();
//        第一步先执行baseline,之后才能执行migrate
        flyway.baseline();
//        flyway.migrate();
        System.out.println("执行完了");

    }
}

4.3 运行测试

image-20210921173515577

也正常生成 flyway_schema_history

参考文章

flyway 源码解析

flyway-4.2适配DM8数据库

问题解决:flyway源码编译,报:flyway-core: Fatal error compiling: 无效的标记: --release

About

flyway 适配达梦数据库

Resources

License

Apache-2.0, Unknown licenses found

Licenses found

Apache-2.0
LICENSE
Unknown
LICENSE.txt

Stars

Watchers

Forks

Packages

No packages published

Languages