1

早上好,

我们正在使用带有 Java API 的 flyway。flyway 版本:6.5.0 企业版。数据库:h2(版本 1.4.197)。

我们正在尝试以编程方式应用撤消操作,设置多个版本并应用撤消,直到当前版本相等。例如从 03 到 02 的 undoVersion。

    private void undoVersion(String lastVersion, Flyway flyway, int limit) {
        MigrationInfoService info = flyway.info();
        String currentVersion = info.current()!=null && info.current().getVersion()!=null ?
            info.current().getVersion().getVersion() : null;

        if (limit > 0 && !lastVersion.equals(currentVersion)) {
            flyway.undo();
            
            undoVersion(lastVersion, flyway, limit - 1);
        }
    }

如果我们以这种方式使用 dryRunOutput 创建 flyway 实例,则不会真正执行撤消操作,因此当前版本永远不会改变。如果我删除 .dryRunOutput(outputFileName) 撤消执行得很好,但我无法得到报告。

       Flyway.configure()
            .dataSource(countryConfig.getString("url"), flywayUser, countryConfig.getString("password"))
            .licenseKey(FLYWAY_LICENSE)
            .schemas(flyWayConfig.getString("schemas"))
            .encoding(flyWayConfig.getString("encoding"))
            .validateOnMigrate(flyWayConfig.getBoolean("validateOnMigrate"))
            .cleanDisabled(flyWayConfig.getBoolean("cleanDisabled"))
            .baselineOnMigrate(flyWayConfig.getBoolean("validateOnMigrate"))
            .table(flyWayConfig.getString("table"))
            .outOfOrder(flyWayConfig.getBoolean("outOfOrder"))
            .placeholderReplacement(true)
            .locations("filesystem:" + countryConfig.getString("flywayLocation"))
            .dryRunOutput(outputFileName)
            .load();

有没有办法使用 dryRun 应用撤消,以便使用 SQL 中应用的撤消查询获取报告?

先感谢您。

最好的问候阿尔瓦罗纳瓦罗

4

3 回答 3

1

试运行应该与undo. 但是,如果您打算一次撤消一个迁移(默认撤消行为),这将不起作用,因为 undo-with-dry-run 不会更新 Flyway 模式历史记录表,因此 Flyway 将始终认为最后实际应用的迁移是要撤消的迁移。

您可以做的是使用target参数来定义要撤消的迁移,并将其与单次试运行结合使用。

编辑:这是我们的问题跟踪器上的一个案例:https ://github.com/flyway/flyway/issues/2890

于 2020-07-24T12:14:49.003 回答
1

非常感谢您对@Julia Hayward 的回复。

在我用于测试的示例中,我有 3 个更新版本文件和 3 个撤消文件。

V01__create_auto_bot.sql
V02__add_bot.sql
V03__update_auto_bot.sql
U01__drop_auto_bot.sql
U02__delete_bot.sql
U03__update_auto_bot.sql

代码很简单

V01__create_auto_bot.sql
CREATE TABLE autobots.auto_bot (ID int not null, NAME varchar(100) not null);

V02__add_bot.sql
INSERT INTO autobots.auto_bot (id, name) VALUES (1, 'Optimus Prime');

V03__update_auto_bot.sql
UPDATE autobots.auto_bot SET name = 'Megatron' WHERE id = 1;

U01__drop_auto_bot.sql
DROP table autobots.auto_bot;

U02__delete_bot.sql
DELETE FROM autobots.auto_bot WHERE id = 1;

U03__update_auto_bot.sql
UPDATE autobots.auto_bot SET name = 'Optimus Prime' WHERE id = 1;

如果我使用 dryRunOutput 执行迁移,它工作正常,我得到这个报告:

-- -====================================
-- Flyway Dry Run (2020-07-24 17:12:39)
-- -====================================

