Skip to content

Commit e967648

Browse files
committed
Drop cached db connections in macros upon hitting an error
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.
1 parent e6e8fc7 commit e967648

File tree

1 file changed

+10
-2
lines changed
  • sqlx-macros-core/src/database

1 file changed

+10
-2
lines changed

sqlx-macros-core/src/database/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,16 @@ impl<DB: DatabaseExt> CachingDescribeBlocking<DB> {
8888
}
8989
};
9090

91-
conn.describe(AssertSqlSafe(query.to_string()).into_sql_str())
92-
.await
91+
match conn.describe(AssertSqlSafe(query.to_string()).into_sql_str()).await {
92+
Ok(describe) => Ok(describe),
93+
Err(e) => {
94+
if matches!(e, sqlx_core::Error::Io(_) | sqlx_core::Error::Protocol(_)) {
95+
cache.remove(database_url);
96+
}
97+
98+
Err(e)
99+
}
100+
}
93101
})
94102
}
95103
}

0 commit comments

Comments
 (0)