可以在 dryRun 模式下应用撤销吗?

huangapple go评论65阅读模式
英文:

Is possible to apply undo with dryRun?

问题

早上好,

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

我们正试图以编程方式应用撤消操作,设置一个版本号,并执行撤消操作,直到当前版本相等为止。例如从版本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撤消查询的报告?

提前谢谢。

最好的祝愿,
Álvaro Navarro

英文:

Good Morining,

We are using flyway with a Java API.
flyway version: 6.5.0 Enterprise.
database: h2 (version 1.4.197).

We are trying to apply undo operation programmatically setting a number of a version and applying undo until the current version would be equal. For example undoVersion from 03 to 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);
        }
    }

If we create the flyway instance using dryRunOutput this way the undo operation is not truly executed and therefore the current version never changes. If I remove .dryRunOutput(outputFileName) the undo is performed fine but I can't get the report.

       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();

Is there a way to apply undo with dryRun in order to get the report with the undo queries applied in SQL ?

Thank you in advance.

Best regards
Álvaro Navarro

答案1

得分: 1

Dry runs ought to work with undo. However, if you intend to undo one migration at a time (the default undo behaviour) that won't work, as the undo-with-dry-run doesn't update the Flyway schema history table and therefore Flyway will always think that the last actually applied migration is the one to be undone.

What you can do is use the target parameter in order to define which migration you want to undo up to, and use that in conjunction with a single dry run.

EDIT: This is now a case on our issue tracker: Link

英文:

Dry runs ought to work with undo. However, if you intend to undo one migration at a time (the default undo behaviour) that won't work, as the undo-with-dry-run doesn't update the Flyway schema history table and therefore Flyway will always think that the last actually applied migration is the one to be undone.

What you can do is use the target parameter in order to define which migration you want to undo up to, and use that in conjunction with a single dry run.

EDIT: This is now a case on our issue tracker: https://github.com/flyway/flyway/issues/2890

答案2

得分: 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参数一起执行,它不起作用 可以在 dryRun 模式下应用撤销吗?

我只会得到这个...

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

SET SCHEMA "autobots";

-- Executing: info (with callbacks)
-- ---------------------------------------------------------------------------------------
SET SCHEMA "PUBLIC";
英文:

Thank you very much for your response @Julia Hayward.

In the example which I'm using for testing I have 3 update version files and 3 undo files.

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

The code is very simple

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, &#39;Optimus Prime&#39;);
V03__update_auto_bot.sql
UPDATE autobots.auto_bot SET name = &#39;Megatron&#39; 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 = &#39;Optimus Prime&#39; WHERE id = 1;

If I perform the migration using the dryRunOutput it works fine and i get this report:

