csv-reconcile

    Deterministic reconciliation and release checks for keyed CSV snapshots.

    csv
    reconciliation
    data-quality
    Download zip
    Author
    Version
    0.3.0
    License
    Apache-2.0
    Last updated
    9 hours ago
    Downloads
    17

    #CSV 主键对账与发布检查

    用业务主键对齐两份 CSV 快照,找出新增、删除和修改的记录。库存迁移、商品发布和定期数据导出可以复用 MoonBit 库,也可以在 CI 中用命令行按规则放行。

    #快速运行

    安装 MoonBit 工具链 和 Node.js 24。首次使用可复制以下命令。已有仓库时,从项目根目录的 moon update 开始。

    git clone https://github.com/WanderHY/csv-reconcile.git cd csv-reconcile moon update moon run cmd/main -- examples/inventory/baseline.csv examples/inventory/candidate.csv --key warehouse --key sku --summary

    预期看到 1 added, 1 removed, 1 changed, 1 unchanged 和 Gate: PASS。默认允许有差异。去掉 --summary 可查看每个主键和字段的前后值。

    只想确认两次导出的业务内容一致? 加上 --check。下面两份文件只重排了行和列,结果为 PASS。新增、删除、修改记录或增删列都会使检查退出 1。

    moon run cmd/main -- examples/ci/baseline.csv examples/ci/reordered.csv --key job --key region --check --summary

    定时导出时,三条记录的时间戳都变了,实际改动了几条业务记录? 以下命令把 exported_at 排除在修改行数上限之外,同时检查售价。

    moon run cmd/main -- examples/export/baseline.csv examples/export/candidate.csv --key sku --max-changed 1 --except-from-limit exported_at --protect price --summary

    Rows: 0 added, 0 removed, 3 changed, 0 unchanged Columns: 0 added, 0 removed Changed rows counted toward limit: 1 of 1 allowed Gate: REJECT protected-column-changed "price": observed 1, limit 0

    退出码为 1。A001 的价格和库存同时改动,按主键计为一条,符合数量上限。价格保护单独拒绝了此次发布。去掉 --summary 即可在 JSON 中核查所有字段的前后值,包括时间戳。全部示例与预期结果

    #输入、结果和退出码

    • 文件使用 UTF-8,第一条 CSV 记录必须为表头。支持可选 BOM、LF 或 CRLF、用双引号包住的逗号或换行,以及用 "" 表示一个引号。引号内的换行统一为 LF。每条记录的字段数必须和表头相同。
    • --key COLUMN 可重复,顺序决定复合键各分量。比较保留大小写、前导零和非空白值周围的空格。缺少主键列、空白键值或重复键属于输入错误。重复键诊断给出候选或基准一侧、当前记录的起始物理行号及重复前的原行号。
    • 同一键仅比较两侧共有的非键列。新增与删除的列单独列出。新增与删除的记录包含对应快照的全部值。修改键值会形成一条删除和一条新增。记录和列按 UTF-16 字典序输出,纯重排行列时报告不变。
    • --max-added N 和 --max-removed N 限制新增和删除的数量,达到上限仍可通过。--same-columns 保持列名集合一致。--protect COLUMN 可重复,用于保护基准中的非键列。候选删除该列或修改匹配记录的相应值,均触发规则。新增和删除的记录由数量规则处理。
    • --check 等同于 --same-columns --max-added 0 --max-removed 0 --max-changed 0。需要允许部分变更时,可直接指定各项限额。和 --check 混用正限额或豁免列会得到配置错误。单独使用 -h 或 --help 可查看参数和可复制的示例。
    • --max-changed N 限制发生字段修改的匹配记录数。同一键的多个字段发生修改只计一条。配合可重复的 --except-from-limit COLUMN 可豁免两侧共有的非键列。豁免只影响这一项限额,字段详情与保护规则仍使用完整差异。拼错、重复或只存在于一侧的豁免列会报配置错误。
    • 退出 0 表示通过当前规则。退出 1 表示报告生成成功但规则拒绝,两种情况都使用标准输出。默认输出 JSON,--summary 输出行数、列数和规则结果,不打印记录值。退出 2 表示参数、输入或规则配置错误,两种模式均在标准错误输出 JSON。

    程序将两份快照读入内存。输出包含原始字段值,应按源数据的权限管理报告。

    #MoonBit 库用法

    可用 moon add WanderHY/csv-reconcile 安装 Mooncakes 上的最新发行版。在你的包的 moon.pkg 中导入。

    import {
    "WanderHY/csv-reconcile" @reconcile,
    }

    ///|
    test "protect quantity during a migration" {
    let baseline = "warehouse,sku,quantity\nNorth,A001,10\n"
    let candidate = "warehouse,sku,quantity\nNorth,A001,11\n"
    guard! @reconcile.reconcile(baseline, candidate, ["warehouse", "sku"]) is Ok(report)
    let gate = @reconcile.Gate::new(protected_columns=["quantity"])
    guard! gate.evaluate(report) is Ok(decision)
    assert_false(decision.passed)
    }

    reconcile 接收已解码的两个字符串和键列数组,返回 Result[Report, Issue]。Gate::evaluate 返回 Result[Decision, Issue],其中 Decision 可区分通过与规则拒绝。自 0.2.0 起,可通过 Gate::new(max_changed=1, except_from_limit=["exported_at"]) 设置业务修改量上限,或用 report.count_changed_except(["exported_at"]) 读取该口径的行数。

    #开发与自检

    Python 3.10 或更新版本用于独立测试,两份脚本仅使用标准库。

    moon check --deny-warn moon fmt --check moon build --release --deny-warn moon test --deny-warn python scripts/check_cli.py python scripts/check_package.py

    check_cli.py 检查四个业务样例、无效输入及 24 组独立 CSV 对照案例,每组再重排行列验证确定性。check_package.py 将本地打包产物解压到临时目录,由独立 MoonBit 模块调用公开 API。公开发布后可加 --registry 从 Mooncakes 安装同一版本进行复验。

    项目使用 Apache-2.0。CSV 解析复用 Wayfarer 0.1.0,文件读取复用 moonbitlang/x 0.5.6。对账、发布规则、示例和验证由本期实现。 立项范围与来源

    Cell

    pub struct Cell {
    column : String
    value : String
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    One named cell from an added or removed record.

    CellChange

    pub struct CellChange {
    column : String
    before : String
    after : String
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    Old and new values for a common non-key column on a matched record.

    Decision

    pub struct Decision {
    passed : Bool
    violations : Array[Violation]
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    Gate

    pub struct Gate {
    max_added : Int?
    max_removed : Int?
    max_changed : Int?
    except_from_limit : Array[String]
    require_same_columns : Bool
    protected_columns : Array[String]
    }

    Release constraints applied to one reconciliation report.

    Gate::evaluate

    fn Gate::evaluate(self : Gate, report : Report) -> Result[Decision, Issue]

    Evaluate the release gate. Configuration errors are distinct from failures.

    Gate::new

    fn Gate::new(max_added? : Int, max_removed? : Int, max_changed? : Int, except_from_limit? : Array[String], require_same_columns? : Bool, protected_columns? : Array[String]) -> Gate

    Issue

    pub(all) struct Issue {
    code : String
    source : String
    line : Int?
    message : String
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    A validation failure with an optional one-based CSV record start line.

    Record

    pub struct Record {
    key : Array[String]
    cells : Array[Cell]
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    A complete record and its key in the configured column order.

    RecordChange

    pub struct RecordChange {
    key : Array[String]
    cells : Array[CellChange]
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    Report

    pub struct Report {
    key_columns : Array[String]
    baseline_columns : Array[String]
    candidate_columns : Array[String]
    added_columns : Array[String]
    removed_columns : Array[String]
    added_rows : Array[Record]
    removed_rows : Array[Record]
    changed_rows : Array[RecordChange]
    unchanged_rows : Int
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    Reconciliation result. Ordering is independent of input layout.

    Report::count_changed_except

    fn Report::count_changed_except(self : Report, columns : Array[String]) -> Int

    Count matched records with a change outside the named columns.

    Violation

    pub struct Violation {
    code : String
    column : String?
    observed : Int
    limit : Int
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    reconcile

    fn reconcile(baseline : String, candidate : String, keys : Array[String]) -> Result[Report, Issue]

    Compare headered CSV snapshots by a caller-supplied ordered key.