【问题标题】:Is possible to apply undo with dryRun?可以使用 dryRun 应用撤消吗?
【发布时间】:2020-11-14 01:42:14
【问题描述】:

早安,

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

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

    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 中应用的撤消查询获得报告?

提前谢谢你。

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

【问题讨论】:

    标签: java flyway undo dry-run


    【解决方案1】:

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

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

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

    【讨论】:

    • 嗨,朱莉娅,你能看到我粘贴的第二篇文章,以解释我们的问题。谢谢
    【解决方案2】:

    感谢您的回答,实际上我们关心的是,即使在 Flyway 实例中使用 undo-with-dry-run 选项单次使用 undo,我们也看不到使用 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";
    

    【讨论】:

      【解决方案3】:

      非常感谢您@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 与目标参数结合使用,它不起作用:(

      我只知道这个...

      -- -====================================
      -- Flyway Dry Run (2020-07-24 17:15:48)
      -- -====================================
      
      SET SCHEMA "autobots";
      
      -- Executing: info (with callbacks)
      -- ---------------------------------------------------------------------------------------
      SET SCHEMA "PUBLIC";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-22
        • 2021-12-31
        • 2018-08-12
        • 2013-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-10
        相关资源
        最近更新 更多