首页龙虾技能列表 › Rust DB Contracts — Rust 数据库契约

Rust DB Contracts — Rust 数据库契约

v1.0.0

Rust + SeaORM 数据库显式契约工具。

0· 145·0 当前·0 累计
by @xiamu-ssr (Xiamu-ssr)·MIT-0
下载技能包
License
MIT-0
最后更新
2026/3/20
安全扫描
VirusTotal
无害
查看报告
OpenClaw
安全
high confidence
The skill's files and runtime instructions are consistent with its stated purpose (enforcing SeaORM/Rust entity conventions); it only inspects project source and provides a local CI check script, requests no credentials, and has no install step — but it does include an example that, if executed, would auto-sync schema against a database and should be treated with caution.
评估建议
This skill appears to do what it says: it gives guidance and a local CI-check script to enforce SeaORM entity conventions and doesn't request secrets or install external code. Before you install or let an agent run it: (1) inspect references/check_db_contracts.sh yourself to confirm the grep patterns and paths match your repo layout; (2) do NOT execute the schema-sync example (db.get_schema_registry(...).sync(...)) against production — that example would mutate the database schema if run with re...
详细分析 ▾
用途与能力
The name/description (Rust + SeaORM explicit DB contracts) match the delivered artifacts: guidance (SKILL.md), an entity example, and a local shell script that scans Rust source for the described patterns. There are no unrelated binaries, env vars, or external services requested.
指令范围
The SKILL.md stays on-topic (read entity files first, enforce patterns, preferred SeaORM usage). The included check_db_contracts.sh only scans the repository (defaults to ./src). One notable point: SKILL.md includes an example showing db.get_schema_registry(...).sync(db).await? — this code, if actually executed in a runtime with a real DB_URL, would perform automatic schema changes (CREATE/ALTER/RENAME). The skill does not itself run this, but the presence of that example is an operational risk if an agent or developer executes it against production.
安装机制
No install spec; this is instruction-only with a single helper script. Nothing is downloaded or written to disk by an installer. The provided shell script is a local, static file and uses only standard POSIX tools (grep/sed/xargs).
凭证需求
The skill declares no required env vars or credentials. The only implicit runtime concern is that Rust code snippets or an operator running the schema-sync example would need a DB URL/credentials from the environment — that is a normal development concern but not requested by the skill itself.
持久化与权限
Flags show always: false and user-invocable true. The skill does not request persistent presence or elevated agent-wide privileges.
安全有层次,运行前请审查代码。

License

MIT-0

可自由使用、修改和再分发,无需署名。

运行时依赖

无特殊依赖

版本

latestv1.0.02026/3/20

Initial release of rust-db-contracts: explicit database contract standard for Rust + SeaORM projects. - Defines core conventions for Entity files as the single source of truth for database schemas. - Introduces strict type mapping, mandatory status transition checks, and documentation standards. - Enforces business constant centralization, transaction use for cross-table operations, and soft delete query filtering. - Provides optional best practices and a detection script for CI integration. - Lists prohibited patterns to prevent implicit or unsafe database contracts.

● 无害

安装命令 点击复制

官方npx clawhub@latest install rust-db-contracts
镜像加速npx clawhub@latest install rust-db-contracts --registry https://cn.clawhub-mirror.com

技能文档

一句话:Entity 文件就是数据表的完全表达。
打开一个 entity 文件,不需要连数据库、不需要看 DDL,就能知道这张表的一切。

核心规则

1. 类型映射

数据表特性Rust 写法说明
NOT NULLpub name: String不写 Option = NOT NULL,SeaORM 建表时自动加约束
可为 NULLpub name: Option编译器强制处理 None
枚举字段pub status: MyEnum用 Rust enum + DeriveActiveEnum禁止用 String
JSON 字段pub config: MyStruct用强类型 struct + FromJsonQueryResult禁止用 String 或 serde_json::Value(结构不固定时除外)
时间字段pub created_at: DateTimeUtc禁止用 NaiveDateTime,统一 UTC
默认值#[sea_orm(default_value = "xxx")]注解标注,SeaORM 建表时自动加 DEFAULT
主键#[sea_orm(primary_key)]
唯一约束#[sea_orm(unique)]
索引#[sea_orm(indexed)]