-- -====================================
-- Flyway Dry Run (2020-07-24 17:12:39)
-- -====================================
CREATE SCHEMA &quot;autobots&quot;;
CREATE TABLE IF NOT EXISTS &quot;autobots&quot;.&quot;FLYWAY_schema_history&quot; (
&quot;installed_rank&quot; INT NOT NULL,
&quot;version&quot; VARCHAR(50),
&quot;description&quot; VARCHAR(200) NOT NULL,
&quot;type&quot; VARCHAR(20) NOT NULL,
&quot;script&quot; VARCHAR(1000) NOT NULL,
&quot;checksum&quot; INT,
&quot;installed_by&quot; VARCHAR(100) NOT NULL,
&quot;installed_on&quot; TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
&quot;execution_time&quot; INT NOT NULL,
&quot;success&quot; BOOLEAN NOT NULL,
CONSTRAINT &quot;FLYWAY_schema_history_pk&quot; PRIMARY KEY (&quot;installed_rank&quot;)
) AS SELECT -1, NULL, &#39;&lt;&lt; Flyway Schema History table created &gt;&gt;&#39;, &#39;TABLE&#39;, &#39;&#39;, NULL, &#39;SA&#39;, CURRENT_TIMESTAMP, 0, TRUE;
CREATE INDEX &quot;autobots&quot;.&quot;FLYWAY_schema_history_s_idx&quot; ON &quot;autobots&quot;.&quot;FLYWAY_schema_history&quot; (&quot;success&quot;);
INSERT INTO &quot;autobots&quot;.&quot;FLYWAY_schema_history&quot; (&quot;installed_rank&quot;, &quot;version&quot;, &quot;description&quot;, &quot;type&quot;, &quot;script&quot;, &quot;checksum&quot;, &quot;installed_by&quot;, &quot;execution_time&quot;, &quot;success&quot;) VALUES (0, null, &#39;&lt;&lt; Flyway Schema Creation &gt;&gt;&#39;, &#39;SCHEMA&#39;, &#39;&quot;autobots&quot;&#39;, null, &#39;SA&#39;, 0, 1);
-- Executing: migrate (with callbacks)
-- ---------------------------------------------------------------------------------------
-- Executing: migrate -&gt; 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 &quot;autobots&quot;.&quot;FLYWAY_schema_history&quot; (&quot;installed_rank&quot;, &quot;version&quot;, &quot;description&quot;, &quot;type&quot;, &quot;script&quot;, &quot;checksum&quot;, &quot;installed_by&quot;, &quot;execution_time&quot;, &quot;success&quot;) VALUES (1, &#39;01&#39;, &#39;create auto bot&#39;, &#39;SQL&#39;, &#39;V01__create_auto_bot.sql&#39;, -1088653058, &#39;SA&#39;, 5, 1);
-- Executing: migrate -&gt; 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, &#39;Optimus Prime&#39;);
INSERT INTO &quot;autobots&quot;.&quot;FLYWAY_schema_history&quot; (&quot;installed_rank&quot;, &quot;version&quot;, &quot;description&quot;, &quot;type&quot;, &quot;script&quot;, &quot;checksum&quot;, &quot;installed_by&quot;, &quot;execution_time&quot;, &quot;success&quot;) VALUES (2, &#39;02&#39;, &#39;add bot&#39;, &#39;SQL&#39;, &#39;V02__add_bot.sql&#39;, 1213011392, &#39;SA&#39;, 1, 1);
-- Executing: migrate -&gt; 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 = &#39;Megatron&#39; WHERE id = 1;
INSERT INTO &quot;autobots&quot;.&quot;FLYWAY_schema_history&quot; (&quot;installed_rank&quot;, &quot;version&quot;, &quot;description&quot;, &quot;type&quot;, &quot;script&quot;, &quot;checksum&quot;, &quot;installed_by&quot;, &quot;execution_time&quot;, &quot;success&quot;) VALUES (3, &#39;03&#39;, &#39;update auto bot&#39;, &#39;SQL&#39;, &#39;V03__update_auto_bot.sql&#39;, 101152142, &#39;SA&#39;, 1, 1);
SET SCHEMA &quot;PUBLIC&quot;;
SET SCHEMA &quot;PUBLIC&quot;;

But If I do the same with UNDO, as you mention, using the dryRunOutput in conjuntion with the target parameter, it doesn't work 可以在 dryRun 模式下应用撤销吗?

I only get this ...

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

答案3

得分: 0

感谢您的答复,实际上让我们担心的是,即使在 Flyway 实例中使用 undo-with-dry-run 选项一次性使用 undo,我们也无法看到使用 UNDO 操作创建的报告,就像我们在文件 U01__drop_table 中指定的那样:

DROP TABLE AUTO_BOT

相反,我们只看到一个始终具有相同信息的报告:

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

SET SCHEMA "ADMIN_IT";

-- Executing: info (with callbacks)
-- ---------------------------------------------------------------------------------------
SET SCHEMA "PUBLIC";
英文:

thanks for the answer, actually what is concern us, is that even using undo once single time with the option undo-with-dry-runin the Flyway instance, we cannot see the report created with the UNDO action to do, as we specify in the file U01__drop_table

DROP TABLE AUTO_BOT

Instead we just see a repo with always the same information

-- -====================================
-- Flyway Dry Run (2020-07-23 11:56:02)
-- -====================================
SET SCHEMA &quot;ADMIN_IT&quot;;
-- Executing: info (with callbacks)
-- ---------------------------------------------------------------------------------------
SET SCHEMA &quot;PUBLIC&quot;;

huangapple
  • 本文由 发表于 2020年7月24日 16:02:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63069358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定