Flyway 5.0.7 关于使用 schema_version table 的警告

Flyway 5.0.7 warning about using schema_version table

我们使用 Flyway Gradle 插件进行离线迁移(即我们在系统关闭时迁移)。我们最近升级到 Flyway 5.0.7,我们现在看到这个迁移警告:

无法找到架构历史记录 table XXXXXXX.flyway_schema_history,但找到了 XXXXXXX.schema_version。您看到此消息是因为 Flyway 在 5.0.0 版中将 flyway.table 的默认值更改为 flyway_schema_history,而您仍然依赖旧的默认值 (schema_version)。在您的配置中设置 flyway.table=schema_version 来解决这个问题。 Flyway 6.0.0 将移除此回退机制。

(我使用 XXXXXXX 来掩盖实际的架构名称)。

所以,看来我们可以通过设置flyway.table=schema_version来避免这个错误。但是,它也说这个机制将在 Flyway 6.0.0 中被删除。

我们是否应该做些什么来使这种兼容性向前发展?我们必须手动将 schema_version table 重命名为 flyway_schema_history 吗?或者有没有办法让 Flyway 做到这一点?如果不是,当 Flyway 6.0.0 出来时会发生什么?它会自动将数据迁移到适当的 table 名称吗?

flyway.table 的默认值已从 schema_version 更改为 flyway_schema_history。他们还提供了自动回退到旧默认设置的警告,以避免使用旧默认设置破坏现有安装。

表示从flyway 5开始,如果你的配置文件中没有指定flyway.table 属性,那么flyway会在db中寻找table flyway_schema_history,如果没有找到,它会寻找 table schema_version 作为后备,如果找到旧的 table 然后会警告你现在收到的消息。从 flyway 6 开始,这种回退机制将被移除。如果你没有提供 flyway.table 属性,它会在数据库中寻找 flyway_schema_history,如果没有找到它不会寻找 schema_version table 即使你有any 并将创建一个名为 flyway_schema_history 的新 table 以保持功能。

在 flyway 6 中,如果您设置 flyway.table=schema_version,您现有的系统将 运行 正常,您不需要更改数据库中的 table 名称。但是如果你没有设置属性,那么你必须要更改table名称,否则flyway将无法识别现有的schema_version table,将系统视为一个新的,将创建 flyway_schema_history table 并将从头开始执行脚本。

希望对您有所帮助。

通过将 table 映射到另一个并复制相关记录,可以从 schema_version 迁移到 flyway_schema_history:

DROP TABLE IF EXISTS `flyway_schema_history`;
SET character_set_client = utf8mb4 ;
CREATE TABLE `flyway_schema_history` (
    `installed_rank` int(11) NOT NULL,
    `version` varchar(50) DEFAULT NULL,
    `description` varchar(200) NOT NULL,
    `type` varchar(20) NOT NULL,
    `script` varchar(1000) NOT NULL,
    `checksum` int(11) DEFAULT NULL,
    `installed_by` varchar(100) NOT NULL,
    `installed_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `execution_time` int(11) NOT NULL,
    `success` tinyint(1) NOT NULL,
    PRIMARY KEY (`installed_rank`),
    KEY `flyway_schema_history_s_idx` (`success`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

insert into flyway_schema_history (installed_rank, version, description, type, script, checksum, installed_by, installed_on, execution_time, success)
select installed_rank, version, description, type, script, checksum, installed_by, installed_on, execution_time, success
from schema_version;

这是从 flyway 5.2.2 开始的 flyway_schema_history 的架构版本。我建议,为了安全地使用这个脚本,先迁移到这个版本,然后再继续。

请理解,此脚本必须按照您的数据库控制台中的原样执行。此脚本仅适用于 MySQL。您必须为其他数据库制作自己的数据库。

在 PostgreSQL 上,我只用一次迁移就解决了这个问题:

DO $$
  BEGIN
    IF (EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'schema_version')
        AND EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'flyway_schema_history'))
    THEN
        DROP TABLE schema_version;
    END IF ;
    IF (EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'schema_version')
        AND NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'flyway_schema_history'))
    THEN
        CREATE TABLE flyway_schema_history AS TABLE schema_version;
    END IF ;
  END
$$ ;

它实际上分两个阶段工作:

  • 第一次重新启动时,它会复制历史记录并迁移到 'old' 历史记录 table。
  • 第二次重新启动时,它会删除旧历史记录 table。现在迁移进入 'new' 历史,一切都完成了。

我不认为这是真的:“这意味着从 flyway 5,如果你没有在你的配置文件中指定 flyway.table 属性,那么 flyway 将寻找 table flyway_schema_history 在 db 中,如果未找到,它将寻找 table schema_version 作为后备,如果找到旧的 table,则将警告您正在收到的消息现在。”

因为我有两个 table,但它仍然抱怨:“找不到模式历史记录 table“xxxx”。“flyway_schema_history”,但找到了“xxxx” ."schema_version" 而不是。您看到此消息是因为 Flyway 在 5.0.0 版中将 flyway.table 的默认值更改为 flyway_schema_history 而您仍然依赖旧的默认值(schema_version)."

也就是说,flyway的逻辑是反的,肯定是先查了“schema_version”,发现了就假设没有“flyway_schema_history”