Skip to content

Commit

Permalink
Revert "Make chrono work (#46)"
Browse files Browse the repository at this point in the history
This would break feature unification.

This reverts commit d3be5e3.
  • Loading branch information
maxcountryman committed Sep 3, 2024
1 parent d3be5e3 commit 3a546a9
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 50 deletions.
15 changes: 5 additions & 10 deletions sqlx-store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,16 @@ 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 = ["runtime-tokio"] }
sqlx = { version = "0.8.0", features = ["time", "runtime-tokio"] }
thiserror = "1.0.56"
time = { version = "0.3.31", optional = true }
chrono = { version = "0.4.38", optional = true }
time = "0.3.31"
tower-sessions-core = { version = "0.12.1", features = ["deletion-task"] }

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

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

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

[[example]]
name = "mysql"
required-features = ["mysql", "time"]
required-features = ["mysql"]
28 changes: 0 additions & 28 deletions sqlx-store/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
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 @@ -54,25 +48,3 @@ 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: 4 additions & 3 deletions sqlx-store/src/mysql_store.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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::{convert_expiry_date, current_time, SqlxStoreError};
use crate::SqlxStoreError;

/// A MySQL session store.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -146,7 +147,7 @@ impl MySqlStore {
sqlx::query(&query)
.bind(record.id.to_string())
.bind(rmp_serde::to_vec(&record).map_err(SqlxStoreError::Encode)?)
.bind(convert_expiry_date(record.expiry_date))
.bind(record.expiry_date)
.execute(conn)
.await
.map_err(SqlxStoreError::Sqlx)?;
Expand Down Expand Up @@ -204,7 +205,7 @@ impl SessionStore for MySqlStore {
);
let data: Option<(Vec<u8>,)> = sqlx::query_as(&query)
.bind(session_id.to_string())
.bind(current_time())
.bind(OffsetDateTime::now_utc())
.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,11 +1,13 @@
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 @@ -155,11 +157,10 @@ 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(convert_expiry_date(record.expiry_date))
.bind(record.expiry_date)
.execute(conn)
.await
.map_err(SqlxStoreError::Sqlx)?;
Expand Down Expand Up @@ -216,10 +217,9 @@ 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(current_time())
.bind(OffsetDateTime::now_utc())
.fetch_optional(&self.pool)
.await
.map_err(SqlxStoreError::Sqlx)?;
Expand Down
7 changes: 4 additions & 3 deletions sqlx-store/src/sqlite_store.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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::{convert_expiry_date, current_time, SqlxStoreError};
use crate::SqlxStoreError;

/// A SQLite session store.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -100,7 +101,7 @@ impl SqliteStore {
sqlx::query(&query)
.bind(record.id.to_string())
.bind(rmp_serde::to_vec(record).map_err(SqlxStoreError::Encode)?)
.bind(convert_expiry_date(record.expiry_date))
.bind(record.expiry_date)
.execute(conn)
.await
.map_err(SqlxStoreError::Sqlx)?;
Expand Down Expand Up @@ -157,7 +158,7 @@ impl SessionStore for SqliteStore {
);
let data: Option<(Vec<u8>,)> = sqlx::query_as(&query)
.bind(session_id.to_string())
.bind(current_time())
.bind(OffsetDateTime::now_utc())
.fetch_optional(&self.pool)
.await
.map_err(SqlxStoreError::Sqlx)?;
Expand Down
1 change: 0 additions & 1 deletion tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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 3a546a9

Please sign in to comment.