Skip to content

Commit

Permalink
Merge pull request #17 from wasdacraic/upgrade-axum
Browse files Browse the repository at this point in the history
Upgrade `axum` to 0.6
  • Loading branch information
connec authored Feb 8, 2023
2 parents e98b009 + 302717b commit ca316d8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
22 changes: 11 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "axum-sqlx-tx"
description = "Request-scoped SQLx transactions for axum"
version = "0.4.1"
version = "0.5.0"
license = "MIT"
repository = "https://github.com/wasdacraic/axum-sqlx-tx/"
edition = "2021"
Expand All @@ -27,20 +27,20 @@ runtime-tokio-rustls = ["sqlx/runtime-tokio-rustls"]
features = ["all-databases", "runtime-tokio-rustls"]

[dependencies]
axum-core = "0.2.1"
bytes = "1.1.0"
futures-core = "0.3.21"
http = "0.2.6"
http-body = "0.4.4"
parking_lot = "0.12.0"
axum-core = "0.3"
bytes = "1"
futures-core = "0.3"
http = "0.2"
http-body = "0.4"
parking_lot = "0.12"
sqlx = { version = "0.6", default-features = false }
thiserror = "1.0.30"
tower-layer = "0.3.1"
tower-service = "0.3.1"
thiserror = "1"
tower-layer = "0.3"
tower-service = "0.3"

[dev-dependencies]
axum-sqlx-tx = { path = ".", features = ["runtime-tokio-rustls", "sqlite"] }
axum = "0.5.1"
axum = "0.6.4"
hyper = "0.14.17"
tempfile = "3.3.0"
tokio = { version = "1.17.0", features = ["macros"] }
Expand Down
9 changes: 9 additions & 0 deletions src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ impl<DB: sqlx::Database> Layer<DB> {
}
}

impl<DB: sqlx::Database, E> Clone for Layer<DB, E> {
fn clone(&self) -> Self {
Self {
pool: self.pool.clone(),
_error: self._error,
}
}
}

impl<DB: sqlx::Database, S, E> tower_layer::Layer<S> for Layer<DB, E> {
type Service = Service<DB, S, E>;

Expand Down
22 changes: 9 additions & 13 deletions src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

use std::marker::PhantomData;

use axum_core::{
extract::{FromRequest, RequestParts},
response::IntoResponse,
};
use axum_core::{extract::FromRequestParts, response::IntoResponse};
use http::request::Parts;
use sqlx::Transaction;

use crate::{
Expand Down Expand Up @@ -114,25 +112,23 @@ impl<DB: sqlx::Database, E> std::ops::DerefMut for Tx<DB, E> {
}
}

impl<DB: sqlx::Database, B, E> FromRequest<B> for Tx<DB, E>
impl<DB: sqlx::Database, S, E> FromRequestParts<S> for Tx<DB, E>
where
B: Send,
E: From<Error> + IntoResponse,
{
type Rejection = E;

fn from_request<'req, 'ctx>(
req: &'req mut RequestParts<B>,
fn from_request_parts<'req, 'state, 'ctx>(
parts: &'req mut Parts,
_state: &'state S,
) -> futures_core::future::BoxFuture<'ctx, Result<Self, Self::Rejection>>
where
'req: 'ctx,
Self: 'ctx,
'req: 'ctx,
'state: 'ctx,
{
Box::pin(async move {
let ext: &mut Lazy<DB> = req
.extensions_mut()
.get_mut()
.ok_or(Error::MissingExtension)?;
let ext: &mut Lazy<DB> = parts.extensions.get_mut().ok_or(Error::MissingExtension)?;

let tx = ext.get_or_begin().await?;

Expand Down
2 changes: 1 addition & 1 deletion tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ struct Response {

async fn build_app<H, T>(handler: H) -> (NamedTempFile, sqlx::SqlitePool, Response)
where
H: axum::handler::Handler<T, axum::body::Body>,
H: axum::handler::Handler<T, (), axum::body::Body>,
T: 'static,
{
let db = NamedTempFile::new().unwrap();
Expand Down

0 comments on commit ca316d8

Please sign in to comment.