在运行迁移之前通过脚本删除 Flyway 历史记录

问题描述 投票:0回答:1

有没有办法定义一个sql脚本,在运行flyway迁移之前删除flyway历史记录(并删除由init脚本创建的所有表)。

我尝试了 data.sql 和 beforeMigration.sql 脚本,但两者都执行得很晚,并且 init 脚本(flyway)不再运行。

spring-boot flyway
1个回答
0
投票

您可以在 springboot 应用程序启动之前删除目标表:

    public static void main(String[] args) {
        String url = "jdbc:postgresql://localhost:5432/yourdatabase";
        String user = "yourusername";
        String password = "yourpassword";

        try {
            String sql = "drop table flyway_schema_history";
            Connection connection = DriverManager.getConnection(url, user, password);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }


        SpringApplication.run(YourApplication.class, args);
    }

如果你想删除数据库中的所有表,你可以看到这个评论 截断 Postgres 数据库中的所有表

但我建议你不要这样做,如果你想修复你的flyway历史记录,你可以使用flyway修复插件。 https://documentation.red-gate.com/flyway/flyway-cli-and-api/commands/repair

© www.soinside.com 2019 - 2024. All rights reserved.