Skip to content

Commit

Permalink
Upgrade all packages, refactor sqlx
Browse files Browse the repository at this point in the history
  • Loading branch information
Jupeyy committed Nov 30, 2024
1 parent 9ad7c1e commit 106c7ab
Show file tree
Hide file tree
Showing 46 changed files with 1,344 additions and 1,045 deletions.
782 changes: 377 additions & 405 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ ddnet-account-sql = { version = "0.2.0", path = "lib/ddnet-account-sql", feature
tokio = { version = "1.41.1", features = ["rt-multi-thread", "sync", "fs", "time", "macros"] }
axum = "0.7.9"
anyhow = { version = "1.0.93", features = ["backtrace"] }
# https://github.com/launchbadge/sqlx/issues/2636
sqlx = { version = "=0.6.3", features = ["mysql", "any", "runtime-tokio-rustls", "chrono"] }
sqlx = { version = "0.8.2", features = ["mysql", "any", "runtime-tokio-rustls", "chrono"] }
email_address = { version = "0.2.9", features = ["serde"] }
ed25519-dalek = { version = "2.1.1", features = ["serde", "pkcs8"] }
async-trait = "0.1.83"
Expand All @@ -53,14 +52,14 @@ sha2 = { version = "0.10", features = ["oid"] }
der = { version = "0.7.9", features = ["derive"] }
chrono = { version = "0.4.38", features = ["serde"] }
tower_governor = "0.4.3"
tower = "0.4.13"
tower = "0.5.1"
strum = { version = "0.26.3", features = ["derive"] }
reqwest = { version = "0.12.9" }
hex = "0.4.3"
iprange = "0.6.7"
ipnet = "2.10.1"
either = "1.13.0"
notify = { version = "6.1.1", default-features = false, features = ["macos_kqueue"] }
notify = { version = "7.0.0", default-features = false, features = ["macos_kqueue"] }

[dev-dependencies]
ddnet-account-client = { version = "0.1.0", path = "lib/ddnet-account-client" }
Expand Down
4 changes: 2 additions & 2 deletions lib/ddnet-account-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ async-trait = "0.1.83"
email_address = { version = "0.2.9", features = ["serde"] }
serde_json = "1.0.133"
anyhow = { version = "1.0.93", features = ["backtrace"] }
thiserror = "1.0.69"
thiserror = "2.0.3"
serde = { version = "1.0.215", features = ["derive"] }
x509-parser = { version = "0.16.0", default-features = false }
x509-cert = { version = "0.2.5" }
hex = "0.4.3"

[dev-dependencies]
pollster = "0.3.0"
pollster = "0.4.0"
5 changes: 2 additions & 3 deletions lib/ddnet-account-game-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ ddnet-accounts-types = { version = "0.1.0", path = "../ddnet-accounts-types" }
ddnet-accounts-shared = { version = "0.1.0", path = "../ddnet-accounts-shared" }
ddnet-account-sql = { version = "0.2.0", path = "../ddnet-account-sql", default-features = false }

# https://github.com/launchbadge/sqlx/issues/2636
sqlx = { version = "=0.6.3", features = ["any", "runtime-tokio-rustls", "chrono"] }
sqlx = { version = "0.8.2", features = ["any", "runtime-tokio-rustls", "chrono"] }
anyhow = { version = "1.0.93", features = ["backtrace"] }
async-trait = "0.1.83"
thiserror = "1.0.69"
thiserror = "2.0.3"

[dev-dependencies]
tokio = { version = "1.41.1", features = ["rt-multi-thread", "sync", "fs", "time", "macros"] }
Expand Down
11 changes: 5 additions & 6 deletions lib/ddnet-account-game-server/src/auto_login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ pub(crate) mod queries;

use std::sync::Arc;

use ddnet_account_sql::query::Query;
use ddnet_account_sql::{any::AnyPool, query::Query};
use ddnet_accounts_shared::game_server::user_id::UserId;
use ddnet_accounts_types::account_id::AccountId;
use sqlx::Acquire;
use thiserror::Error;

use crate::shared::Shared;
Expand Down Expand Up @@ -42,15 +41,15 @@ pub fn default_name(account_id: &AccountId) -> String {
/// information to the account now.
pub async fn auto_login(
shared: Arc<Shared>,
pool: &sqlx::AnyPool,
pool: &AnyPool,
user_id: &UserId,
) -> anyhow::Result<bool, AutoLoginError> {
if let Some(account_id) = &user_id.account_id {
let mut pool_con = pool
.acquire()
.await
.map_err(|err| AutoLoginError::Database(err.into()))?;
let con = pool_con
let mut con = pool_con
.acquire()
.await
.map_err(|err| AutoLoginError::Database(err.into()))?;
Expand All @@ -62,8 +61,8 @@ pub async fn auto_login(
};

let res = qry
.query(con, &shared.db.register_user_statement)
.execute(&mut *con)
.query(&shared.db.register_user_statement)
.execute(&mut con)
.await
.map_err(|err| AutoLoginError::Database(err.into()))?;

Expand Down
27 changes: 17 additions & 10 deletions lib/ddnet-account-game-server/src/auto_login/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use anyhow::anyhow;
use async_trait::async_trait;
use ddnet_account_sql::query::Query;
use ddnet_accounts_types::account_id::AccountId;
use sqlx::any::AnyRow;
use sqlx::Executor;
use sqlx::Statement;

Expand All @@ -20,39 +19,47 @@ pub struct RegisterUser<'a> {
impl Query<()> for RegisterUser<'_> {
#[cfg(feature = "mysql")]
async fn prepare_mysql(
connection: &mut sqlx::AnyConnection,
) -> anyhow::Result<sqlx::any::AnyStatement<'static>> {
connection: &mut sqlx::mysql::MySqlConnection,
) -> anyhow::Result<sqlx::mysql::MySqlStatement<'static>> {
Ok(connection
.prepare(include_str!("mysql/try_insert_user.sql"))
.await?)
}
#[cfg(feature = "sqlite")]
async fn prepare_sqlite(
connection: &mut sqlx::AnyConnection,
) -> anyhow::Result<sqlx::any::AnyStatement<'static>> {
connection: &mut sqlx::sqlite::SqliteConnection,
) -> anyhow::Result<sqlx::sqlite::SqliteStatement<'static>> {
Ok(connection
.prepare(include_str!("sqlite/try_insert_user.sql"))
.await?)
}
#[cfg(feature = "mysql")]
fn query_mysql<'b>(
&'b self,
statement: &'b sqlx::any::AnyStatement<'static>,
) -> sqlx::query::Query<'b, sqlx::Any, sqlx::any::AnyArguments<'b>> {
statement: &'b sqlx::mysql::MySqlStatement<'static>,
) -> sqlx::query::Query<'b, sqlx::MySql, sqlx::mysql::MySqlArguments> {
let account_id = self.account_id;

