rust-sqlx-postgres-service
Use when adding, changing, testing, or reviewing Postgres persistence in Rust services that use sqlx, especially when designing migrations, query! or query_as! calls, PgPool injection, transactions, compile-time checked SQL, SQLx offline mode, repository boundaries, or database-backed integration tests.
- License 许可
- MIT
- 最近更新
- 2026-06-26
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- rust-sqlx-postgres-service
- license
- MIT
- metadata
- { "tags": "rust,sqlx,postgres,migrations,persistence", "version": "0.1.0", "category": "Rust", "displayName": "Rust SQLx Postgres Service" }
- description
- Use when adding, changing, testing, or reviewing Postgres persistence in Rust services that use sqlx, especially when designing migrations, query! or query_as! calls, PgPool injection, transactions, compile-time checked SQL, SQLx offline mode, repository boundaries, or database-backed integration tests.
- compatibility
- Codex, Claude Code, and other Agent Skills-compatible clients.
02 资源文件 4
- 参考文档agents/openai.yaml202 B
- 参考文档references/migration-safety.md1.1 KB
- 参考文档references/sqlx-patterns.md2.2 KB
- 脚本scripts/sqlx-preflight.sh1.1 KB
03 SKILL.md 正文
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
- Inspect
Cargo.toml,migrations/,.sqlx/,docker-compose.yml, CI, and existing database helper modules before changing code. - Design the schema change first. Create or update a migration before writing repository code that depends on it.
- Prefer
sqlx::query!andquery_as!for compile-time checked SQL. Use dynamic SQL only when the query shape truly changes at runtime. - Inject
PgPool,PgConnection, orTransactionexplicitly. Do not create a pool inside handlers or repository functions. - Keep repository functions small and named by behavior. Return domain types or persistence DTOs instead of leaking raw rows through the app.
- Use a transaction for multi-step operations that must commit or roll back together.
- Add or update integration tests that run migrations and exercise the public behavior when the change affects API semantics.
- 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_optionalfor lookup-by-key operations. - Use
executefor inserts/updates where row data is not needed. - Use
RETURNINGwhen 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.
04 同仓库更多 Skills
android-emulator-qa
Use when validating Android feature flows in an emulator with adb-driven launch, input, UI-tree inspection, screenshots, and logcat capture.
supabase-postgres-best-practices
Postgres performance optimization and best practices from Supabase. Use this skill when writing, reviewing, or optimizing Postgres queries, schema designs, or database configurations.
sentry
Use when the user asks to inspect Sentry issues or events, summarize recent production errors, or pull basic Sentry health data via the Sentry API; perform read-only queries with the bundled script and require `SENTRY_AUTH_TOKEN`.
render-workflows
Sets up, develops, tests, and deploys Render Workflows. Covers first-time scaffolding (via CLI or manual), SDK installation (Python or TypeScript), task patterns (retries, subtasks, fan-out), local development, Dashboard deployment, and troubleshooting. Use when a user wants to set up Render Workflows for the first time, scaffold a workflow service, add or modify workflow tasks, test workflows locally, or deploy workflows to Render.
render-web-services
Configures Render web services—port binding, TLS, health checks, custom domains, auto-deploy, PR previews, persistent disks, and deploy lifecycle. Use when the user needs to set up a web service, fix health check failures, add a custom domain, configure zero-downtime deploys, or troubleshoot port binding issues.
render-static-sites
Deploys and configures static sites on Render's global CDN—build commands, publish paths, SPA routing, redirects, custom headers, and PR previews. Use when the user needs to deploy a static site, set up a React/Vue/Hugo/Gatsby frontend, configure SPA fallback routing, add redirect rules, customize response headers, or choose between a static site and a web service for their frontend. Trigger terms: static site, CDN, SPA, single-page app, React deploy, Vue deploy, Hugo, Gatsby, Docusaurus, Jekyll, staticPublishPath.