Enhance money history tracking: Implement best-effort creation of money_history table in BaseWorker, EventsWorker, and UndergroundWorker to ensure logging of monetary changes without operation failure.
This commit is contained in:
@@ -239,6 +239,19 @@ impl BaseWorker {
|
|||||||
// can show the change even if the DB-function doesn't write it.
|
// can show the change even if the DB-function doesn't write it.
|
||||||
// We don't want to fail the whole operation if this insert fails,
|
// We don't want to fail the whole operation if this insert fails,
|
||||||
// so log errors and continue.
|
// so log errors and continue.
|
||||||
|
// Ensure money_history table exists (best-effort). If this fails,
|
||||||
|
// we still don't want to abort the money update.
|
||||||
|
let create_sql = r#"
|
||||||
|
CREATE TABLE IF NOT EXISTS falukant_log.money_history (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
user_id INTEGER NOT NULL,
|
||||||
|
change NUMERIC(10,2) NOT NULL,
|
||||||
|
action TEXT,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
|
);
|
||||||
|
"#;
|
||||||
|
let _ = conn.query(create_sql);
|
||||||
|
|
||||||
let history_sql = format!(
|
let history_sql = format!(
|
||||||
"INSERT INTO falukant_log.money_history (user_id, change, action, created_at) VALUES ({uid}, {money}::numeric, '{act}', NOW());",
|
"INSERT INTO falukant_log.money_history (user_id, change, action, created_at) VALUES ({uid}, {money}::numeric, '{act}', NOW());",
|
||||||
uid = uid_i32,
|
uid = uid_i32,
|
||||||
|
|||||||
@@ -1013,6 +1013,17 @@ impl EventsWorker {
|
|||||||
let money_str = format!("{:.2}", change);
|
let money_str = format!("{:.2}", change);
|
||||||
fn escape_sql_literal(s: &str) -> String { s.replace('\'', "''") }
|
fn escape_sql_literal(s: &str) -> String { s.replace('\'', "''") }
|
||||||
let escaped_action = escape_sql_literal(&action);
|
let escaped_action = escape_sql_literal(&action);
|
||||||
|
let create_sql = r#"
|
||||||
|
CREATE TABLE IF NOT EXISTS falukant_log.money_history (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
user_id INTEGER NOT NULL,
|
||||||
|
change NUMERIC(10,2) NOT NULL,
|
||||||
|
action TEXT,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
|
);
|
||||||
|
"#;
|
||||||
|
let _ = conn.query(create_sql);
|
||||||
|
|
||||||
let history_sql = format!(
|
let history_sql = format!(
|
||||||
"INSERT INTO falukant_log.money_history (user_id, change, action, created_at) VALUES ({uid}, {money}::numeric, '{act}', NOW());",
|
"INSERT INTO falukant_log.money_history (user_id, change, action, created_at) VALUES ({uid}, {money}::numeric, '{act}', NOW());",
|
||||||
uid = user_id,
|
uid = user_id,
|
||||||
|
|||||||
@@ -967,6 +967,17 @@ fn change_falukant_user_money(
|
|||||||
let money_str = format!("{:.2}", money_change);
|
let money_str = format!("{:.2}", money_change);
|
||||||
fn escape_sql_literal(s: &str) -> String { s.replace('\'', "''") }
|
fn escape_sql_literal(s: &str) -> String { s.replace('\'', "''") }
|
||||||
let escaped_action = escape_sql_literal(action);
|
let escaped_action = escape_sql_literal(action);
|
||||||
|
let create_sql = r#"
|
||||||
|
CREATE TABLE IF NOT EXISTS falukant_log.money_history (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
user_id INTEGER NOT NULL,
|
||||||
|
change NUMERIC(10,2) NOT NULL,
|
||||||
|
action TEXT,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
|
);
|
||||||
|
"#;
|
||||||
|
let _ = conn.query(create_sql);
|
||||||
|
|
||||||
let history_sql = format!(
|
let history_sql = format!(
|
||||||
"INSERT INTO falukant_log.money_history (user_id, change, action, created_at) VALUES ({uid}, {money}::numeric, '{act}', NOW());",
|
"INSERT INTO falukant_log.money_history (user_id, change, action, created_at) VALUES ({uid}, {money}::numeric, '{act}', NOW());",
|
||||||
uid = falukant_user_id,
|
uid = falukant_user_id,
|
||||||
|
|||||||
Reference in New Issue
Block a user