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.
This commit is contained in:
Sean Lynch 2025-08-28 20:37:49 -07:00 committed by GitHub
parent 3abb186324
commit 69bb5952ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -88,8 +88,19 @@ impl<DB: DatabaseExt> CachingDescribeBlocking<DB> {
}
};
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)
}
}
})
}
}