运行 Liquibase 按上下文名称更改集

Run Liquibase changesets by context name

有什么方法可以 运行 只有一组带有 Liquibase API 的变更集?

以下代码在开始集成测试之前初始化整个架构 class:

liquibase = new Liquibase(
    LIQUIBASE_CHANGELOG_PATH,
    new FileSystemResourceAccessor(),
    new JdbcConnection(embeddedTestDatabase.getConnection())
);
liquibase.dropAll();
liquibase.update(""); // PROBLEM: for some reason this launch all changesets including changesets with name `test`

现在我想在特定测试方法之前执行类似 DBUnit 的 @DatabaseSetup 的操作 - 意味着仅执行具有 test 上下文的变更集:

liquibase.update("test"); // PROBLEM : this also run all changesets

<changeSet author="me" id="some_id" logicalFilePath="some_path" context="test">
    <sql>
        INSERT INTO COMPANY (ID, CREATED_AT, CREATED_BY, NAME) VALUES (1, '2017-09-15 16:55:57.558', 'My company');
    </sql>
    <rollback>
        DELETE FROM COMPANY;
    </rollback>
</changeSet>

是的,the "contexts" feature in Liquibase 满足您的需求。

文档中给出的示例符合您的要求。

文档解释:

When you run the migrator though any of the available methods, you can pass in a set of contexts to run. Only changeSets marked with the passed contexts will be run.

If you don’t assign a context to a changeSet, it will run all the time, regardless of what contexts you pass in to the migrator.

所以我认为您需要在其余迁移中添加 "test" 以外的上下文,如果您不想在此处 运行 的话。也许是“main”?