Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Setting trackerFileExtension to the instrument name fails with a SQL syntax error, or is silently ignored

未关闭 适合新手
#311 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
80/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust, sql
领域
backend, database

调研方向

从 update_instrument(位于 src/db_service.rs)开始。tracker_file_extension 分支在其值等于仪器名称时会跳过该字段,而 is_empty() 检查在构建 UPDATE 之前运行。按 issue 中第 2 步和第 4 步的做法,将 trackerFileExtension 设为仪器名称后运行 configure 进行复现。完成标准是:该调用会更新该行(如建议修复所述存储 NULL),不会生成空的 SET,并且 insert_new 存储相同的值。

由索引模型根据 Issue 内容生成。

描述

Summary

configure skips a trackerFileExtension that equals the instrument name (src/db_service.rs, update_instrument):

if let Some(ext) = &self.tracker_file_extension {
    if ext != &self.name {
        // extension defaults to instrument name
        fields.push("tracker_file_extension=");
        fields.push_bind_unseparated(ext);
    }
}

This causes two problems:

  1. SQL syntax error. When the extension is the only field being updated, the is_empty() check passes (the extension is Some), but no field is added to the SET list. The query becomes UPDATE instrument SET WHERE name = ? RETURNING *, and the mutation returns
    error returned from database: (code: 1) near "WHERE": syntax error.
  2. The extension can't be reset to the default. Once an extension has been set to something else, setting it back to the instrument name is silently dropped. If another field is updated in the same call, the mutation succeeds and returns the old extension unchanged.

A related inconsistency: creating an instrument with trackerFileExtension equal to its name stores it explicitly. The check only applies to updates. So two instances that behave identically can report null and "b01-1" for the same instrument.

Steps to reproduce (v1.1.0, also in v1.0.1)
git checkout v1.1.0
cargo build
./target/debug/numtracker serve --db repro.db --port 8123

Run each mutation against http://localhost:8123/graphql, for example in graphiql:

1. Create an instrument without an extension. This works and returns trackerFileExtension: null.

mutation {
  configure(instrument: "b01-1", config: {
    directory: "/tmp/{instrument}/data/{year}/{visit}"
    scan: "{subdirectory}/{instrument}-{scan_number}"
    detector: "{subdirectory}/{instrument}-{detector}-{scan_number}"
  }) { instrument trackerFileExtension }
}

2. Set the extension to the instrument name. ❌ This returns the SQL syntax error.

mutation {
  configure(instrument: "b01-1", config: { trackerFileExtension: "b01-1" }) { instrument trackerFileExtension }
}
{"data":null,"errors":[{"message":"error returned from database: (code: 1) near \"WHERE\": syntax error","path":["configure"]}]}

3. Set the extension to something else. This works and returns trackerFileExtension: "other".

mutation {
  configure(instrument: "b01-1", config: { trackerFileExtension: "other" }) { instrument trackerFileExtension }
}

4. Try to set it back to the instrument name, with another field. ❌ This succeeds, but the extension is still "other".

mutation {
  configure(instrument: "b01-1", config: { trackerFileExtension: "b01-1", scanNumber: 5 }) { instrument trackerFileExtension }
}
{"data":{"configure":{"instrument":"b01-1","trackerFileExtension":"other"}}}

5. Create a different instrument with the extension equal to its name. This stores it explicitly (trackerFileExtension: "i22"), which isn't consistent with step 2.

mutation {
  configure(instrument: "i22", config: {
    directory: "/tmp/{instrument}/data/{year}/{visit}"
    scan: "{subdirectory}/{instrument}-{scan_number}"
    detector: "{subdirectory}/{instrument}-{detector}-{scan_number}"
    trackerFileExtension: "i22"
  }) { instrument trackerFileExtension }
}
Expected
  • Setting the extension to the instrument name updates it. In the database, that means setting tracker_file_extension to NULL (the default) or to the name; either works, since NumTracker::for_instrument falls back to the instrument name with ext.unwrap_or(bl).
  • An update that ends up with no fields to set returns the current configuration, as is_empty() already does, instead of running an invalid UPDATE.
Suggested fix

In update_instrument, write NULL when the extension equals the name instead of skipping it:

if let Some(ext) = &self.tracker_file_extension {
    fields.push("tracker_file_extension=");
    // extension defaults to instrument name, so store NULL rather than duplicating it
    fields.push_bind_unseparated((ext != &self.name).then_some(ext));
}

This fixes all three problems: no empty SET, the extension can be reset, and the default is stored the same way every time. insert_new could normalise the same way for consistency.

How this was found

An integration test comparing the configurations query on prod and staging reported b01-1 as null on one and "b01-1" on the other. Trying to align them with the configure mutation hit the syntax error.

主要语言
Rust
星标
1
派生
2
平均合并
14 天 15 小时
30 天内合并 PR
1

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

DiamondLightSource/numtracker 的其他 Issue

查看 DiamondLightSource/numtracker 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。