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

如何通过编程方式移除Liquibase中的<validCheckSum>1:any</validCheckSum>标签?

编程方式移除Liquibase的标签

方法一:使用Liquibase Java API

直接借助Liquibase官方API加载并修改changelog文件,兼容性最佳,能准确处理Liquibase的changelog结构:

import liquibase.changelog.ChangeLogParameters;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.parser.ChangeLogParserFactory;
import liquibase.resource.ClassLoaderResourceAccessor;
import liquibase.changelog.ChangeSet;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class ClearValidCheckSums {
    public static void main(String[] args) throws Exception {
        // 替换为你的changelog文件路径
        String changelogFilePath = "src/main/resources/db/changelog.xml";
        
        // 加载changelog
        DatabaseChangeLog changeLog = ChangeLogParserFactory.getInstance()
                .getParser(changelogFilePath, new ClassLoaderResourceAccessor())
                .parse(changelogFilePath, new ChangeLogParameters(), new ClassLoaderResourceAccessor());

        // 遍历所有changeSet,清空validCheckSum列表
        for (ChangeSet changeSet : changeLog.getChangeSets()) {
            changeSet.getValidCheckSums().clear();
        }

        // 将修改后的changelog写回文件
        try (FileWriter writer = new FileWriter(new File(changelogFilePath))) {
            changeLog.write(writer);
        }
    }
}

方法二:用脚本直接修改XML文件

如果不想引入Liquibase依赖,可通过XML解析脚本批量处理changelog文件,以Python为例:

import xml.etree.ElementTree as ET
import os

# 定义要处理的changelog文件目录或单个文件路径
target_path = "src/main/resources/db/changelog"

# Liquibase XML命名空间
ns = {"lb": "http://www.liquibase.org/xml/ns/dbchangelog"}

def remove_valid_checksum(file_path):
    tree = ET.parse(file_path)
    root = tree.getroot()
    
    # 找到所有changeSet下的validCheckSum标签并删除
    for change_set in root.findall("lb:changeSet", ns):
        for vcs in change_set.findall("lb:validCheckSum", ns):
            change_set.remove(vcs)
    
    # 保存修改,保留UTF-8编码和XML声明
    tree.write(file_path, encoding="UTF-8", xml_declaration=True)

# 处理目录下所有XML文件
if os.path.isdir(target_path):
    for filename in os.listdir(target_path):
        if filename.endswith(".xml"):
            remove_valid_checksum(os.path.join(target_path, filename))
else:
    remove_valid_checksum(target_path)

注意事项

  • 操作前务必备份所有changelog文件,防止数据丢失
  • 修改完成后,先在测试环境执行liquibase validate验证文件合法性
  • 运行liquibase update时,Liquibase会自动重新计算每个changeSet的md5sum并更新到数据库的DATABASECHANGELOG表中

内容的提问来源于stack exchange,提问作者Tushar Thakur

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 02:01:12