rust-sqlx-postgres-service

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Rust SQLx Postgres Service

Rust SQLx Postgres 服务

Use this skill to make Rust/Postgres changes schema-first, compile-checked, and testable. Prefer explicit migrations, injected pools, narrow repository functions, and transaction boundaries that match the business operation.
使用本技能可让Rust/Postgres相关变更遵循「先Schema、再编译检查、可测试」的原则。优先选择显式迁移、注入式连接池、职责单一的仓储函数,以及与业务操作匹配的事务边界。

Core Workflow

核心工作流

  1. Inspect
    Cargo.toml
    ,
    migrations/
    ,
    .sqlx/
    ,
    docker-compose.yml
    , CI, and existing database helper modules before changing code.
  2. Design the schema change first. Create or update a migration before writing repository code that depends on it.
  3. Prefer
    sqlx::query!
    and
    query_as!
    for compile-time checked SQL. Use dynamic SQL only when the query shape truly changes at runtime.
  4. Inject
    PgPool
    ,
    PgConnection
    , or
    Transaction
    explicitly. Do not create a pool inside handlers or repository functions.
  5. Keep repository functions small and named by behavior. Return domain types or persistence DTOs instead of leaking raw rows through the app.
  6. Use a transaction for multi-step operations that must commit or roll back together.
  7. Add or update integration tests that run migrations and exercise the public behavior when the change affects API semantics.
  8. Run the bundled preflight script or the repository's equivalent SQLx/Cargo CI commands.
  1. 在修改代码前,检查
    Cargo.toml
    、
    migrations/
    、
    .sqlx/
    、
    docker-compose.yml
    、CI配置以及现有的数据库辅助模块。
  2. 先设计Schema变更。在编写依赖该Schema的仓储代码之前,创建或更新迁移脚本。
  3. 优先使用
    sqlx::query!
    和
    query_as!
    实现编译时检查的SQL。仅当查询结构确实需要在运行时动态变化时,才使用动态SQL。
  4. 显式注入
    PgPool
    、
    PgConnection
    或
    Transaction
    。不要在处理器或仓储函数内部创建连接池。
  5. 保持仓储函数体积小巧,并以业务行为命名。返回领域类型或持久化DTO,避免将原始数据库行暴露到应用上层。
  6. 对于必须一起提交或回滚的多步操作,使用事务。
  7. 当变更影响API语义时,添加或更新集成测试,这些测试会执行迁移并验证公开的业务行为。
  8. 运行捆绑的预检查脚本或仓库对应的SQLx/Cargo CI命令。

Migration Rules

迁移规则

Read
references/migration-safety.md
before changing existing tables or data.
Default migration preferences:
  • Use additive changes first: new nullable columns, new tables, new indexes.
  • Backfill separately from schema creation when a table may be large.
  • Add
    not null
    , uniqueness, and foreign-key constraints only after existing data satisfies them.
  • Name constraints and indexes deliberately when they will appear in errors or operations.
  • Keep down migrations if the project uses them; otherwise document rollback expectations in the migration review.
在修改现有表或数据之前,请阅读
references/migration-safety.md
。
默认迁移偏好:
  • 优先采用增量变更:新增可为空的列、新表、新索引。
  • 当表数据量较大时,将数据回填与Schema创建分开执行。
  • 仅在现有数据满足约束条件后,再添加
    not null
    、唯一性和外键约束。
  • 当约束和索引会出现在错误信息或操作流程中时,需为其指定明确的名称。
  • 如果项目使用回滚迁移,则保留down迁移脚本;否则在迁移审核中记录回滚预期。

Query Rules

查询规则

Read
references/sqlx-patterns.md
for SQLx macro, transaction, and offline mode details.
  • Prefer bind parameters over string interpolation.
  • Use
    fetch_optional
    for lookup-by-key operations.
  • Use
    execute
    for inserts/updates where row data is not needed.
  • Use
    RETURNING
    when the caller needs generated IDs or timestamps.
  • Check affected row counts for updates and deletes that should target exactly one row.
  • Map database uniqueness conflicts to domain errors near the repository edge.
有关SQLx宏、事务和离线模式的详细信息,请阅读
references/sqlx-patterns.md
。
  • 优先使用绑定参数而非字符串拼接。
  • 对于按键查找的操作,使用
    fetch_optional
    。
  • 对于不需要返回行数据的插入/更新操作,使用
    execute
    。
  • 当调用方需要生成的ID或时间戳时,使用
    RETURNING
    子句。
  • 对于应精确影响一行数据的更新和删除操作,检查受影响的行数。
  • 在仓储层附近将数据库唯一性冲突映射为领域错误。

Verification

验证步骤

Run from the Rust project root when the standard SQLx workflow applies:
bash
path/to/rust-sqlx-postgres-service/scripts/sqlx-preflight.sh
The script is conservative: it checks formatting and tests, inspects SQLx migration state when the CLI and
DATABASE_URL
are available, verifies SQLx offline metadata with
cargo sqlx prepare --check
when
.sqlx/
or
DATABASE_URL
is available, and only applies migrations when
RUST_SQLX_PREFLIGHT_RUN_MIGRATIONS=1
is set.
If the repository has a different CI command, use that command and explain why.
当标准SQLx工作流适用时,从Rust项目根目录运行以下命令:
bash
path/to/rust-sqlx-postgres-service/scripts/sqlx-preflight.sh
该脚本较为保守:它会检查代码格式并运行测试;当CLI和
DATABASE_URL
可用时,检查SQLx迁移状态;当
.sqlx/
或
DATABASE_URL
可用时,通过
cargo sqlx prepare --check
验证SQLx离线元数据;仅当设置
RUST_SQLX_PREFLIGHT_RUN_MIGRATIONS=1
时才会执行迁移。
如果仓库使用其他CI命令,请使用该命令并说明原因。

Reference Files

参考文件

  • references/sqlx-patterns.md
    :
    query!
    ,
    query_as!
    , pools, transactions, feature flags, and offline mode.
  • references/migration-safety.md
    : safe migration sequencing and rollout checks.
  • references/sqlx-patterns.md
    :包含
    query!
    、
    query_as!
    、连接池、事务、功能标志和离线模式的相关内容。
  • references/migration-safety.md
    :包含安全迁移顺序和发布检查的相关内容。