You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Liquibase中update(liquibase.Contexts)方法的弃用警告?

Liquibase 弃用update(Contexts)后的CommandScope替代示例

你的代码中liquibase.update(new Contexts())方法已被标记为弃用,官方推荐使用CommandScope执行Liquibase命令。以下是两个实用的替代实现:

示例1:基础Update执行(无额外上下文/标签)

import liquibase.command.CommandScope;
import liquibase.command.DatabaseCommandArgument;
import liquibase.command.ChangelogCommandArgument;
import liquibase.command.CommandArgument;
import liquibase.resource.ClassLoaderResourceAccessor;

// ...

public void deploy(String ldap, String masterChangeLog) throws Exception {
    Database database = getDatabase(ldap);
    
    new CommandScope("update")
            .addArgumentValue(DatabaseCommandArgument.DATABASE_ARG, database)
            .addArgumentValue(ChangelogCommandArgument.CHANGELOG_FILE_ARG, masterChangeLog)
            .addArgumentValue(CommandArgument.RESOURCE_ACCESSOR_ARG, new ClassLoaderResourceAccessor())
            .execute();
}

示例2:带上下文(Contexts)的Update执行

如果需要保留原代码的上下文参数传递逻辑,可以这样实现:

import liquibase.command.CommandScope;
import liquibase.command.DatabaseCommandArgument;
import liquibase.command.ChangelogCommandArgument;
import liquibase.command.CommandArgument;
import liquibase.command.ContextsCommandArgument;
import liquibase.resource.ClassLoaderResourceAccessor;

// ...

public void deployWithContexts(String ldap, String masterChangeLog, String contextStr) throws Exception {
    Database database = getDatabase(ldap);
    
    new CommandScope("update")
            .addArgumentValue(DatabaseCommandArgument.DATABASE_ARG, database)
            .addArgumentValue(ChangelogCommandArgument.CHANGELOG_FILE_ARG, masterChangeLog)
            .addArgumentValue(CommandArgument.RESOURCE_ACCESSOR_ARG, new ClassLoaderResourceAccessor())
            .addArgumentValue(ContextsCommandArgument.CONTEXTS_ARG, contextStr) // 多上下文用逗号分隔,如"dev,uat"
            .execute();
}

注意事项

  • 确保导入示例中对应的Liquibase命令参数类,避免硬编码参数名
  • 若需要添加标签(Labels)过滤,可额外添加LabelsCommandArgument.LABELS_ARG参数,用法与上下文参数一致

内容的提问来源于stack exchange,提问作者Mikhail T.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 15:53:13