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! and query_as! for compile-time checked SQL. Use
dynamic SQL only when the query shape truly changes at runtime.
- Inject
PgPool, PgConnection, or Transaction explicitly. 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_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.
Source: hashgraph-online/awesome-codex-plugins → plugins/LVTD-LLC/skills/skills/rust-sqlx-postgres-service/SKILL.md
1---2name: rust-sqlx-postgres-service3description: 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.4---567# Rust SQLx Postgres Service89Use this skill to make Rust/Postgres changes schema-first, compile-checked, and10testable. Prefer explicit migrations, injected pools, narrow repository11functions, and transaction boundaries that match the business operation.1213## Core Workflow14151. Inspect `Cargo.toml`, `migrations/`, `.sqlx/`, `docker-compose.yml`, CI, and16 existing database helper modules before changing code.172. Design the schema change first. Create or update a migration before writing18 repository code that depends on it.193. Prefer `sqlx::query!` and `query_as!` for compile-time checked SQL. Use20 dynamic SQL only when the query shape truly changes at runtime.214. Inject `PgPool`, `PgConnection`, or `Transaction` explicitly. Do not create a22 pool inside handlers or repository functions.235. Keep repository functions small and named by behavior. Return domain types24 or persistence DTOs instead of leaking raw rows through the app.256. Use a transaction for multi-step operations that must commit or roll back26 together.277. Add or update integration tests that run migrations and exercise the public28 behavior when the change affects API semantics.298. Run the bundled preflight script or the repository's equivalent SQLx/Cargo30 CI commands.3132## Migration Rules3334Read `references/migration-safety.md` before changing existing tables or data.3536Default migration preferences:3738- Use additive changes first: new nullable columns, new tables, new indexes.39- Backfill separately from schema creation when a table may be large.40- Add `not null`, uniqueness, and foreign-key constraints only after existing41 data satisfies them.42- Name constraints and indexes deliberately when they will appear in errors or43 operations.44- Keep down migrations if the project uses them; otherwise document rollback45 expectations in the migration review.4647## Query Rules4849Read `references/sqlx-patterns.md` for SQLx macro, transaction, and offline50mode details.5152- Prefer bind parameters over string interpolation.53- Use `fetch_optional` for lookup-by-key operations.54- Use `execute` for inserts/updates where row data is not needed.55- Use `RETURNING` when the caller needs generated IDs or timestamps.56- Check affected row counts for updates and deletes that should target exactly57 one row.58- Map database uniqueness conflicts to domain errors near the repository edge.5960## Verification6162Run from the Rust project root when the standard SQLx workflow applies:6364```bash65path/to/rust-sqlx-postgres-service/scripts/sqlx-preflight.sh66```6768The script is conservative: it checks formatting and tests, inspects SQLx69migration state when the CLI and `DATABASE_URL` are available, verifies SQLx70offline metadata with `cargo sqlx prepare --check` when `.sqlx/` or71`DATABASE_URL` is available, and only applies migrations when72`RUST_SQLX_PREFLIGHT_RUN_MIGRATIONS=1` is set.7374If the repository has a different CI command, use that command and explain why.7576## Reference Files7778- `references/sqlx-patterns.md`: `query!`, `query_as!`, pools, transactions,79 feature flags, and offline mode.80- `references/migration-safety.md`: safe migration sequencing and rollout81 checks.8283---8485**Source:** [`hashgraph-online/awesome-codex-plugins`](https://github.com/hashgraph-online/awesome-codex-plugins) → `plugins/LVTD-LLC/skills/skills/rust-sqlx-postgres-service/SKILL.md`