Skip to content

Commit 6c67271

Browse files
feat(schema_cache): implement diagnostics
1 parent 99194c9 commit 6c67271

File tree

12 files changed

+84
-22
lines changed

12 files changed

+84
-22
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pg_completions/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ async-std = "1.12.0"
1717
text-size.workspace = true
1818

1919
pg_schema_cache.workspace = true
20-
pg_test_utils.workspace = true
2120
tree-sitter.workspace = true
2221
tree_sitter_sql.workspace = true
2322

2423
sqlx.workspace = true
2524

2625
tokio = { version = "1.41.1", features = ["full"] }
2726

27+
[dev-dependencies]
28+
pg_test_utils.workspace = true
29+
2830
[lib]
2931
doctest = false
3032

crates/pg_completions/src/complete.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ mod tests {
7272
.expect("Error loading sql language");
7373

7474
let tree = parser.parse(input, None).unwrap();
75-
let schema_cache = SchemaCache::load(&test_db).await;
75+
let schema_cache = SchemaCache::load(&test_db)
76+
.await
77+
.expect("Couldn't load Schema Cache");
7678

7779
let p = CompletionParams {
7880
position: ((input.len() - 1) as u32).into(),
@@ -117,7 +119,9 @@ mod tests {
117119
.await
118120
.expect("Failed to execute setup query");
119121

120-
let schema_cache = SchemaCache::load(&test_db).await;
122+
let schema_cache = SchemaCache::load(&test_db)
123+
.await
124+
.expect("Couldn't load Schema Cache");
121125

122126
let mut parser = tree_sitter::Parser::new();
123127
parser
@@ -180,7 +184,9 @@ mod tests {
180184
.await
181185
.expect("Failed to execute setup query");
182186

183-
let schema_cache = SchemaCache::load(&test_db).await;
187+
let schema_cache = SchemaCache::load(&test_db)
188+
.await
189+
.expect("Couldn't load SchemaCache");
184190

185191
let mut parser = tree_sitter::Parser::new();
186192
parser

crates/pg_inlay_hints/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ tree_sitter_sql.workspace = true
2323

2424
[dev-dependencies]
2525
async-std = "1.12.0"
26+
pg_test_utils.workspace = true
27+
2628

2729
[lib]
2830
doctest = false

crates/pg_inlay_hints/src/functions_args.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn resolve_func_arg_hint(
8080
mod tests {
8181
use async_std::task::block_on;
8282
use pg_schema_cache::SchemaCache;
83-
use sqlx::PgPool;
83+
use pg_test_utils::test_database::get_new_test_db;
8484

8585
use crate::{
8686
functions_args::FunctionArgHint,
@@ -89,17 +89,14 @@ mod tests {
8989

9090
#[test]
9191
fn test_function_args() {
92+
let test_db = block_on(get_new_test_db());
9293
let input = "select lower('TEST')";
9394

94-
let conn_string = std::env::var("DATABASE_URL").unwrap();
95-
96-
let pool = block_on(PgPool::connect(conn_string.as_str())).unwrap();
97-
9895
let root = pg_query_ext::parse(input).unwrap();
99-
10096
let res = pg_syntax::parse_syntax(input, &root);
10197

102-
let schema_cache = block_on(SchemaCache::load(&pool));
98+
let schema_cache =
99+
block_on(SchemaCache::load(&test_db)).expect("Couldn't load Schema Cache");
103100

104101
let hints = FunctionArgHint::find_all(InlayHintsParams {
105102
ast: Some(&root),

crates/pg_lsp/src/db_connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl DbConnection {
4141
match res {
4242
Ok(not) => {
4343
if not.payload().to_string() == "reload schema" {
44-
let schema_cache = SchemaCache::load(&cloned_pool).await;
44+
let schema_cache = SchemaCache::load(&cloned_pool).await.unwrap();
4545
ide.write().await.set_schema_cache(schema_cache);
4646
};
4747
}

crates/pg_schema_cache/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ async-std = { version = "1.12.0" }
1717
futures-util = "0.3.31"
1818
serde.workspace = true
1919
serde_json.workspace = true
20+
pg_diagnostics.workspace = true
21+
pg_console.workspace = true
2022

2123
sqlx.workspace = true
2224

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use pg_diagnostics::Diagnostic;
2+
use std::fmt::Debug;
3+
4+
#[derive(Debug, Diagnostic)]
5+
pub enum SchemaCacheError {
6+
DatabaseConnectionError(DatabaseConnectionError),
7+
}
8+
9+
#[derive(Debug, Diagnostic)]
10+
#[diagnostic(
11+
category = "database/connection",
12+
severity = Error,
13+
message(
14+
description = "Unable to Load Database Schema",
15+
message("Database Error Message:: "{self.message})
16+
)
17+
)]
18+
pub struct DatabaseConnectionError {
19+
pub message: String,
20+
pub code: Option<String>,
21+
}
22+
23+
impl From<sqlx::Error> for SchemaCacheError {
24+
fn from(err: sqlx::Error) -> Self {
25+
let db_err = err.as_database_error();
26+
if let Some(db_err) = db_err {
27+
Self::DatabaseConnectionError(DatabaseConnectionError {
28+
message: db_err.message().to_string(),
29+
code: db_err.code().map(|c| c.to_string()),
30+
})
31+
} else {
32+
Self::DatabaseConnectionError(DatabaseConnectionError {
33+
message: err.to_string(),
34+
code: None,
35+
})
36+
}
37+
}
38+
}

crates/pg_schema_cache/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
//! The schema cache
22
33
#![allow(dead_code)]
4-
#![feature(future_join)]
54

5+
mod diagnostics;
66
mod functions;
77
mod schema_cache;
88
mod schemas;
99
mod tables;
1010
mod types;
1111
mod versions;
1212

13+
pub use diagnostics::SchemaCacheError;
1314
pub use functions::{Behavior, Function, FunctionArg, FunctionArgs};
1415
pub use schema_cache::SchemaCache;
1516
pub use tables::{ReplicaIdentity, Table};

crates/pg_schema_cache/src/schema_cache.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use anyhow::Context;
21
use sqlx::postgres::PgPool;
32

3+
use crate::diagnostics::SchemaCacheError;
44
use crate::functions::Function;
55
use crate::schemas::Schema;
66
use crate::tables::Table;
@@ -21,15 +21,14 @@ impl SchemaCache {
2121
SchemaCache::default()
2222
}
2323

24-
pub async fn load(pool: &PgPool) -> anyhow::Result<SchemaCache> {
24+
pub async fn load(pool: &PgPool) -> Result<SchemaCache, SchemaCacheError> {
2525
let (schemas, tables, functions, types, versions) = futures_util::try_join!(
2626
Schema::load(pool),
2727
Table::load(pool),
2828
Function::load(pool),
2929
PostgresType::load(pool),
3030
Version::load(pool),
31-
)
32-
.with_context(|| format!("Unable to load Schema Cache"))?;
31+
)?;
3332

3433
Ok(SchemaCache {
3534
schemas,
@@ -86,7 +85,7 @@ mod tests {
8685

8786
let pool = async_std::task::block_on(PgPool::connect(conn_string.as_str())).unwrap();
8887

89-
async_std::task::block_on(SchemaCache::load(&pool));
88+
async_std::task::block_on(SchemaCache::load(&pool)).expect("Couldn't load Schema Cache");
9089

9190
assert!(true);
9291
}

crates/pg_workspace_new/src/diagnostics.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use pg_diagnostics::{
55
category, Advices, Category, Diagnostic, DiagnosticTags, LogCategory, Severity, Visit,
66
};
77
use pg_fs::FileSystemDiagnostic;
8+
use pg_schema_cache::SchemaCacheError;
89
use serde::{Deserialize, Serialize};
910
use std::error::Error;
1011
use std::fmt;
@@ -239,6 +240,19 @@ impl From<sqlx::Error> for WorkspaceError {
239240
}
240241
}
241242

243+
impl From<SchemaCacheError> for WorkspaceError {
244+
fn from(err: SchemaCacheError) -> Self {
245+
match err {
246+
SchemaCacheError::DatabaseConnectionError(db_err) => {
247+
WorkspaceError::DatabaseConnectionError(DatabaseConnectionError {
248+
message: db_err.message,
249+
code: db_err.code,
250+
})
251+
}
252+
}
253+
}
254+
}
255+
242256
#[derive(Debug, Serialize, Deserialize, Diagnostic)]
243257
#[diagnostic(
244258
category = "internalError/fs",

crates/pg_workspace_new/src/workspace/server.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,8 @@ impl WorkspaceServer {
128128
tracing::info!("Reloading schema cache");
129129
// TODO return error if db connection is not available
130130
if let Some(c) = self.connection.read().unwrap().get_pool() {
131-
let schema_cache = run_async(async move {
132-
// TODO load should return a Result
133-
SchemaCache::load(&c).await
134-
})?;
131+
let maybe_schema_cache = run_async(async move { SchemaCache::load(&c).await })?;
132+
let schema_cache = maybe_schema_cache?;
135133

136134
let mut cache = self.schema_cache.write().unwrap();
137135
*cache = schema_cache;

0 commit comments

Comments
 (0)