feat(orm)!: transactions support - #468
Conversation
Codecov Report❌ Patch coverage is
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
|
| Project | cot |
| Branch | transactions |
| Testbed | github-ubuntu-latest |
Click to view all benchmark results
| Benchmark | Latency | Benchmark Result microseconds (µs) (Result Δ%) | Upper Boundary microseconds (µs) (Limit %) |
|---|---|---|---|
| empty_router/empty_router | 📈 view plot 🚷 view threshold | 14,492.00 µs(+75.96%)Baseline: 8,236.15 µs | 15,503.45 µs (93.48%) |
| json_api/json_api | 📈 view plot 🚷 view threshold | 1,108.00 µs(+4.65%)Baseline: 1,058.72 µs | 1,383.29 µs (80.10%) |
| nested_routers/nested_routers | 📈 view plot 🚷 view threshold | 1,027.90 µs(+3.93%)Baseline: 989.04 µs | 1,263.56 µs (81.35%) |
| single_root_route/single_root_route | 📈 view plot 🚷 view threshold | 993.87 µs(+4.32%)Baseline: 952.68 µs | 1,227.11 µs (80.99%) |
| single_root_route_burst/single_root_route_burst | 📈 view plot 🚷 view threshold | 16,144.00 µs(-5.46%)Baseline: 17,077.06 µs | 21,559.37 µs (74.88%) |
There was a problem hiding this comment.
Pull request overview
Adds first-class transaction support to Cot’s ORM layer and updates higher-level components (session store, auth, examples, and macros) to execute ORM operations against either a direct database connection or an in-flight transaction.
Changes:
- Introduces a
Transactiontype and internal executor abstraction so ORM operations can run on both&Databaseand transactions (including nested transactions/savepoints). - Refactors ORM APIs (
DatabaseBackend,Model,Query,ForeignKey, auth DB helpers) to accept mutable executors and support transactional execution. - Updates session store and example app initialization to use transactions; adds new DB tests covering commit/rollback/nested behavior.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| examples/admin/src/main.rs | Uses a DB transaction when ensuring the admin user exists. |
| cot/tests/db.rs | Adds tests for transaction commit, rollback, and nested transactions. |
| cot/src/session/store/db.rs | Wraps session save in a transaction and reuses create logic via an executor parameter. |
| cot/src/session/store/db.rs | (same file) Adjusts DB calls to work with either connection or transaction. |
| cot/src/db/sea_query_db.rs | Exposes backend macro internally and adds a SeaQuery-based transaction backend macro. |
| cot/src/db/relations.rs | Updates foreign key loading to operate on a mutable executor. |
| cot/src/db/query.rs | Refactors query execution to accept a generic mutable DatabaseBackend, including count. |
| cot/src/db/impl_sqlite.rs | Wires in the SeaQuery transaction backend for SQLite. |
| cot/src/db/impl_postgres.rs | Wires in the SeaQuery transaction backend for PostgreSQL. |
| cot/src/db/impl_mysql.rs | Wires in the SeaQuery transaction backend for MySQL. |
| cot/src/db.rs | Adds Transaction, executor plumbing, and updates DatabaseBackend to be &mut self-based. |
| cot/src/auth/db.rs | Updates auth DB helpers/tests to work with the new mutable-executor ORM APIs. |
| cot-macros/src/model.rs | Updates generated Model impls to use cot::db::async_trait and the new executor signatures. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
cot/src/session/store/db.rs:136
- This helper is used by
create()/save()but still usesunwrap()/expect()when building theSessionmodel (JSON serialization + expiry conversion). Those fallible conversions should return asession_store::Errorinstead of panicking, to avoid crashing the server on unexpected input.
expiry,
};
let res = db.insert(&mut model).await;
25ab4ac to
20da089
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 39 out of 40 changed files in this pull request and generated no new comments.
Suppressed comments (3)
cot/src/session/store/db.rs:1
- The unique-collision retry loop never changes the session key/ID, so a
UniqueViolationwill deterministically repeat and the retries cannot succeed. OnUniqueViolation, regeneraterecord.id(or otherwise choose a new key), rebuild the model, and retry; if tower-sessions provides a helper for this, use that API to ensure consistent ID format.
//! Database-backed session store.
docs/databases/transactions.md:1
- The examples use
#[model]andLimitedStringbut only importAutoandDatabase. To keep the snippets copy-pasteable, either add the missing imports (e.g.,use cot::db::{model, LimitedString};) in hidden#lines or switch the example field type fromLimitedString<...>toString.
cot/src/lib.rs:83 - Changing
utilsfrompub mod utils;topub(crate) mod utils;is a breaking public API change for downstream crates that may importcot::utils::*. If this is intentional (e.g., because functionality moved tocot-cli), it should be reflected in the crate’s release notes/versioning; otherwise, consider keepingutilspublic or re-exporting the specific items that are still supported.
pub(crate) mod utils;
629ad59 to
93f50a6
Compare
No description provided.