update to sqlx 0.7, using personal fork of axum-login

This commit is contained in:
Joe Ardent 2023-07-17 14:56:18 -07:00
parent 359a732a84
commit c824a2b07a
4 changed files with 641 additions and 310 deletions

892
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -14,28 +14,28 @@ askama = { version = "0.12", features = ["with-axum"] }
askama_axum = "0.3" askama_axum = "0.3"
async-session = "3" async-session = "3"
axum = { version = "0.6", features = ["macros", "headers"] } axum = { version = "0.6", features = ["macros", "headers"] }
axum-login = { version = "0.5", features = ["sqlite", "sqlx"] } axum-login = { git = "https://github.com/nebkor/axum-login", branch = "sqlx-0.7", features = ["sqlite"], default-features = false }
axum-macros = "0.3" axum-macros = "0.3"
chrono = { version = "0.4", default-features = false, features = ["std", "clock"] } chrono = { version = "0.4", default-features = false, features = ["std", "clock"] }
clap = { version = "4.3.10", features = ["derive", "env", "unicode", "suggestions", "usage"] } clap = { version = "4", features = ["derive", "env", "unicode", "suggestions", "usage"] }
justerror = "1" justerror = "1"
password-hash = { version = "0.5", features = ["std", "getrandom"] } password-hash = { version = "0.5", features = ["std", "getrandom"] }
rand = "0.8" rand = "0.8"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
sqlx = { version = "0.6", default-features = false, features = ["runtime-tokio-rustls", "any", "sqlite", "chrono", "time"] } sqlx = { version = "0.7", default-features = false, features = ["runtime-tokio-rustls", "sqlite", "chrono", "time"] }
thiserror = "1" thiserror = "1"
tokio = { version = "1", features = ["full", "tracing"], default-features = false } tokio = { version = "1", features = ["full", "tracing"], default-features = false }
tokio-retry = "0.3.0" tokio-retry = "0.3"
tokio-stream = "0.1.14" tokio-stream = "0.1"
tower = { version = "0.4", features = ["util", "timeout"], default-features = false } tower = { version = "0.4", features = ["util", "timeout"], default-features = false }
tower-http = { version = "0.4", features = ["add-extension", "trace"] } tower-http = { version = "0.4", features = ["add-extension", "trace"] }
tracing = "0.1" tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] } tracing-subscriber = { version = "0.3", features = ["env-filter"] }
ulid = { version = "1", features = ["rand"] } ulid = { version = "1", features = ["rand"] }
unicode-segmentation = "1" unicode-segmentation = "1"
rand_distr = "0.4.3" rand_distr = "0.4"
[dev-dependencies] [dev-dependencies]
axum-test = "9.0.0" axum-test = "9"
serde_test = "1.0.164" serde_test = "1"

View file

