From 69bb5952ab665f6edfb461b45e63cc3b6d99a4d0 Mon Sep 17 00:00:00 2001 From: Sean Lynch <42618346+swlynch99@users.noreply.github.com> Date: Thu, 28 Aug 2025 20:37:49 -0700 Subject: [PATCH] Drop cached db connections in macros upon hitting an error (#4009) Once a connection to the database is lost all future macro evaluations will fail. This is fine for normal compilation since it tends to be short but causes issues with rust-analyzer since it keeps the macro binaries loaded for a long time. This commit changes the macro implementation to drop the cached connection when it encounters an IO or protocol error. In practice these seem to be the errors that show up when the connection is lost and dumping the connection on every error would have unnecessary overhead. --- sqlx-macros-core/src/database/mod.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/sqlx-macros-core/src/database/mod.rs b/sqlx-macros-core/src/database/mod.rs index c247af06..0885b3cc 100644 --- a/sqlx-macros-core/src/database/mod.rs +++ b/sqlx-macros-core/src/database/mod.rs @@ -88,8 +88,19 @@ impl CachingDescribeBlocking { } }; - conn.describe(AssertSqlSafe(query.to_string()).into_sql_str()) + match conn + .describe(AssertSqlSafe(query.to_string()).into_sql_str()) .await + { + Ok(describe) => Ok(describe), + Err(e) => { + if matches!(e, sqlx_core::Error::Io(_) | sqlx_core::Error::Protocol(_)) { + cache.remove(database_url); + } + + Err(e) + } + } }) } }