{"record":{"id":"a19bdccf0c77d873","repo":"nautechsystems/nautilus_trader","slug":"failed-to-check-if-account-event-exists-e","errorCode":null,"errorMessage":"Failed to check if account event exists: {e}","messagePattern":"Failed to check if account event exists: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":608,"sourceCode":"    /// Checks if any account event exists for the given `account_id` via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SQL SELECT operation fails.\n    pub async fn check_if_account_event_exists(\n        pool: &PgPool,\n        account_id: AccountId,\n    ) -> anyhow::Result<bool> {\n        sqlx::query(\n            r#\"\n            SELECT EXISTS(SELECT 1 FROM \"account_event\" WHERE account_id = $1)\n        \"#,\n        )\n        .bind(account_id.to_string())\n        .fetch_one(pool)\n        .await\n        .map(|row| row.get(0))\n        .map_err(|e| anyhow::anyhow!(\"Failed to check if account event exists: {e}\"))\n    }\n\n    /// Inserts or updates an order event entry via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SQL INSERT or UPDATE operation fails, or if\n    /// serialization of `exec_algorithm_params` fails.\n    #[expect(\n        clippy::too_many_lines,\n        reason = \"order event persistence maps the full database schema in one transaction\"\n    )]\n    pub async fn add_order_event(\n        pool: &PgPool,\n        order_event: Box<dyn OrderEvent>,\n        client_id: Option<ClientId>,\n    ) -> anyhow::Result<()> {\n        let mut transaction = pool.begin().await?;","sourceCodeStart":590,"sourceCodeEnd":626,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L590-L626","documentation":"Wraps sqlx failures from the query that checks whether any account event row exists for a given `account_id`. Like the other existence checks, it only errors when the SQL execution fails, not when no events exist (that yields `Ok(false)`). The driver error is wrapped in anyhow context for diagnostics.","triggerScenarios":"Calling `check_if_account_event_exists(pool, account_id)` when the database is unreachable, the account-event table is missing or renamed, or the account_id string does not match the column type.","commonSituations":"Migrations not applied on a fresh environment; account_id UUID formatting mismatch; connection dropped by the server due to idle timeout; wrong database credentials.","solutions":["Confirm connectivity with `SELECT 1` on the same pool.","Verify the account event table and its `account_id` column exist (apply migrations).","Check `account_id.to_string()` matches the stored format.","Inspect `{e}` for the driver-level cause; retry transient failures."],"exampleFix":"// before\nlet has = check_if_account_event_exists(&pool, account_id).await?;\n// after\nlet has = check_if_account_event_exists(&pool, account_id).await\n    .map_err(|e| { tracing::error!(\"account event check failed: {e:#}\"); e })?;","handlingStrategy":"try-catch","validationCode":"sqlx::query(\"SELECT 1\").execute(pool).await\n    .map_err(|e| anyhow::anyhow!(\"db unreachable: {e}\"))?;","typeGuard":"fn is_transient_db_error(e: &anyhow::Error) -> bool {\n    let s = e.to_string().to_lowercase();\n    s.contains(\"connection\") || s.contains(\"timeout\") || s.contains(\"broken pipe\")\n}","tryCatchPattern":"match check_if_account_event_exists(&pool, account_id).await {\n    Ok(has_events) => { /* bool result */ }\n    Err(e) if is_transient_db_error(&e) => { /* retry with backoff */ }\n    Err(e) => return Err(e),\n}","preventionTips":["Health-check connectivity before reconciliation reads.","Standardize account_id string format (UUID canonical form) before querying.","Keep migrations automated per environment."],"tags":["database","sqlx","postgres","existence-check"],"backgroundTag":"database-query-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}