Rust Database Layer Delivery
Deliver a migration-compatible, rollback-capable, and observable data access layer based on the actual dialects, schemas, and transaction semantics of databases. Do not treat HTTP handlers as repositories; do not use mocks to validate real database behavior.
Confirming Database Contracts
Before modifications:
- Collect information about the database product, exact version, extensions, deployment topology, read/write nodes;
- Gather current schema constraints, indexes, triggers, views, and migration history;
- Identify locked versions of SQLx, Diesel, SeaORM (or other access stacks) along with their features;
- Determine Rust toolchain/MSRV, synchronous or asynchronous execution models;
- Establish connection limits, instance counts, timeouts, transaction isolation levels, and consistency requirements;
- Define data volume profiles, hot query patterns, pagination strategies, retention policies, and archival needs;
- Document credential sources, TLS configurations, tenant boundaries, row-level permissions, and audit requirements;
- Clarify whether deployments allow downtime, double writes, backfills, or destructive DDL operations.
When real database access is unavailable, distinguish between "code/offline metadata validation" and "real dialect validation." Do not conflate these two approaches in reporting.
Workflow
1. Establish Current Baseline
rustc --version --verbose
cargo metadata --format-version 1
cargo tree -e features
cargo test --all-targets
Locate connection initialization, pool configuration, repository implementations, queries, migrations, test fixtures, and CI database connections. Execute existing migration status/check commands; do not run operations that modify shared databases without explicit authorization.
2. Preserve Existing Access Stacks
Avoid rewriting solely based on preference among SQLx, Diesel, or SeaORM. Select the appropriate stack according to current codebase and task requirements:
- SQLx: Requires direct SQL access, asynchronous querying, and optional compile-time query validation;
- Diesel: Utilizes typed DSL queries, synchronous connections, or existing Diesel schemas;
- SeaORM: Leverages async ORM/entity models with corresponding migration systems;
- Native database drivers: Reserved for cases where protocol or functionality does not support a generic layer, though expanding maintenance and security review scope.
Specific selection criteria and type boundaries are documented in Access Stack and Types. All APIs must align with the versioned documentation from Cargo.lock, avoiding direct application of latest examples to legacy projects.
3. Let Schema Constraints Enforce Facts
- Define primary keys, uniqueness constraints, foreign keys, non-null checks, and check constraints as immutable invariants guaranteed by the database;
- Rust types express domain semantics but cannot replace cross-process concurrency guarantees provided by databases;
- Explicitly specify integer widths, decimal precision, timezones, UUIDs, JSON formats, nullability mappings;
- Bind parameters to all queries. Use dynamic identifiers or sort fields only via allowlists and type mapping—not as regular bind values pretending to be safe;
- Restrict column selection to necessary columns to avoid reliance on unstable
SELECT * layouts;
- Prioritize stable sorting strategies and keyset-based pagination over unbounded offset operations under high data volumes.
4. Design Migrations Before Code Changes
For each schema change, document:
- Forward DDL statements;
- Compatibility windows between old and new versions for applications;
- Data backfill methods, batch sizes, and rollback points;
- Index construction risks and lock contention implications;
- Rollback or forward recovery strategies;
- Validation queries, monitoring metrics, and termination conditions.
Adopt the sequence: expand → backfill → switch → contract. Destructive operations—column deletion, type changes, large table reconstruction, data clearing, irreversible backfills—require explicit target parsing and authorization details found in Migrations and Schema Evolution.
5. Place Transactions Within Complete Use Cases
- Initiate transactions via application services or repository unit-of-work units; do not commit transactionally across HTTP handlers indiscriminately;
- Ensure atomic success of writes within a single shared transaction and connection set;
- Avoid unbounded user interactions outside the transaction scope, including unnecessary external HTTP waits inside transactions;
- Select isolation levels per database documentation to identify risks such as lost updates, write skew, phantom reads;
- Retry only transient errors explicitly identified by the database. Set retry counts, backoff strategies, and idempotency boundaries;
- Commit failures are possible; do not treat "last SQL statement success" as transactional completion;
- Drop/rollback serves as a safety net but does not replace explicit control flow design or testing practices.
Connection pooling, isolation levels, and retry mechanisms are detailed in Transactions and Pooling.
6. Design Stable Data Access Interfaces
application use case
-> repository / unit of work port
-> SQL/ORM adapter
-> database
- Repository methods should accept domain parameters and results without leaking Web DTOs;
- Classify errors into categories: not found, uniqueness conflicts, foreign key violations, serialization failures, timeouts, unavailability;
- Preserve full error chains for logging and diagnostics while preventing sensitive SQL parameter exposure in user responses;
- Batch interfaces must define ordering semantics, partial failure handling, idempotency guarantees, and return row counts;
- Avoid N+1 queries. Validate performance under realistic data scales when pre-fetching joins or batching is required.
7. Configure Limited Connection Budgets
Validate total connection budgets using the formula: (per-instance pool limit × maximum instance count) + operational connections. Set acquire, connect, statement, and transaction timeouts; define idle/max lifetimes, health checks, and graceful shutdown procedures. Do not instantiate a new pool per request nor let indefinite waits mask resource exhaustion issues.
8. Validate with Real Engines
Conduct at least the following validations:
- Execute all migrations from an empty database;
- Upgrade schema versions supported by existing databases;
- Verify repository success, missing records, constraint conflicts, and type boundaries;
- Test transaction commits, rollbacks, commit failures, and retryable concurrency issues;
- Ensure invariants hold under concurrent writes;
- Confirm query plans, index hits, and representative data volumes;
- Assess pool exhaustion scenarios, database restarts/disconnections, and graceful shutdown behaviors;
- Verify logs, metrics, and traces do not leak bound values or credentials.
Test execution and operation lists are provided in Testing and Operations.
Common Gates (Gateways)
cargo fmt --all -- --check
cargo check --all-targets
cargo test --all-targets
cargo clippy --all-targets -- -D warnings
Run repository-specific migration validation, SQLx offline metadata checks, Diesel schema audits, or real database integration tests. Commands and features may vary by version; consult project configuration first.
Completion Criteria
- Database versions, schemas, access stacks, and consistency assumptions are clearly defined;
- All queries use parameterization with dynamic identifiers filtered via allowlists;
- Migration compatibility windows, backfill strategies, recovery procedures, and destructive boundaries are documented;
- Transaction coverage spans complete use cases with justified connection budgets and timeouts;
- Real database testing validates migrations, constraints, rollbacks, and concurrency risks;
- fmt, check, test, Clippy pass; unvalidated real-database tests must be explicitly marked.
Handoff Boundaries
| Primary Responsibility |
Assigned To |
| HTTP routing, handlers, status codes, middleware |
rust-web |
| Credentials, tenant authorization, audit logs, sensitive field exposure |
rust-web-security |
| Async cancellation, locks, tasks, blocking isolation |
rust-concurrency |
| Test layering, coverage metrics, performance baselines |
rust-testing |
| Features, build scripts, offline metadata, release pipelines |
rust-cargo-build |
| Types, ownership semantics, error traits, standard library behavior |
rust-stable |
On-Demand Resources
- Access Stack and Types: Consult when selecting or adapting SQLx, Diesel, SeaORM, or type mappings.
- Migrations and Schema Evolution: Review DDL design, compatibility backfills, and recovery strategies.
- Transactions and Pooling: Refer for atomicity guarantees, isolation levels, retry logic, and capacity management.
- Testing and Operations: Access real-database test setups, query plans, observability practices.
- Scenario Examples: Use this template when end-to-end task decomposition is required.
examples/golden-transaction/: Offline compilation and validation of transaction boundary examples available here.
References
1---2name: rust-database3description: Design, implement, migrate, test, and operate Rust database access, including SQLx, Diesel, SeaORM, schema evolution, transaction boundaries, connection pools, retries, type mapping, concurrency control, and real-database verification. Use when users ask about Rust SQL or ORM code, migrations, transactions, PostgreSQL or MySQL integration, connection pools, query safety, or database production readiness.4---56# Rust Database Layer Delivery78Deliver a migration-compatible, rollback-capable, and observable data access layer based on the actual dialects, schemas, and transaction semantics of databases. Do not treat HTTP handlers as repositories; do not use mocks to validate real database behavior.910## Confirming Database Contracts1112Before modifications:13- Collect information about the database product, exact version, extensions, deployment topology, read/write nodes;14- Gather current schema constraints, indexes, triggers, views, and migration history;15- Identify locked versions of SQLx, Diesel, SeaORM (or other access stacks) along with their features;16- Determine Rust toolchain/MSRV, synchronous or asynchronous execution models;17- Establish connection limits, instance counts, timeouts, transaction isolation levels, and consistency requirements;18- Define data volume profiles, hot query patterns, pagination strategies, retention policies, and archival needs;19- Document credential sources, TLS configurations, tenant boundaries, row-level permissions, and audit requirements;20- Clarify whether deployments allow downtime, double writes, backfills, or destructive DDL operations.2122When real database access is unavailable, distinguish between "code/offline metadata validation" and "real dialect validation." Do not conflate these two approaches in reporting.2324## Workflow2526### 1. Establish Current Baseline27```bash28rustc --version --verbose29cargo metadata --format-version 130cargo tree -e features31cargo test --all-targets32```33Locate connection initialization, pool configuration, repository implementations, queries, migrations, test fixtures, and CI database connections. Execute existing migration status/check commands; do not run operations that modify shared databases without explicit authorization.3435### 2. Preserve Existing Access Stacks36Avoid rewriting solely based on preference among SQLx, Diesel, or SeaORM. Select the appropriate stack according to current codebase and task requirements:37- **SQLx**: Requires direct SQL access, asynchronous querying, and optional compile-time query validation;38- **Diesel**: Utilizes typed DSL queries, synchronous connections, or existing Diesel schemas;39- **SeaORM**: Leverages async ORM/entity models with corresponding migration systems;40- **Native database drivers**: Reserved for cases where protocol or functionality does not support a generic layer, though expanding maintenance and security review scope.4142Specific selection criteria and type boundaries are documented in [Access Stack and Types](references/access-stack-and-types.md). All APIs must align with the versioned documentation from `Cargo.lock`, avoiding direct application of latest examples to legacy projects.4344### 3. Let Schema Constraints Enforce Facts45- Define primary keys, uniqueness constraints, foreign keys, non-null checks, and check constraints as immutable invariants guaranteed by the database;46- Rust types express domain semantics but cannot replace cross-process concurrency guarantees provided by databases;47- Explicitly specify integer widths, decimal precision, timezones, UUIDs, JSON formats, nullability mappings;48- Bind parameters to all queries. Use dynamic identifiers or sort fields only via allowlists and type mapping—not as regular bind values pretending to be safe;49- Restrict column selection to necessary columns to avoid reliance on unstable `SELECT *` layouts;50- Prioritize stable sorting strategies and keyset-based pagination over unbounded offset operations under high data volumes.5152### 4. Design Migrations Before Code Changes53For each schema change, document:541. Forward DDL statements;552. Compatibility windows between old and new versions for applications;563. Data backfill methods, batch sizes, and rollback points;574. Index construction risks and lock contention implications;585. Rollback or forward recovery strategies;596. Validation queries, monitoring metrics, and termination conditions.6061Adopt the sequence: expand → backfill → switch → contract. Destructive operations—column deletion, type changes, large table reconstruction, data clearing, irreversible backfills—require explicit target parsing and authorization details found in [Migrations and Schema Evolution](references/migrations-and-schema.md).6263### 5. Place Transactions Within Complete Use Cases64- Initiate transactions via application services or repository unit-of-work units; do not commit transactionally across HTTP handlers indiscriminately;65- Ensure atomic success of writes within a single shared transaction and connection set;66- Avoid unbounded user interactions outside the transaction scope, including unnecessary external HTTP waits inside transactions;67- Select isolation levels per database documentation to identify risks such as lost updates, write skew, phantom reads;68- Retry only transient errors explicitly identified by the database. Set retry counts, backoff strategies, and idempotency boundaries;69- Commit failures are possible; do not treat "last SQL statement success" as transactional completion;70- Drop/rollback serves as a safety net but does not replace explicit control flow design or testing practices.7172Connection pooling, isolation levels, and retry mechanisms are detailed in [Transactions and Pooling](references/transactions-and-pooling.md).7374### 6. Design Stable Data Access Interfaces75```text76application use case77 -> repository / unit of work port78 -> SQL/ORM adapter79 -> database80```81- Repository methods should accept domain parameters and results without leaking Web DTOs;82- Classify errors into categories: not found, uniqueness conflicts, foreign key violations, serialization failures, timeouts, unavailability;83- Preserve full error chains for logging and diagnostics while preventing sensitive SQL parameter exposure in user responses;84- Batch interfaces must define ordering semantics, partial failure handling, idempotency guarantees, and return row counts;85- Avoid N+1 queries. Validate performance under realistic data scales when pre-fetching joins or batching is required.8687### 7. Configure Limited Connection Budgets88Validate total connection budgets using the formula: `(per-instance pool limit × maximum instance count) + operational connections`. Set acquire, connect, statement, and transaction timeouts; define idle/max lifetimes, health checks, and graceful shutdown procedures. Do not instantiate a new pool per request nor let indefinite waits mask resource exhaustion issues.8990### 8. Validate with Real Engines91Conduct at least the following validations:921. Execute all migrations from an empty database;932. Upgrade schema versions supported by existing databases;943. Verify repository success, missing records, constraint conflicts, and type boundaries;954. Test transaction commits, rollbacks, commit failures, and retryable concurrency issues;965. Ensure invariants hold under concurrent writes;976. Confirm query plans, index hits, and representative data volumes;987. Assess pool exhaustion scenarios, database restarts/disconnections, and graceful shutdown behaviors;998. Verify logs, metrics, and traces do not leak bound values or credentials.100101Test execution and operation lists are provided in [Testing and Operations](references/testing-and-operations.md).102103## Common Gates (Gateways)104105```bash106cargo fmt --all -- --check107cargo check --all-targets108cargo test --all-targets109cargo clippy --all-targets -- -D warnings110```111Run repository-specific migration validation, SQLx offline metadata checks, Diesel schema audits, or real database integration tests. Commands and features may vary by version; consult project configuration first.112113## Completion Criteria114- Database versions, schemas, access stacks, and consistency assumptions are clearly defined;115- All queries use parameterization with dynamic identifiers filtered via allowlists;116- Migration compatibility windows, backfill strategies, recovery procedures, and destructive boundaries are documented;117- Transaction coverage spans complete use cases with justified connection budgets and timeouts;118- Real database testing validates migrations, constraints, rollbacks, and concurrency risks;119- fmt, check, test, Clippy pass; unvalidated real-database tests must be explicitly marked.120121## Handoff Boundaries122123| Primary Responsibility | Assigned To |124|---|---|125| HTTP routing, handlers, status codes, middleware | `rust-web` |126| Credentials, tenant authorization, audit logs, sensitive field exposure | `rust-web-security` |127| Async cancellation, locks, tasks, blocking isolation | `rust-concurrency` |128| Test layering, coverage metrics, performance baselines | `rust-testing` |129| Features, build scripts, offline metadata, release pipelines | `rust-cargo-build` |130| Types, ownership semantics, error traits, standard library behavior | `rust-stable` |131132## On-Demand Resources133- [Access Stack and Types](references/access-stack-and-types.md): Consult when selecting or adapting SQLx, Diesel, SeaORM, or type mappings.134- [Migrations and Schema Evolution](references/migrations-and-schema.md): Review DDL design, compatibility backfills, and recovery strategies.135- [Transactions and Pooling](references/transactions-and-pooling.md): Refer for atomicity guarantees, isolation levels, retry logic, and capacity management.136- [Testing and Operations](references/testing-and-operations.md): Access real-database test setups, query plans, observability practices.137- [Scenario Examples](examples/examples.md): Use this template when end-to-end task decomposition is required.138- `examples/golden-transaction/`: Offline compilation and validation of transaction boundary examples available here.139140## References141- [`std::error`](https://doc.rust-lang.org/stable/std/error/) (Rust Standard Library)142- [SQLx documentation](https://docs.rs/sqlx/)143- [Diesel guides](https://diesel.rs/guides/)144- [SeaORM documentation](https://www.sea-ql.org/SeaORM/)