CREATE SCHEMA "autobots";
CREATE TABLE IF NOT EXISTS "autobots"."FLYWAY_schema_history" (
    "installed_rank" INT NOT NULL,
    "version" VARCHAR(50),
    "description" VARCHAR(200) NOT NULL,
    "type" VARCHAR(20) NOT NULL,
    "script" VARCHAR(1000) NOT NULL,
    "checksum" INT,
    "installed_by" VARCHAR(100) NOT NULL,
    "installed_on" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "execution_time" INT NOT NULL,
    "success" BOOLEAN NOT NULL,
    CONSTRAINT "FLYWAY_schema_history_pk" PRIMARY KEY ("installed_rank")
) AS SELECT -1, NULL, '<< Flyway Schema History table created >>', 'TABLE', '', NULL, 'SA', CURRENT_TIMESTAMP, 0, TRUE;
CREATE INDEX "autobots"."FLYWAY_schema_history_s_idx" ON "autobots"."FLYWAY_schema_history" ("success");
INSERT INTO "autobots"."FLYWAY_schema_history" ("installed_rank", "version", "description", "type", "script", "checksum", "installed_by", "execution_time", "success") VALUES (0, null, '<< Flyway Schema Creation >>', 'SCHEMA', '"autobots"', null, 'SA', 0, 1);

-- Executing: migrate (with callbacks)
-- ---------------------------------------------------------------------------------------

-- Executing: migrate -> v01 (with callbacks)
-- ---------------------------------------------------------------------------------------

-- Source: C:\Users\JQ00CT\projects\maggie-flyway\maggie-flyway-infrastructure\target\test-classes\db\autobots\es\V01__create_auto_bot.sql
-- ----------------------------------------------------------------------------------------------------------------------------------------
CREATE TABLE autobots.auto_bot (
    ID int not null,
    NAME varchar(100) not null
);
INSERT INTO "autobots"."FLYWAY_schema_history" ("installed_rank", "version", "description", "type", "script", "checksum", "installed_by", "execution_time", "success") VALUES (1, '01', 'create auto bot', 'SQL', 'V01__create_auto_bot.sql', -1088653058, 'SA', 5, 1);

-- Executing: migrate -> v02 (with callbacks)
-- ---------------------------------------------------------------------------------------

-- Source: C:\Users\JQ00CT\projects\maggie-flyway\maggie-flyway-infrastructure\target\test-classes\db\autobots\es\V02__add_bot.sql
-- --------------------------------------------------------------------------------------------------------------------------------
INSERT INTO autobots.auto_bot (id, name) VALUES (1, 'Optimus Prime');
INSERT INTO "autobots"."FLYWAY_schema_history" ("installed_rank", "version", "description", "type", "script", "checksum", "installed_by", "execution_time", "success") VALUES (2, '02', 'add bot', 'SQL', 'V02__add_bot.sql', 1213011392, 'SA', 1, 1);

-- Executing: migrate -> v03 (with callbacks)
-- ---------------------------------------------------------------------------------------

-- Source: C:\Users\JQ00CT\projects\maggie-flyway\maggie-flyway-infrastructure\target\test-classes\db\autobots\es\V03__update_auto_bot.sql
-- ----------------------------------------------------------------------------------------------------------------------------------------
UPDATE autobots.auto_bot SET name = 'Megatron' WHERE id = 1;
INSERT INTO "autobots"."FLYWAY_schema_history" ("installed_rank", "version", "description", "type", "script", "checksum", "installed_by", "execution_time", "success") VALUES (3, '03', 'update auto bot', 'SQL', 'V03__update_auto_bot.sql', 101152142, 'SA', 1, 1);
SET SCHEMA "PUBLIC";
SET SCHEMA "PUBLIC";

但是,如果我对 UNDO 做同样的事情,正如你提到的,将 dryRunOutput 与 target 参数结合使用,它不起作用:(

我只得到这个...

-- -====================================
-- Flyway Dry Run (2020-07-24 17:15:48)
-- -====================================

SET SCHEMA "autobots";

-- Executing: info (with callbacks)
-- ---------------------------------------------------------------------------------------
SET SCHEMA "PUBLIC";
于 2020-07-24T15:19:33.557 回答
0

感谢您的回答,实际上我们关心的是,即使在 Flyway 实例中undo使用该选项一次undo-with-dry-run,我们也看不到使用 UNDO 操作创建的报告,正如我们在文件中指定的那样U01__drop_table

DROP TABLE AUTO_BOT

相反,我们只看到一个信息总是相同的 repo

-- -====================================
-- Flyway Dry Run (2020-07-23 11:56:02)
-- -====================================

SET SCHEMA "ADMIN_IT";

-- Executing: info (with callbacks)
-- ---------------------------------------------------------------------------------------
SET SCHEMA "PUBLIC";
于 2020-07-24T15:14:20.930 回答