statement.query().bind(self.default_name).bind(account_id)
}
#[cfg(feature = "sqlite")]
fn query_sqlite<'b>(
&'b self,
statement: &'b sqlx::any::AnyStatement<'static>,
) -> sqlx::query::Query<'b, sqlx::Any, sqlx::any::AnyArguments<'b>> {
statement: &'b sqlx::sqlite::SqliteStatement<'static>,
) -> sqlx::query::Query<'b, sqlx::Sqlite, sqlx::sqlite::SqliteArguments<'b>> {
let account_id = self.account_id;

statement.query().bind(self.default_name).bind(account_id)
}
fn row_data(_row: &AnyRow) -> anyhow::Result<()> {
#[cfg(feature = "mysql")]
fn row_data_mysql(_row: &sqlx::mysql::MySqlRow) -> anyhow::Result<()> {
Err(anyhow!(
"Data rows are not supported for this query.
You probably want to check affected rows instead."
))
}
#[cfg(feature = "sqlite")]
fn row_data_sqlite(_row: &sqlx::sqlite::SqliteRow) -> anyhow::Result<()> {
Err(anyhow!(
"Data rows are not supported for this query.
You probably want to check affected rows instead."
Expand Down
2 changes: 1 addition & 1 deletion lib/ddnet-account-game-server/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sqlx::any::AnyStatement;
use ddnet_account_sql::any::AnyStatement;

/// Shared data for a db connection
pub struct DbConnectionShared {
Expand Down
13 changes: 6 additions & 7 deletions lib/ddnet-account-game-server/src/prepare.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
use std::sync::Arc;

use ddnet_account_sql::query::Query;
use sqlx::Acquire;
use ddnet_account_sql::{any::AnyPool, query::Query};

use crate::{
auto_login::queries::RegisterUser, db::DbConnectionShared, rename::queries::RenameUser,
shared::Shared,
};

async fn prepare_statements(pool: &sqlx::AnyPool) -> anyhow::Result<DbConnectionShared> {
async fn prepare_statements(pool: &AnyPool) -> anyhow::Result<DbConnectionShared> {
let mut pool_con = pool.acquire().await?;
let con = pool_con.acquire().await?;
let mut con = pool_con.acquire().await?;

Ok(DbConnectionShared {
register_user_statement: RegisterUser::prepare(con).await?,
try_rename_statement: RenameUser::prepare(con).await?,
register_user_statement: RegisterUser::prepare(&mut con).await?,
try_rename_statement: RenameUser::prepare(&mut con).await?,
})
}

/// Prepare shared data used in the game server's implementation
pub async fn prepare(pool: &sqlx::AnyPool) -> anyhow::Result<Arc<Shared>> {
pub async fn prepare(pool: &AnyPool) -> anyhow::Result<Arc<Shared>> {
Ok(Arc::new(Shared {
db: prepare_statements(pool).await?,
}))
Expand Down
11 changes: 5 additions & 6 deletions lib/ddnet-account-game-server/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ pub(crate) mod queries;

use std::sync::Arc;

use ddnet_account_sql::{is_duplicate_entry, query::Query};
use ddnet_account_sql::{any::AnyPool, is_duplicate_entry, query::Query};
use ddnet_accounts_shared::game_server::user_id::UserId;
use sqlx::Acquire;
use thiserror::Error;

use crate::{
Expand Down Expand Up @@ -39,7 +38,7 @@ pub enum RenameError {
/// Returns `false` if the user had no account.
pub async fn rename(
shared: Arc<Shared>,
pool: &sqlx::AnyPool,
pool: &AnyPool,
user_id: &UserId,
name: &str,
) -> anyhow::Result<bool, RenameError> {
Expand All @@ -65,16 +64,16 @@ pub async fn rename(
.acquire()
.await
.map_err(|err| RenameError::Database(err.into()))?;
let con = pool_con
let mut con = pool_con
.acquire()
.await
.map_err(|err| RenameError::Database(err.into()))?;

let qry = RenameUser { account_id, name };

let res = qry
.query(con, &shared.db.try_rename_statement)
.execute(&mut *con)
.query(&shared.db.try_rename_statement)
.execute(&mut con)
.await;

if is_duplicate_entry(&res) {
Expand Down
27 changes: 17 additions & 10 deletions lib/ddnet-account-game-server/src/rename/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use anyhow::anyhow;
use async_trait::async_trait;
use ddnet_account_sql::query::Query;
use ddnet_accounts_types::account_id::AccountId;
use sqlx::any::AnyRow;
use sqlx::Executor;
use sqlx::Statement;

Expand All @@ -20,39 +19,47 @@ pub struct RenameUser<'a> {
impl Query<()> for RenameUser<'_> {
#[cfg(feature = "mysql")]
async fn prepare_mysql(
connection: &mut sqlx::AnyConnection,
) -> anyhow::Result<sqlx::any::AnyStatement<'static>> {
connection: &mut sqlx::mysql::MySqlConnection,
) -> anyhow::Result<sqlx::mysql::MySqlStatement<'static>> {
Ok(connection
.prepare(include_str!("mysql/try_rename.sql"))
.await?)
}
#[cfg(feature = "sqlite")]
async fn prepare_sqlite(
connection: &mut sqlx::AnyConnection,
) -> anyhow::Result<sqlx::any::AnyStatement<'static>> {
connection: &mut sqlx::sqlite::SqliteConnection,
) -> anyhow::Result<sqlx::sqlite::SqliteStatement<'static>> {
Ok(connection
.prepare(include_str!("sqlite/try_rename.sql"))
.await?)
}
#[cfg(feature = "mysql")]
fn query_mysql<'b>(
&'b self,
statement: &'b sqlx::any::AnyStatement<'static>,
) -> sqlx::query::Query<'b, sqlx::Any, sqlx::any::AnyArguments<'b>> {
statement: &'b sqlx::mysql::MySqlStatement<'static>,
) -> sqlx::query::Query<'b, sqlx::MySql, sqlx::mysql::MySqlArguments> {
let account_id = self.account_id;

statement.query().bind(self.name).bind(account_id)
}
#[cfg(feature = "sqlite")]
fn query_sqlite<'b>(
&'b self,
statement: &'b sqlx::any::AnyStatement<'static>,
) -> sqlx::query::Query<'b, sqlx::Any, sqlx::any::AnyArguments<'b>> {
statement: &'b sqlx::sqlite::SqliteStatement<'static>,
) -> sqlx::query::Query<'b, sqlx::Sqlite, sqlx::sqlite::SqliteArguments<'b>> {
let account_id = self.account_id;

statement.query().bind(self.name).bind(account_id)
}
fn row_data(_row: &AnyRow) -> anyhow::Result<()> {
#[cfg(feature = "mysql")]
fn row_data_mysql(_row: &sqlx::mysql::MySqlRow) -> anyhow::Result<()> {
Err(anyhow!(
"Data rows are not supported for this query.
You probably want to check affected rows instead."
))
}
#[cfg(feature = "sqlite")]
fn row_data_sqlite(_row: &sqlx::sqlite::SqliteRow) -> anyhow::Result<()> {
Err(anyhow!(
"Data rows are not supported for this query.
You probably want to check affected rows instead."
Expand Down
Loading

0 comments on commit 106c7ab

Please sign in to comment.