2. 状态枚举必须带流转检查

有状态流转的枚举,必须实现 can_transition_to() 方法,状态变更前必须调用。

impl OrderStatus {
    pub fn can_transition_to(&self, next: &OrderStatus) -> bool {
        matches!((self, next),
            (OrderStatus::Draft, OrderStatus::Active)
            | (OrderStatus::Active, OrderStatus::Archived)
        )
    }
}

3. 业务常量不散落

  • 与枚举关联的常量 → 绑定到 enum 方法(如 member_level.max_borrows()
  • 全局常量 → 集中定义,不在业务代码中出现裸数字/字符串

4. 跨表操作必须用事务

db.transaction::<_, ResultType, ErrType>(|txn| {
    Box::pin(async move {
        // 所有操作共享 txn,出错自动 rollback
        Ok(result)
    })
}).await?;

5. 软删除表查询必须过滤

使用软删除(is_deleted: bool)的表,查询活跃记录时必须带 .filter(Column::IsDeleted.eq(false))。除非明确需要查全量(注释说明原因)。

6. Entity 文件必须有文档

  • 文件级 //! doc 评论:业务规则、状态流转、软删除约定、关联关系等
  • 每个字段 /// 注释:业务含义,不只是字段名的复述

自动建表

使用 SeaORM 2.0 Entity First,entity 就是数据库 schema 的唯一事实源:

let db = Database::connect(db_url).await?;
db.get_schema_registry("my_crate::entity::*").sync(db).await?;
  • 新增 entity → 自动建表
  • 新增字段 → 自动 ALTER 表 添加 列
  • 改列名 → #[sea_orm(renamed_from = "old_name")] 自动 重命名
  • 不需要手写 migration

可选实践(按需采用)

以下不是强制要求,根据项目规模和需要决定:

  • 关系定义Relation enum):需要关联查询(JOIN/预加载)时添加
  • 工厂方法impl 模型 { fn new_xxx() }):有动态默认值(如"可借数量 = 总数量")时添加,让创建逻辑集中在 entity 文件
  • 查询封装impl Entity { 异步 fn find_xxx() }):同一段查询逻辑重复使用时提取

禁止清单

  • ❌ 用 字符串 存枚举值
  • ❌ 用 字符串serde_json::值 存结构化 JSON
  • ❌ 用 NaiveDateTime
  • ❌ 跨表操作不加事务
  • ❌ 状态变更不检查 can_transition_to()
  • ❌ 查询软删除表漏掉过滤(除非注释说明)
  • ❌ entity 文件缺少文件级 doc 评论
  • ❌ 魔法数字/字符串散落在业务代码中

给 AI coding agent 的提示

  • 先读 entity 文件,一个文件 = 一张表的全部信息
  • 直接用 Entity/ActiveModel/列 操作数据,不需要额外的中间类型
  • 新增字段:改 entity struct,程序启动时 schema 同步 自动更新数据库
  • 改状态流转:同时改 enum 和 can_transition_to() 方法

参考 references/ENTITY_EXAMPLE.md 获取完整示例。

检测脚本

references/check_db_contracts.sh 可集成到 CI,自动检查以上规则。

# 用法
./check_db_contracts.sh [src_dir]  # 默认 ./src

# 集成到 CI cargo build && ./check_db_contracts.sh

检测项:

  • 是否引入了非 SeaORM 数据库库(rusqlite/sqlx/diesel 等)
  • 是否有裸 SQL 操作(execute_unprepared / 硬编码 SQL 语句)
  • col_expr 中是否用了字符串字面量(应用 .设置(ActiveModel) 替代)
  • Entity 文件中是否用了 serde_json::值NaiveDateTime
  • 状态变更是否有 can_transition_to() 检查
  • 软删除表查询是否过滤了 is_deleted
  • Entity 文件是否有文件级 doc 评论 和字段注释

错误(❌)会导致脚本返回非零退出码(CI 失败),警告(⚠)不阻断。

数据来源:ClawHub ↗ · 中文优化:龙虾技能库
OpenClaw 技能定制 / 插件定制 / 私有工作流定制

免费技能或插件可能存在安全风险,如需更匹配、更安全的方案,建议联系付费定制

了解定制服务