Skip to content

Commit 34eef56

Browse files
fix(workspace)?: dont hang on db connection errors
1 parent 120625a commit 34eef56

File tree

9 files changed

+206
-179
lines changed

9 files changed

+206
-179
lines changed

crates/pg_lsp/src/handlers/completions.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::session::Session;
22
use anyhow::Result;
3-
use pg_workspace::workspace;
3+
use pg_workspace::{workspace, WorkspaceError};
44
use tower_lsp::lsp_types::{self, CompletionItem, CompletionItemLabelDetails};
55

66
#[tracing::instrument(level = "trace", skip_all)]
@@ -26,12 +26,22 @@ pub fn get_completions(
2626
pg_lsp_converters::negotiated_encoding(client_capabilities),
2727
)?;
2828

29-
let completion_result = session
29+
let completion_result = match session
3030
.workspace
3131
.get_completions(workspace::CompletionParams {
3232
path,
3333
position: offset,
34-
})?;
34+
}) {
35+
Ok(result) => result,
36+
Err(e) => match e {
37+
WorkspaceError::DatabaseConnectionError(_) => {
38+
return Ok(lsp_types::CompletionResponse::Array(vec![]));
39+
}
40+
_ => {
41+
return Err(e.into());
42+
}
43+
},
44+
};
3545

3646
let items: Vec<CompletionItem> = completion_result
3747
.into_iter()

crates/pg_schema_cache/src/schema_cache.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use crate::versions::Version;
99

1010
#[derive(Debug, Clone, Default)]
1111
pub struct SchemaCache {
12+
cached_conn_str: String,
13+
1214
pub schemas: Vec<Schema>,
1315
pub tables: Vec<Table>,
1416
pub functions: Vec<Function>,
@@ -29,6 +31,7 @@ impl SchemaCache {
2931
)?;
3032

3133
Ok(SchemaCache {
34+
cached_conn_str: SchemaCache::pool_to_conn_str(pool),
3235
schemas,
3336
tables,
3437
functions,
@@ -38,6 +41,22 @@ impl SchemaCache {
3841
})
3942
}
4043

44+
fn pool_to_conn_str(pool: &PgPool) -> String {
45+
let conn = pool.connect_options();
46+
47+
format!(
48+
"postgres://{}:<redacted_pw>@{}:{}/{}",
49+
conn.get_username(),
50+
conn.get_host(),
51+
conn.get_port(),
52+
conn.get_database().unwrap_or("default")
53+
)
54+
}
55+
56+
pub fn has_already_cached_connection(&self, pool: &PgPool) -> bool {
57+
self.cached_conn_str == SchemaCache::pool_to_conn_str(pool)
58+
}
59+
4160
/// Applies an AST node to the repository
4261
///
4362
/// For example, alter table add column will add the column to the table if it does not exist

crates/pg_workspace/src/workspace.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ pub trait Workspace: Send + Sync + RefUnwindSafe {
119119
params: CompletionParams,
120120
) -> Result<pg_completions::CompletionResult, WorkspaceError>;
121121

122-
/// Refresh the schema cache for this workspace
123-
fn refresh_schema_cache(&self) -> Result<(), WorkspaceError>;
124-
125122
/// Update the global settings for this workspace
126123
fn update_settings(&self, params: UpdateSettingsParams) -> Result<(), WorkspaceError>;
127124

crates/pg_workspace/src/workspace/client.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,6 @@ where
117117
self.request("pglsp/get_file_content", params)
118118
}
119119

120-
fn refresh_schema_cache(&self) -> Result<(), WorkspaceError> {
121-
self.request("pglsp/refresh_schema_cache", ())
122-
}
123-
124120
fn pull_diagnostics(
125121
&self,
126122
params: super::PullDiagnosticsParams,

0 commit comments

Comments
 (0)