Skip to content

Commit

Permalink
Make chrono work (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
karuna committed Sep 2, 2024
1 parent 9873452 commit d3be5e3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 18 deletions.
15 changes: 10 additions & 5 deletions sqlx-store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ documentation = "https://docs.rs/tower-sessions-sqlx-store"
readme = "README.md"

[features]
default = ["time"]
sqlite = ["sqlx/sqlite"]
postgres = ["sqlx/postgres"]
mysql = ["sqlx/mysql"]

time = ["sqlx/time", "dep:time"]
chrono = ["sqlx/chrono", "dep:chrono"]

[dependencies]
async-trait = "0.1.77"
rmp-serde = "1.1.2"
sqlx = { version = "0.8.0", features = ["time", "runtime-tokio"] }
sqlx = { version = "0.8.0", features = ["runtime-tokio"] }
thiserror = "1.0.56"
time = "0.3.31"
time = { version = "0.3.31", optional = true }
chrono = { version = "0.4.38", optional = true }
tower-sessions-core = { version = "0.12.1", features = ["deletion-task"] }

[dev-dependencies]
Expand All @@ -34,12 +39,12 @@ serde = "1"

[[example]]
name = "sqlite"
required-features = ["sqlite"]
required-features = ["sqlite", "time"]

[[example]]
name = "postgres"
required-features = ["postgres"]
required-features = ["postgres", "time"]

[[example]]
name = "mysql"
required-features = ["mysql"]
required-features = ["mysql", "time"]
28 changes: 28 additions & 0 deletions sqlx-store/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
pub use sqlx;
use tower_sessions_core::session_store;

#[cfg(any(
all(feature = "time", feature = "chrono"),
all(not(feature = "time"), not(feature = "chrono"))
))]
compile_error!("Exactly one of `time` and `chrono` features must be enabled. This is due to a change in sqlx where chrono types can only be enabled if time is disabled.");

#[cfg(feature = "mysql")]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
pub use self::mysql_store::MySqlStore;
Expand Down Expand Up @@ -48,3 +54,25 @@ impl From<SqlxStoreError> for session_store::Error {
}
}
}

#[cfg(feature = "time")]
pub fn current_time() -> time::OffsetDateTime {
time::OffsetDateTime::now_utc()
}

#[cfg(feature = "chrono")]
pub fn current_time() -> chrono::DateTime<chrono::Utc> {
chrono::Utc::now()
}

#[cfg(feature = "time")]
pub fn convert_expiry_date(expiry_date: time::OffsetDateTime) -> time::OffsetDateTime {
expiry_date
}

#[cfg(feature = "chrono")]
pub fn convert_expiry_date(expiry_date: time::OffsetDateTime) -> chrono::DateTime<chrono::Utc> {
// if we can't convert the expiry date to a chrono type, return the current time i.e. effectively assume our session has expired
chrono::DateTime::from_timestamp(expiry_date.unix_timestamp(), expiry_date.nanosecond())
.unwrap_or(chrono::Utc::now())
}
7 changes: 3 additions & 4 deletions sqlx-store/src/mysql_store.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use async_trait::async_trait;
use sqlx::{MySqlConnection, MySqlPool};
use time::OffsetDateTime;
use tower_sessions_core::{
session::{Id, Record},
session_store, ExpiredDeletion, SessionStore,
};

use crate::SqlxStoreError;
use crate::{convert_expiry_date, current_time, SqlxStoreError};

/// A MySQL session store.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -147,7 +146,7 @@ impl MySqlStore {
sqlx::query(&query)
.bind(record.id.to_string())
.bind(rmp_serde::to_vec(&record).map_err(SqlxStoreError::Encode)?)
.bind(record.expiry_date)
.bind(convert_expiry_date(record.expiry_date))
.execute(conn)
.await
.map_err(SqlxStoreError::Sqlx)?;
Expand Down Expand Up @@ -205,7 +204,7 @@ impl SessionStore for MySqlStore {
);
let data: Option<(Vec<u8>,)> = sqlx::query_as(&query)
.bind(session_id.to_string())
.bind(OffsetDateTime::now_utc())
.bind(current_time())
.fetch_optional(&self.pool)
.await
.map_err(SqlxStoreError::Sqlx)?;
Expand Down
10 changes: 5 additions & 5 deletions sqlx-store/src/postgres_store.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::{convert_expiry_date, current_time, SqlxStoreError};
use async_trait::async_trait;
use sqlx::{PgConnection, PgPool};
use time::OffsetDateTime;
use tower_sessions_core::{
session::{Id, Record},
session_store, ExpiredDeletion, SessionStore,
};

use crate::SqlxStoreError;

/// A PostgreSQL session store.
#[derive(Clone, Debug)]
pub struct PostgresStore {
Expand Down Expand Up @@ -157,10 +155,11 @@ impl PostgresStore {
schema_name = self.schema_name,
table_name = self.table_name
);

sqlx::query(&query)
.bind(record.id.to_string())
.bind(rmp_serde::to_vec(&record).map_err(SqlxStoreError::Encode)?)
.bind(record.expiry_date)
.bind(convert_expiry_date(record.expiry_date))
.execute(conn)
.await
.map_err(SqlxStoreError::Sqlx)?;
Expand Down Expand Up @@ -217,9 +216,10 @@ impl SessionStore for PostgresStore {
schema_name = self.schema_name,
table_name = self.table_name
);

let record_value: Option<(Vec<u8>,)> = sqlx::query_as(&query)
.bind(session_id.to_string())
.bind(OffsetDateTime::now_utc())
.bind(current_time())
.fetch_optional(&self.pool)
.await
.map_err(SqlxStoreError::Sqlx)?;
Expand Down
7 changes: 3 additions & 4 deletions sqlx-store/src/sqlite_store.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use async_trait::async_trait;
use sqlx::{sqlite::SqlitePool, SqliteConnection};
use time::OffsetDateTime;
use tower_sessions_core::{
session::{Id, Record},
session_store::{self, ExpiredDeletion},
SessionStore,
};

use crate::SqlxStoreError;
use crate::{convert_expiry_date, current_time, SqlxStoreError};

/// A SQLite session store.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -101,7 +100,7 @@ impl SqliteStore {
sqlx::query(&query)
.bind(record.id.to_string())
.bind(rmp_serde::to_vec(record).map_err(SqlxStoreError::Encode)?)
.bind(record.expiry_date)
.bind(convert_expiry_date(record.expiry_date))
.execute(conn)
.await
.map_err(SqlxStoreError::Sqlx)?;
Expand Down Expand Up @@ -158,7 +157,7 @@ impl SessionStore for SqliteStore {
);
let data: Option<(Vec<u8>,)> = sqlx::query_as(&query)
.bind(session_id.to_string())
.bind(OffsetDateTime::now_utc())
.bind(current_time())
.fetch_optional(&self.pool)
.await
.map_err(SqlxStoreError::Sqlx)?;
Expand Down
1 change: 1 addition & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ tower-sessions-sqlx-store = { path = "../sqlx-store/", features = [
"sqlite",
"mysql",
"postgres",
"time",
] }
tower-sessions-redis-store = { path = "../redis-store/" }
tower-sessions-mongodb-store = { path = "../mongodb-store/" }
Expand Down

0 comments on commit d3be5e3

Please sign in to comment.