diff --git a/Cargo.toml b/Cargo.toml index b452a5d..9fbb93e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,13 +24,14 @@ async_std = ["async-std"] mysql = ["sqlx/mysql", "sqlx/json"] [dependencies] -async-session = "3.0.0" -sqlx = { version = "0.6.2", features = ["chrono"] } +async-session = { git = "https://github.com/http-rs/async-session", branch = "main"} +sqlx = { version = "0.7.1", features = [ "time" ] } async-std = { version = "1.12.0", optional = true } +time = "0.3.18" [dev-dependencies] async-std = { version = "1.12.0", features = ["attributes"] } [dev-dependencies.sqlx] -version = "0.6.2" -features = ["chrono", "runtime-async-std-native-tls"] +version = "0.7.1" +features = ["runtime-async-std-native-tls"] diff --git a/src/mysql.rs b/src/mysql.rs index 0edf82c..372a6ea 100644 --- a/src/mysql.rs +++ b/src/mysql.rs @@ -235,7 +235,7 @@ impl MySqlSessionStore { let mut connection = self.connection().await?; sqlx::query(&self.substitute_table_name("DELETE FROM %%TABLE_NAME%% WHERE expires < ?")) .bind(Utc::now()) - .execute(&mut connection) + .execute(&mut *connection) .await?; Ok(()) @@ -261,7 +261,7 @@ impl MySqlSessionStore { pub async fn count(&self) -> sqlx::Result { let (count,) = sqlx::query_as(&self.substitute_table_name("SELECT COUNT(*) FROM %%TABLE_NAME%%")) - .fetch_one(&mut self.connection().await?) + .fetch_one(&mut *self.connection().await?) .await?; Ok(count) @@ -279,7 +279,7 @@ impl SessionStore for MySqlSessionStore { )) .bind(&id) .bind(Utc::now()) - .fetch_optional(&mut connection) + .fetch_optional(&mut *connection) .await?; Ok(result @@ -304,7 +304,7 @@ impl SessionStore for MySqlSessionStore { .bind(&id) .bind(&string) .bind(&session.expiry()) - .execute(&mut connection) + .execute(&mut *connection) .await?; Ok(session.into_cookie_value()) @@ -315,7 +315,7 @@ impl SessionStore for MySqlSessionStore { let mut connection = self.connection().await?; sqlx::query(&self.substitute_table_name("DELETE FROM %%TABLE_NAME%% WHERE id = ?")) .bind(&id) - .execute(&mut connection) + .execute(&mut *connection) .await?; Ok(()) @@ -324,7 +324,7 @@ impl SessionStore for MySqlSessionStore { async fn clear_store(&self) -> Result { let mut connection = self.connection().await?; sqlx::query(&self.substitute_table_name("TRUNCATE %%TABLE_NAME%%")) - .execute(&mut connection) + .execute(&mut *connection) .await?; Ok(()) @@ -360,9 +360,9 @@ mod tests { let cloned = session.clone(); let cookie_value = store.store_session(session).await?.unwrap(); - let (id, expires, serialized, count): (String, Option>, String, i64) = + let (id, expires, serialized, count): (String, Option, String, i64) = sqlx::query_as("select id, expires, session, (select count(*) from async_sessions) from async_sessions") - .fetch_one(&mut store.connection().await?) + .fetch_one(&mut *store.connection().await?) .await?; assert_eq!(1, count); @@ -399,7 +399,7 @@ mod tests { let (id, count): (String, i64) = sqlx::query_as("select id, (select count(*) from async_sessions) from async_sessions") - .fetch_one(&mut store.connection().await?) + .fetch_one(&mut *store.connection().await?) .await?; assert_eq!(1, count); @@ -426,10 +426,10 @@ mod tests { let session = store.load_session(cookie_value.clone()).await?.unwrap(); assert_eq!(session.expiry().unwrap(), &new_expires); - let (id, expires, count): (String, DateTime, i64) = sqlx::query_as( + let (id, expires, count): (String, DateTime, i64) = sqlx::query_as( "select id, expires, (select count(*) from async_sessions) from async_sessions", ) - .fetch_one(&mut store.connection().await?) + .fetch_one(&mut *store.connection().await?) .await?; assert_eq!(1, count); @@ -449,9 +449,9 @@ mod tests { let cookie_value = store.store_session(session).await?.unwrap(); - let (id, expires, serialized, count): (String, Option>, String, i64) = + let (id, expires, serialized, count): (String, Option, String, i64) = sqlx::query_as("select id, expires, session, (select count(*) from async_sessions) from async_sessions") - .fetch_one(&mut store.connection().await?) + .fetch_one(&mut *store.connection().await?) .await?; assert_eq!(1, count); diff --git a/src/pg.rs b/src/pg.rs index 6cdf4a6..3955c40 100644 --- a/src/pg.rs +++ b/src/pg.rs @@ -1,6 +1,8 @@ -use async_session::{async_trait, chrono::Utc, log, serde_json, Result, Session, SessionStore}; +use async_session::{async_trait, log, serde_json, Result, Session, SessionStore}; use sqlx::{pool::PoolConnection, Executor, PgPool, Postgres}; +use time::OffsetDateTime as DateTime; + /// sqlx postgres session store for async-sessions /// /// ```rust @@ -216,13 +218,14 @@ impl PostgresSessionStore { /// (expired) sessions. You may want to call this from cron. /// ```rust /// # use async_sqlx_session::PostgresSessionStore; - /// # use async_session::{chrono::{Utc,Duration}, Result, SessionStore, Session}; + /// # use async_session::{Result, SessionStore, Session}; + /// # use time::Duration; /// # fn main() -> Result { async_std::task::block_on(async { /// let store = PostgresSessionStore::new(&std::env::var("PG_TEST_DB_URL").unwrap()).await?; /// store.migrate().await?; /// # store.clear_store().await?; /// let mut session = Session::new(); - /// session.set_expiry(Utc::now() - Duration::seconds(5)); + /// session.set_expiry(time::OffsetDateTime::now_utc() - Duration::seconds(5)); /// store.store_session(session).await?; /// assert_eq!(store.count().await?, 1); /// store.cleanup().await?; @@ -232,8 +235,8 @@ impl PostgresSessionStore { pub async fn cleanup(&self) -> sqlx::Result<()> { let mut connection = self.connection().await?; sqlx::query(&self.substitute_table_name("DELETE FROM %%TABLE_NAME%% WHERE expires < $1")) - .bind(Utc::now()) - .execute(&mut connection) + .bind(DateTime::now_utc()) + .execute(&mut *connection) .await?; Ok(()) @@ -259,7 +262,7 @@ impl PostgresSessionStore { pub async fn count(&self) -> sqlx::Result { let (count,) = sqlx::query_as(&self.substitute_table_name("SELECT COUNT(*) FROM %%TABLE_NAME%%")) - .fetch_one(&mut self.connection().await?) + .fetch_one(&mut *self.connection().await?) .await?; Ok(count) @@ -276,8 +279,8 @@ impl SessionStore for PostgresSessionStore { "SELECT session FROM %%TABLE_NAME%% WHERE id = $1 AND (expires IS NULL OR expires > $2)" )) .bind(&id) - .bind(Utc::now()) - .fetch_optional(&mut connection) + .bind(DateTime::now_utc()) + .fetch_optional(&mut *connection) .await?; Ok(result @@ -302,7 +305,7 @@ impl SessionStore for PostgresSessionStore { .bind(&id) .bind(&string) .bind(&session.expiry()) - .execute(&mut connection) + .execute(&mut *connection) .await?; Ok(session.into_cookie_value()) @@ -313,7 +316,7 @@ impl SessionStore for PostgresSessionStore { let mut connection = self.connection().await?; sqlx::query(&self.substitute_table_name("DELETE FROM %%TABLE_NAME%% WHERE id = $1")) .bind(&id) - .execute(&mut connection) + .execute(&mut *connection) .await?; Ok(()) @@ -322,7 +325,7 @@ impl SessionStore for PostgresSessionStore { async fn clear_store(&self) -> Result { let mut connection = self.connection().await?; sqlx::query(&self.substitute_table_name("TRUNCATE %%TABLE_NAME%%")) - .execute(&mut connection) + .execute(&mut *connection) .await?; Ok(()) @@ -332,7 +335,6 @@ impl SessionStore for PostgresSessionStore { #[cfg(test)] mod tests { use super::*; - use async_session::chrono::DateTime; use std::time::Duration; async fn test_store() -> PostgresSessionStore { @@ -358,9 +360,9 @@ mod tests { let cloned = session.clone(); let cookie_value = store.store_session(session).await?.unwrap(); - let (id, expires, serialized, count): (String, Option>, String, i64) = + let (id, expires, serialized, count): (String, Option, String, i64) = sqlx::query_as("select id, expires, session, (select count(*) from async_sessions) from async_sessions") - .fetch_one(&mut store.connection().await?) + .fetch_one(&mut *store.connection().await?) .await?; assert_eq!(1, count); @@ -397,7 +399,7 @@ mod tests { let (id, count): (String, i64) = sqlx::query_as("select id, (select count(*) from async_sessions) from async_sessions") - .fetch_one(&mut store.connection().await?) + .fetch_one(&mut *store.connection().await?) .await?; assert_eq!(1, count); @@ -424,14 +426,14 @@ mod tests { let session = store.load_session(cookie_value.clone()).await?.unwrap(); assert_eq!(session.expiry().unwrap(), &new_expires); - let (id, expires, count): (String, DateTime, i64) = sqlx::query_as( + let (id, expires, count): (String, DateTime, i64) = sqlx::query_as( "select id, expires, (select count(*) from async_sessions) from async_sessions", ) - .fetch_one(&mut store.connection().await?) + .fetch_one(&mut *store.connection().await?) .await?; assert_eq!(1, count); - assert_eq!(expires.timestamp_millis(), new_expires.timestamp_millis()); + assert_eq!(expires.unix_timestamp(), new_expires.unix_timestamp()); assert_eq!(original_id, id); Ok(()) @@ -447,14 +449,14 @@ mod tests { let cookie_value = store.store_session(session).await?.unwrap(); - let (id, expires, serialized, count): (String, Option>, String, i64) = + let (id, expires, serialized, count): (String, Option, String, i64) = sqlx::query_as("select id, expires, session, (select count(*) from async_sessions) from async_sessions") - .fetch_one(&mut store.connection().await?) + .fetch_one(&mut *store.connection().await?) .await?; assert_eq!(1, count); assert_eq!(id, cloned.id()); - assert!(expires.unwrap() > Utc::now()); + assert!(expires.unwrap() > DateTime::now_utc()); let deserialized_session: Session = serde_json::from_str(&serialized)?; assert_eq!(cloned.id(), deserialized_session.id()); diff --git a/src/sqlite.rs b/src/sqlite.rs index 77e973f..a02df0c 100644 --- a/src/sqlite.rs +++ b/src/sqlite.rs @@ -238,7 +238,7 @@ impl SqliteSessionStore { "#, )) .bind(Utc::now().timestamp()) - .execute(&mut connection) + .execute(&mut *connection) .await?; Ok(()) @@ -263,7 +263,7 @@ impl SqliteSessionStore { pub async fn count(&self) -> sqlx::Result { let (count,) = sqlx::query_as(&self.substitute_table_name("SELECT COUNT(*) FROM %%TABLE_NAME%%")) - .fetch_one(&mut self.connection().await?) + .fetch_one(&mut *self.connection().await?) .await?; Ok(count) @@ -284,7 +284,7 @@ impl SessionStore for SqliteSessionStore { )) .bind(&id) .bind(Utc::now().timestamp()) - .fetch_optional(&mut connection) + .fetch_optional(&mut *connection) .await?; Ok(result @@ -309,7 +309,7 @@ impl SessionStore for SqliteSessionStore { .bind(&id) .bind(&string) .bind(&session.expiry().map(|expiry| expiry.timestamp())) - .execute(&mut connection) + .execute(&mut *connection) .await?; Ok(session.into_cookie_value()) @@ -324,7 +324,7 @@ impl SessionStore for SqliteSessionStore { "#, )) .bind(&id) - .execute(&mut connection) + .execute(&mut *connection) .await?; Ok(()) @@ -337,7 +337,7 @@ impl SessionStore for SqliteSessionStore { DELETE FROM %%TABLE_NAME%% "#, )) - .execute(&mut connection) + .execute(&mut *connection) .await?; Ok(()) @@ -370,7 +370,7 @@ mod tests { let (id, expires, serialized, count): (String, Option, String, i64) = sqlx::query_as("select id, expires, session, count(*) from async_sessions") - .fetch_one(&mut store.connection().await?) + .fetch_one(&mut *store.connection().await?) .await?; assert_eq!(1, count); @@ -406,7 +406,7 @@ mod tests { assert_eq!(session.get::("key").unwrap(), "other value"); let (id, count): (String, i64) = sqlx::query_as("select id, count(*) from async_sessions") - .fetch_one(&mut store.connection().await?) + .fetch_one(&mut *store.connection().await?) .await?; assert_eq!(1, count); @@ -435,7 +435,7 @@ mod tests { let (id, expires, count): (String, i64, i64) = sqlx::query_as("select id, expires, count(*) from async_sessions") - .fetch_one(&mut store.connection().await?) + .fetch_one(&mut *store.connection().await?) .await?; assert_eq!(1, count); @@ -457,7 +457,7 @@ mod tests { let (id, expires, serialized, count): (String, Option, String, i64) = sqlx::query_as("select id, expires, session, count(*) from async_sessions") - .fetch_one(&mut store.connection().await?) + .fetch_one(&mut *store.connection().await?) .await?; assert_eq!(1, count);