@ -55,15 +55,10 @@ pub fn get_db_pool() -> SqlitePool {
let pool = SqlitePoolOptions::new() let pool = SqlitePoolOptions::new()
.max_connections(MAX_CONNS) .max_connections(MAX_CONNS)
.min_connections(MIN_CONNS) .min_connections(MIN_CONNS)
.idle_timeout(Some(Duration::from_secs(30))) .idle_timeout(Some(Duration::from_secs(3)))
.max_lifetime(Some(Duration::from_secs(3600))) .max_lifetime(Some(Duration::from_secs(3600)))
.connect_with(conn_opts); .connect_with(conn_opts);
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
let pool = { let pool = {
let rt = tokio::runtime::Builder::new_multi_thread() let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all() .enable_all()
@ -73,8 +68,6 @@ pub fn get_db_pool() -> SqlitePool {
rt.block_on(pool).unwrap() rt.block_on(pool).unwrap()
}; };
let _con = rt.block_on(pool.acquire()).unwrap();
{ {
let rt = tokio::runtime::Builder::new_multi_thread() let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all() .enable_all()
@ -250,8 +243,6 @@ mod session_store {
/// on breaking releases. /// on breaking releases.
pub async fn migrate(&self) -> sqlx::Result<()> { pub async fn migrate(&self) -> sqlx::Result<()> {
log::info!("migrating sessions on `{}`", self.table_name); log::info!("migrating sessions on `{}`", self.table_name);
let mut conn = self.client.acquire().await?;
sqlx::query(&self.substitute_table_name( sqlx::query(&self.substitute_table_name(
r#" r#"
CREATE TABLE IF NOT EXISTS %%TABLE_NAME%% ( CREATE TABLE IF NOT EXISTS %%TABLE_NAME%% (
@ -261,7 +252,7 @@ mod session_store {
) )
"#, "#,
)) ))
.execute(&mut conn) .execute(&self.client)
.await?; .await?;
Ok(()) Ok(())
} }
@ -280,7 +271,6 @@ mod session_store {
/// Performs a one-time cleanup task that clears out stale /// Performs a one-time cleanup task that clears out stale
/// (expired) sessions. You may want to call this from cron. /// (expired) sessions. You may want to call this from cron.
pub async fn cleanup(&self) -> sqlx::Result<()> { pub async fn cleanup(&self) -> sqlx::Result<()> {
let mut connection = self.connection().await?;
sqlx::query(&self.substitute_table_name( sqlx::query(&self.substitute_table_name(
r#" r#"
DELETE FROM %%TABLE_NAME%% DELETE FROM %%TABLE_NAME%%
@ -288,7 +278,7 @@ mod session_store {
"#, "#,
)) ))
.bind(Utc::now().timestamp()) .bind(Utc::now().timestamp())
.execute(&mut connection) .execute(&self.client)
.await?; .await?;
Ok(()) Ok(())
@ -299,7 +289,7 @@ mod session_store {
pub async fn count(&self) -> sqlx::Result<i32> { pub async fn count(&self) -> sqlx::Result<i32> {
let (count,) = let (count,) =
sqlx::query_as(&self.substitute_table_name("SELECT COUNT(*) FROM %%TABLE_NAME%%")) sqlx::query_as(&self.substitute_table_name("SELECT COUNT(*) FROM %%TABLE_NAME%%"))
.fetch_one(&mut self.connection().await?) .fetch_one(&self.client)
.await?; .await?;
Ok(count) Ok(count)
@ -310,8 +300,6 @@ mod session_store {
impl SessionStore for SqliteSessionStore { impl SessionStore for SqliteSessionStore {
async fn load_session(&self, cookie_value: String) -> Result<Option<Session>> { async fn load_session(&self, cookie_value: String) -> Result<Option<Session>> {
let id = Session::id_from_cookie_value(&cookie_value)?; let id = Session::id_from_cookie_value(&cookie_value)?;
let mut connection = self.connection().await?;
let result: Option<(String,)> = sqlx::query_as(&self.substitute_table_name( let result: Option<(String,)> = sqlx::query_as(&self.substitute_table_name(
r#" r#"
SELECT session FROM %%TABLE_NAME%% SELECT session FROM %%TABLE_NAME%%
@ -320,7 +308,7 @@ mod session_store {
)) ))
.bind(&id) .bind(&id)
.bind(Utc::now().timestamp()) .bind(Utc::now().timestamp())
.fetch_optional(&mut connection) .fetch_optional(&self.client)
.await?; .await?;
Ok(result Ok(result
@ -331,7 +319,6 @@ mod session_store {
async fn store_session(&self, session: Session) -> Result<Option<String>> { async fn store_session(&self, session: Session) -> Result<Option<String>> {
let id = session.id(); let id = session.id();
let string = serde_json::to_string(&session)?; let string = serde_json::to_string(&session)?;
let mut connection = self.connection().await?;
sqlx::query(&self.substitute_table_name( sqlx::query(&self.substitute_table_name(
r#" r#"
@ -345,7 +332,7 @@ mod session_store {
.bind(id) .bind(id)
.bind(&string) .bind(&string)
.bind(session.expiry().map(|expiry| expiry.timestamp())) .bind(session.expiry().map(|expiry| expiry.timestamp()))
.execute(&mut connection) .execute(&self.client)
.await?; .await?;
Ok(session.into_cookie_value()) Ok(session.into_cookie_value())
@ -353,27 +340,25 @@ mod session_store {
async fn destroy_session(&self, session: Session) -> Result { async fn destroy_session(&self, session: Session) -> Result {
let id = session.id(); let id = session.id();
let mut connection = self.connection().await?;
sqlx::query(&self.substitute_table_name( sqlx::query(&self.substitute_table_name(
r#" r#"
DELETE FROM %%TABLE_NAME%% WHERE id = ? DELETE FROM %%TABLE_NAME%% WHERE id = ?
"#, "#,
)) ))
.bind(id) .bind(id)
.execute(&mut connection) .execute(&self.client)
.await?; .await?;
Ok(()) Ok(())
} }
async fn clear_store(&self) -> Result { async fn clear_store(&self) -> Result {
let mut connection = self.connection().await?;
sqlx::query(&self.substitute_table_name( sqlx::query(&self.substitute_table_name(
r#" r#"
DELETE FROM %%TABLE_NAME%% DELETE FROM %%TABLE_NAME%%
"#, "#,
)) ))
.execute(&mut connection) .execute(&self.client)
.await?; .await?;
Ok(()) Ok(())
@ -408,7 +393,7 @@ mod session_store {
let (id, expires, serialized, count): (String, Option<i64>, String, i64) = let (id, expires, serialized, count): (String, Option<i64>, String, i64) =
sqlx::query_as("select id, expires, session, count(*) from async_sessions") sqlx::query_as("select id, expires, session, count(*) from async_sessions")
.fetch_one(&mut store.connection().await?) .fetch_one(&store.client)
.await?; .await?;
assert_eq!(1, count); assert_eq!(1, count);
@ -446,7 +431,7 @@ mod session_store {
let (id, count): (String, i64) = let (id, count): (String, i64) =
sqlx::query_as("select id, count(*) from async_sessions") sqlx::query_as("select id, count(*) from async_sessions")
.fetch_one(&mut store.connection().await?) .fetch_one(&store.client)
.await?; .await?;
assert_eq!(1, count); assert_eq!(1, count);
@ -476,7 +461,7 @@ mod session_store {
let (id, expires, count): (String, i64, i64) = let (id, expires, count): (String, i64, i64) =
sqlx::query_as("select id, expires, count(*) from async_sessions") sqlx::query_as("select id, expires, count(*) from async_sessions")
.fetch_one(&mut store.connection().await?) .fetch_one(&store.client)
.await?; .await?;
assert_eq!(1, count); assert_eq!(1, count);
@ -498,7 +483,7 @@ mod session_store {
let (id, expires, serialized, count): (String, Option<i64>, String, i64) = let (id, expires, serialized, count): (String, Option<i64>, String, i64) =
sqlx::query_as("select id, expires, session, count(*) from async_sessions") sqlx::query_as("select id, expires, session, count(*) from async_sessions")
.fetch_one(&mut store.connection().await?) .fetch_one(&store.client)
.await?; .await?;
assert_eq!(1, count); assert_eq!(1, count);

View file

@ -162,7 +162,7 @@ pub(crate) async fn add_new_watch_impl(
.bind(watch.release_date) .bind(watch.release_date)
.bind(&watch.metadata_url) .bind(&watch.metadata_url)
.bind(watch.added_by) .bind(watch.added_by)
.execute(&mut tx) .execute(&mut *tx)
.await .await
.map_err(|err| { .map_err(|err| {
tracing::error!("Got error: {err}"); tracing::error!("Got error: {err}");
@ -175,7 +175,7 @@ pub(crate) async fn add_new_watch_impl(
.bind(quest.watch) .bind(quest.watch)
.bind(quest.is_public) .bind(quest.is_public)
.bind(quest.already_watched) .bind(quest.already_watched)
.execute(&mut tx) .execute(&mut *tx)
.await .await
.map_err(|err| { .map_err(|err| {
tracing::error!("Got error: {err}"); tracing::error!("Got error: {err}");