如何构建基于 flyway java 的迁移

How to build flyway java-based migrations

我正在使用 flyway 命令行工具来处理我的数据库迁移。 到目前为止,所有迁移都是 sql

配置文件(仅使用选项):

flyway.url=jdbc:postgresql://db.host
flyway.user=user
flyway.password=password

flyway.table=flyway_migrations

flyway.locations=filesystem:/home/........./sql/migrations

flyway.sqlMigrationPrefix=update
flyway.validateOnMigrate=false
flyway.outOfOrder=true

效果很好。

但现在我需要添加一个基于 java 的迁移。我真的很困惑 我找不到任何如何做的例子。如何编译,在哪里放置 java 迁移。

我试过简单的迁移-class来自官方文档:

package db.migration;

import org.flywaydb.core.api.migration.jdbc.JdbcMigration;
import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * Example of a Java-based migration.
 */
public class V50_121_1__update_notes implements JdbcMigration {
    public void migrate(Connection connection) throws Exception {
        PreparedStatement statement =
            connection.prepareStatement("INSERT INTO test_user (name) VALUES ('Obelix')");

        try {
            statement.execute();
        } finally {
            statement.close();
        }
    }
}

但是接下来要做什么? 尝试编译:

javac -cp "./flyway-core-3.2.1.jar" V50_121_1__update_notes.java
jar cf V50_121_1__update_dataNode_notes.jar V50_121_1__update_dataNode_notes.class

然后把那个jar放到不同的地方,都没有效果。

飞路信息 - 看不到迁移。

那么如何构建最简单的基于java的迁移。我宁愿不使用 Maven 或类似的东西。只是简单的 jar 文件(???),由 flyway 命令行工具获取。

谢谢。

确保您的 .class 文件位于 .jar 文件内的 db/migration 目录中,并且您的 .jar 文件位于 /jars Flyway 安装目录。

flyway.locations 也应设置为:

db.migration,filesystem:/home/........./sql/migrations

.java 文件指定为 .java como

类路径: package_example.migrations

flyway.url=jdbc:postgresql://db.host
flyway.user=user
flyway.password=password

flyway.table=flyway_migrations

flyway.locations=filesystem:/home/...../sql/migrations,classpath:pack_example.migrations

flyway.sqlMigrationPrefix=update
flyway.validateOnMigrate=false
flyway.outOfOrder=true

查看 link 文档 https://flywaydb.org/documentation/commandline/migrate