Avoid updating users when the user id itself changed

This commit is contained in:
Natsu Kagami 2024-08-04 21:23:35 +02:00 committed by Natsu Kagami
parent fad84b9420
commit 5c523009e1
5 changed files with 73 additions and 22 deletions

View file

@ -0,0 +1,20 @@
{
"db_name": "SQLite",
"query": "SELECT id as \"id: i64\" FROM osu_users WHERE user_id = ?",
"describe": {
"columns": [
{
"name": "id: i64",
"ordinal": 0,
"type_info": "Int64"
}
],
"parameters": {
"Right": 1
},
"nullable": [
false
]
},
"hash": "9c620436cfe463780fca2dc5497c3b787fa284a74b3e3d491755c88a693810e0"
}

View file

@ -182,7 +182,22 @@ impl OsuUser {
impl OsuUser {
/// Stores the user.
pub async fn store<'a>(&self, conn: &mut Transaction<'a, Database>) -> Result<()> {
pub async fn store<'a>(&self, conn: &mut Transaction<'a, Database>) -> Result<bool> {
let old_user_id = {
query!(
r#"SELECT id as "id: i64" FROM osu_users WHERE user_id = ?"#,
self.user_id
)
.fetch_optional(&mut **conn)
.await?
.map(|v| v.id)
};
if old_user_id.is_some_and(|v| v != self.id) {
// There's another update that changed the user_id
return Ok(false);
}
query!(
r#"INSERT
INTO osu_users(user_id, username, id, failures)
@ -221,7 +236,7 @@ impl OsuUser {
.execute(&mut **conn)
.await?;
}
Ok(())
Ok(true)
}
pub async fn delete(user_id: i64, conn: impl Executor<'_, Database = Database>) -> Result<()> {