Update user money handling to prevent serialization errors: Adjusted the SQL query to cast money as numeric and modified the UserCharacterWorker to clamp and format the money value before executing the update. This change ensures valid numeric input and enhances error handling for user transactions.

This commit is contained in:
Torsten Schulz (local)
2026-02-02 15:29:53 +01:00
parent 658a9034ed
commit 85b4c73c11
4 changed files with 23 additions and 4 deletions

View File

@@ -1171,7 +1171,7 @@ pub const QUERY_RANDOM_HEIR: &str = r#"
pub const QUERY_UPDATE_USER_MONEY: &str = r#"
UPDATE falukant_data.falukant_user
SET money = $1,
SET money = $1::numeric,
updated_at = NOW()
WHERE id = $2;
"#;

View File

@@ -698,8 +698,16 @@ impl UserCharacterWorker {
.get()
.map_err(|e| DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?;
let clamped = if !new_amount.is_finite() {
1000.0
} else {
const MAX_ABS: f64 = 99_999_999.99;
new_amount.clamp(-MAX_ABS, MAX_ABS)
};
let money_str = format!("{:.2}", clamped);
conn.prepare("update_user_money", QUERY_UPDATE_USER_MONEY)?;
conn.execute("update_user_money", &[&new_amount, &falukant_user_id])?;
conn.execute("update_user_money", &[&money_str, &falukant_user_id])?;
Ok(())
}

View File

@@ -1168,9 +1168,11 @@ pub const QUERY_RANDOM_HEIR: &str = r#"
chosen.child_character_id;
"#;
/// Aktualisiert den Geldstand eines Users. $1 = Geld als Text (z. B. "1234.56"), wird als numeric gecastet; $2 = user id.
/// Verwendung von Text-Parameter vermeidet "error serializing parameter 0" bei f64/numeric.
pub const QUERY_UPDATE_USER_MONEY: &str = r#"
UPDATE falukant_data.falukant_user
SET money = $1,
SET money = $1::numeric,
updated_at = NOW()
WHERE id = $2;
"#;

View File

@@ -747,8 +747,17 @@ impl UserCharacterWorker {
.get()
.map_err(|e| DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?;
// Endliche Werte und gültiger Bereich für numeric(10,2), sonst Serialisierungsfehler
let clamped = if !new_amount.is_finite() {
1000.0
} else {
const MAX_ABS: f64 = 99_999_999.99;
new_amount.clamp(-MAX_ABS, MAX_ABS)
};
let money_str = format!("{:.2}", clamped);
conn.prepare("update_user_money", QUERY_UPDATE_USER_MONEY)?;
conn.execute("update_user_money", &[&new_amount, &falukant_user_id])?;
conn.execute("update_user_money", &[&money_str, &falukant_user_id])?;
Ok(())
}