跳到主要内容
知仓学习社ZHICANG

rust-sqlx-postgres-service

Use when adding, changing, testing, or reviewing Postgres persistence in Rust services that use sqlx, especially when designing migrations, query! o…

不碰外部(只输出文字)无严重或高危命中hashgraph-online/awesome-codex-plugins

它会碰到什么

扫了多少5 个文本文件,8 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0

这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。

技能内容

Rust SQLx Postgres Service

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.

Core Workflow

  1. Inspect Cargo.toml, migrations/, .sqlx/, docker-compose.yml, CI, and

existing database helper modules before changing code.

  1. Design the schema change first. Create or update a migration before writing

repository code that depends on it.

  1. Prefer sqlx::query! and query_as! for compile-time checked SQL. Use

dynamic SQL only when the query shape truly changes at runtime.

  1. Inject PgPool, PgConnection, or Transaction explicitly. Do not create a

pool inside handlers or repository functions.

  1. Keep repository functions small and named by behavior. Return domain types

or persistence DTOs instead of leaking raw rows through the app.

  1. Use a transaction for multi-step operations that must commit or roll back

together.

  1. Add or update integration tests that run migrations and exercise the public

behavior when the change affects API semantics.

  1. Run the bundled preflight script or the repository's equivalent SQLx/Cargo

CI commands.

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.

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.

Verification

Run from the Rust project root when the standard SQLx workflow applies:

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.

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.

想直接用这个技能?

本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。