From 897b62e69ada99342afb266dc281d344c1316c10 Mon Sep 17 00:00:00 2001 From: Julian Date: Fri, 13 Dec 2024 15:34:05 +0100 Subject: [PATCH 1/5] fix(schema_cache): return results --- Cargo.lock | 2 ++ crates/pg_schema_cache/Cargo.toml | 2 ++ crates/pg_schema_cache/src/functions.rs | 3 +-- crates/pg_schema_cache/src/schema_cache.rs | 15 +++++++-------- crates/pg_schema_cache/src/schemas.rs | 3 +-- crates/pg_schema_cache/src/tables.rs | 3 +-- crates/pg_schema_cache/src/types.rs | 3 +-- crates/pg_schema_cache/src/versions.rs | 3 +-- 8 files changed, 16 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4b69acca..968d1ec9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2560,7 +2560,9 @@ dependencies = [ name = "pg_schema_cache" version = "0.0.0" dependencies = [ + "anyhow", "async-std", + "futures-util", "serde", "serde_json", "sqlx", diff --git a/crates/pg_schema_cache/Cargo.toml b/crates/pg_schema_cache/Cargo.toml index 16baad99..bdd0b5d6 100644 --- a/crates/pg_schema_cache/Cargo.toml +++ b/crates/pg_schema_cache/Cargo.toml @@ -12,7 +12,9 @@ version = "0.0.0" [dependencies] +anyhow.workspace = true async-std = { version = "1.12.0" } +futures-util = "0.3.31" serde.workspace = true serde_json.workspace = true diff --git a/crates/pg_schema_cache/src/functions.rs b/crates/pg_schema_cache/src/functions.rs index 4813de4c..097c8f84 100644 --- a/crates/pg_schema_cache/src/functions.rs +++ b/crates/pg_schema_cache/src/functions.rs @@ -69,7 +69,7 @@ pub struct Function { impl SchemaCacheItem for Function { type Item = Function; - async fn load(pool: &PgPool) -> Vec { + async fn load(pool: &PgPool) -> Result, sqlx::Error> { sqlx::query_as!( Function, r#" @@ -179,6 +179,5 @@ from ) .fetch_all(pool) .await - .unwrap() } } diff --git a/crates/pg_schema_cache/src/schema_cache.rs b/crates/pg_schema_cache/src/schema_cache.rs index f0e019cd..e26f9588 100644 --- a/crates/pg_schema_cache/src/schema_cache.rs +++ b/crates/pg_schema_cache/src/schema_cache.rs @@ -1,5 +1,4 @@ -use std::future::join; - +use anyhow::Context; use sqlx::postgres::PgPool; use crate::functions::Function; @@ -22,23 +21,23 @@ impl SchemaCache { SchemaCache::default() } - pub async fn load(pool: &PgPool) -> SchemaCache { - let (schemas, tables, functions, types, versions) = join!( + pub async fn load(pool: &PgPool) -> anyhow::Result { + let (schemas, tables, functions, types, versions) = futures_util::try_join!( Schema::load(pool), Table::load(pool), Function::load(pool), PostgresType::load(pool), Version::load(pool), ) - .await; + .with_context(|| format!("Unable to load Schema Cache"))?; - SchemaCache { + Ok(SchemaCache { schemas, tables, functions, types, versions, - } + }) } /// Applies an AST node to the repository @@ -72,7 +71,7 @@ impl SchemaCache { pub trait SchemaCacheItem { type Item; - async fn load(pool: &PgPool) -> Vec; + async fn load(pool: &PgPool) -> Result, sqlx::Error>; } #[cfg(test)] diff --git a/crates/pg_schema_cache/src/schemas.rs b/crates/pg_schema_cache/src/schemas.rs index 5ca0a25e..a5d71ef6 100644 --- a/crates/pg_schema_cache/src/schemas.rs +++ b/crates/pg_schema_cache/src/schemas.rs @@ -12,7 +12,7 @@ pub struct Schema { impl SchemaCacheItem for Schema { type Item = Schema; - async fn load(pool: &PgPool) -> Vec { + async fn load(pool: &PgPool) -> Result, sqlx::Error> { sqlx::query_as!( Schema, r#"select @@ -33,6 +33,5 @@ where ) .fetch_all(pool) .await - .unwrap() } } diff --git a/crates/pg_schema_cache/src/tables.rs b/crates/pg_schema_cache/src/tables.rs index 60a1ff0f..1154f106 100644 --- a/crates/pg_schema_cache/src/tables.rs +++ b/crates/pg_schema_cache/src/tables.rs @@ -41,7 +41,7 @@ pub struct Table { impl SchemaCacheItem for Table { type Item = Table; - async fn load(pool: &PgPool) -> Vec { + async fn load(pool: &PgPool) -> Result, sqlx::Error> { sqlx::query_as!( Table, r#"SELECT @@ -87,6 +87,5 @@ group by ) .fetch_all(pool) .await - .unwrap() } } diff --git a/crates/pg_schema_cache/src/types.rs b/crates/pg_schema_cache/src/types.rs index fd113604..dab04308 100644 --- a/crates/pg_schema_cache/src/types.rs +++ b/crates/pg_schema_cache/src/types.rs @@ -50,7 +50,7 @@ pub struct PostgresType { impl SchemaCacheItem for PostgresType { type Item = PostgresType; - async fn load(pool: &PgPool) -> Vec { + async fn load(pool: &PgPool) -> Result, sqlx::Error> { sqlx::query_as!( PostgresType, r#"select @@ -103,6 +103,5 @@ where ) .fetch_all(pool) .await - .unwrap() } } diff --git a/crates/pg_schema_cache/src/versions.rs b/crates/pg_schema_cache/src/versions.rs index eff70f14..40b0eb66 100644 --- a/crates/pg_schema_cache/src/versions.rs +++ b/crates/pg_schema_cache/src/versions.rs @@ -13,7 +13,7 @@ pub struct Version { impl SchemaCacheItem for Version { type Item = Version; - async fn load(pool: &PgPool) -> Vec { + async fn load(pool: &PgPool) -> Result, sqlx::Error> { sqlx::query_as!( Version, r#"select @@ -29,7 +29,6 @@ impl SchemaCacheItem for Version { ) .fetch_all(pool) .await - .unwrap() } /* From 99194c971f7e961518978158c9cb74328727f58b Mon Sep 17 00:00:00 2001 From: Julian Date: Fri, 13 Dec 2024 15:35:42 +0100 Subject: [PATCH 2/5] chore: remove unused schema cache manager --- crates/pg_schema_cache/src/lib.rs | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/crates/pg_schema_cache/src/lib.rs b/crates/pg_schema_cache/src/lib.rs index 82454fc3..f8ba6d1a 100644 --- a/crates/pg_schema_cache/src/lib.rs +++ b/crates/pg_schema_cache/src/lib.rs @@ -10,25 +10,6 @@ mod tables; mod types; mod versions; -use sqlx::postgres::PgPool; - pub use functions::{Behavior, Function, FunctionArg, FunctionArgs}; pub use schema_cache::SchemaCache; pub use tables::{ReplicaIdentity, Table}; - -#[derive(Debug, Clone)] -struct SchemaCacheManager { - pub cache: SchemaCache, -} - -impl SchemaCacheManager { - pub async fn init(pool: &PgPool) -> Self { - SchemaCacheManager { - cache: SchemaCache::load(pool).await, - } - } - - pub async fn reload_cache(&mut self, pool: &PgPool) { - self.cache = SchemaCache::load(pool).await; - } -} From 6c67271c8619d645fbad228e4080618ba7e4162a Mon Sep 17 00:00:00 2001 From: Julian Date: Fri, 13 Dec 2024 17:16:48 +0100 Subject: [PATCH 3/5] feat(schema_cache): implement diagnostics --- Cargo.lock | 3 ++ crates/pg_completions/Cargo.toml | 4 +- crates/pg_completions/src/complete.rs | 12 ++++-- crates/pg_inlay_hints/Cargo.toml | 2 + crates/pg_inlay_hints/src/functions_args.rs | 11 ++---- crates/pg_lsp/src/db_connection.rs | 2 +- crates/pg_schema_cache/Cargo.toml | 2 + crates/pg_schema_cache/src/diagnostics.rs | 38 +++++++++++++++++++ crates/pg_schema_cache/src/lib.rs | 3 +- crates/pg_schema_cache/src/schema_cache.rs | 9 ++--- crates/pg_workspace_new/src/diagnostics.rs | 14 +++++++ .../pg_workspace_new/src/workspace/server.rs | 6 +-- 12 files changed, 84 insertions(+), 22 deletions(-) create mode 100644 crates/pg_schema_cache/src/diagnostics.rs diff --git a/Cargo.lock b/Cargo.lock index 968d1ec9..0919999b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2395,6 +2395,7 @@ dependencies = [ "pg_query_ext", "pg_schema_cache", "pg_syntax", + "pg_test_utils", "pg_type_resolver", "sqlx", "text-size", @@ -2563,6 +2564,8 @@ dependencies = [ "anyhow", "async-std", "futures-util", + "pg_console", + "pg_diagnostics", "serde", "serde_json", "sqlx", diff --git a/crates/pg_completions/Cargo.toml b/crates/pg_completions/Cargo.toml index b62cca3b..9329108e 100644 --- a/crates/pg_completions/Cargo.toml +++ b/crates/pg_completions/Cargo.toml @@ -17,7 +17,6 @@ async-std = "1.12.0" text-size.workspace = true pg_schema_cache.workspace = true -pg_test_utils.workspace = true tree-sitter.workspace = true tree_sitter_sql.workspace = true @@ -25,6 +24,9 @@ sqlx.workspace = true tokio = { version = "1.41.1", features = ["full"] } +[dev-dependencies] +pg_test_utils.workspace = true + [lib] doctest = false diff --git a/crates/pg_completions/src/complete.rs b/crates/pg_completions/src/complete.rs index 7118281a..cdde4726 100644 --- a/crates/pg_completions/src/complete.rs +++ b/crates/pg_completions/src/complete.rs @@ -72,7 +72,9 @@ mod tests { .expect("Error loading sql language"); let tree = parser.parse(input, None).unwrap(); - let schema_cache = SchemaCache::load(&test_db).await; + let schema_cache = SchemaCache::load(&test_db) + .await + .expect("Couldn't load Schema Cache"); let p = CompletionParams { position: ((input.len() - 1) as u32).into(), @@ -117,7 +119,9 @@ mod tests { .await .expect("Failed to execute setup query"); - let schema_cache = SchemaCache::load(&test_db).await; + let schema_cache = SchemaCache::load(&test_db) + .await + .expect("Couldn't load Schema Cache"); let mut parser = tree_sitter::Parser::new(); parser @@ -180,7 +184,9 @@ mod tests { .await .expect("Failed to execute setup query"); - let schema_cache = SchemaCache::load(&test_db).await; + let schema_cache = SchemaCache::load(&test_db) + .await + .expect("Couldn't load SchemaCache"); let mut parser = tree_sitter::Parser::new(); parser diff --git a/crates/pg_inlay_hints/Cargo.toml b/crates/pg_inlay_hints/Cargo.toml index 3a272eec..98835234 100644 --- a/crates/pg_inlay_hints/Cargo.toml +++ b/crates/pg_inlay_hints/Cargo.toml @@ -23,6 +23,8 @@ tree_sitter_sql.workspace = true [dev-dependencies] async-std = "1.12.0" +pg_test_utils.workspace = true + [lib] doctest = false diff --git a/crates/pg_inlay_hints/src/functions_args.rs b/crates/pg_inlay_hints/src/functions_args.rs index c6e13e3b..67dc5fae 100644 --- a/crates/pg_inlay_hints/src/functions_args.rs +++ b/crates/pg_inlay_hints/src/functions_args.rs @@ -80,7 +80,7 @@ fn resolve_func_arg_hint( mod tests { use async_std::task::block_on; use pg_schema_cache::SchemaCache; - use sqlx::PgPool; + use pg_test_utils::test_database::get_new_test_db; use crate::{ functions_args::FunctionArgHint, @@ -89,17 +89,14 @@ mod tests { #[test] fn test_function_args() { + let test_db = block_on(get_new_test_db()); let input = "select lower('TEST')"; - let conn_string = std::env::var("DATABASE_URL").unwrap(); - - let pool = block_on(PgPool::connect(conn_string.as_str())).unwrap(); - let root = pg_query_ext::parse(input).unwrap(); - let res = pg_syntax::parse_syntax(input, &root); - let schema_cache = block_on(SchemaCache::load(&pool)); + let schema_cache = + block_on(SchemaCache::load(&test_db)).expect("Couldn't load Schema Cache"); let hints = FunctionArgHint::find_all(InlayHintsParams { ast: Some(&root), diff --git a/crates/pg_lsp/src/db_connection.rs b/crates/pg_lsp/src/db_connection.rs index 6688bc1c..6948781b 100644 --- a/crates/pg_lsp/src/db_connection.rs +++ b/crates/pg_lsp/src/db_connection.rs @@ -41,7 +41,7 @@ impl DbConnection { match res { Ok(not) => { if not.payload().to_string() == "reload schema" { - let schema_cache = SchemaCache::load(&cloned_pool).await; + let schema_cache = SchemaCache::load(&cloned_pool).await.unwrap(); ide.write().await.set_schema_cache(schema_cache); }; } diff --git a/crates/pg_schema_cache/Cargo.toml b/crates/pg_schema_cache/Cargo.toml index bdd0b5d6..83009283 100644 --- a/crates/pg_schema_cache/Cargo.toml +++ b/crates/pg_schema_cache/Cargo.toml @@ -17,6 +17,8 @@ async-std = { version = "1.12.0" } futures-util = "0.3.31" serde.workspace = true serde_json.workspace = true +pg_diagnostics.workspace = true +pg_console.workspace = true sqlx.workspace = true diff --git a/crates/pg_schema_cache/src/diagnostics.rs b/crates/pg_schema_cache/src/diagnostics.rs new file mode 100644 index 00000000..36639f51 --- /dev/null +++ b/crates/pg_schema_cache/src/diagnostics.rs @@ -0,0 +1,38 @@ +use pg_diagnostics::Diagnostic; +use std::fmt::Debug; + +#[derive(Debug, Diagnostic)] +pub enum SchemaCacheError { + DatabaseConnectionError(DatabaseConnectionError), +} + +#[derive(Debug, Diagnostic)] +#[diagnostic( + category = "database/connection", + severity = Error, + message( + description = "Unable to Load Database Schema", + message("Database Error Message:: "{self.message}) + ) +)] +pub struct DatabaseConnectionError { + pub message: String, + pub code: Option, +} + +impl From for SchemaCacheError { + fn from(err: sqlx::Error) -> Self { + let db_err = err.as_database_error(); + if let Some(db_err) = db_err { + Self::DatabaseConnectionError(DatabaseConnectionError { + message: db_err.message().to_string(), + code: db_err.code().map(|c| c.to_string()), + }) + } else { + Self::DatabaseConnectionError(DatabaseConnectionError { + message: err.to_string(), + code: None, + }) + } + } +} diff --git a/crates/pg_schema_cache/src/lib.rs b/crates/pg_schema_cache/src/lib.rs index f8ba6d1a..85b204a0 100644 --- a/crates/pg_schema_cache/src/lib.rs +++ b/crates/pg_schema_cache/src/lib.rs @@ -1,8 +1,8 @@ //! The schema cache #![allow(dead_code)] -#![feature(future_join)] +mod diagnostics; mod functions; mod schema_cache; mod schemas; @@ -10,6 +10,7 @@ mod tables; mod types; mod versions; +pub use diagnostics::SchemaCacheError; pub use functions::{Behavior, Function, FunctionArg, FunctionArgs}; pub use schema_cache::SchemaCache; pub use tables::{ReplicaIdentity, Table}; diff --git a/crates/pg_schema_cache/src/schema_cache.rs b/crates/pg_schema_cache/src/schema_cache.rs index e26f9588..78d6584f 100644 --- a/crates/pg_schema_cache/src/schema_cache.rs +++ b/crates/pg_schema_cache/src/schema_cache.rs @@ -1,6 +1,6 @@ -use anyhow::Context; use sqlx::postgres::PgPool; +use crate::diagnostics::SchemaCacheError; use crate::functions::Function; use crate::schemas::Schema; use crate::tables::Table; @@ -21,15 +21,14 @@ impl SchemaCache { SchemaCache::default() } - pub async fn load(pool: &PgPool) -> anyhow::Result { + pub async fn load(pool: &PgPool) -> Result { let (schemas, tables, functions, types, versions) = futures_util::try_join!( Schema::load(pool), Table::load(pool), Function::load(pool), PostgresType::load(pool), Version::load(pool), - ) - .with_context(|| format!("Unable to load Schema Cache"))?; + )?; Ok(SchemaCache { schemas, @@ -86,7 +85,7 @@ mod tests { let pool = async_std::task::block_on(PgPool::connect(conn_string.as_str())).unwrap(); - async_std::task::block_on(SchemaCache::load(&pool)); + async_std::task::block_on(SchemaCache::load(&pool)).expect("Couldn't load Schema Cache"); assert!(true); } diff --git a/crates/pg_workspace_new/src/diagnostics.rs b/crates/pg_workspace_new/src/diagnostics.rs index a1a11a1e..88a81712 100644 --- a/crates/pg_workspace_new/src/diagnostics.rs +++ b/crates/pg_workspace_new/src/diagnostics.rs @@ -5,6 +5,7 @@ use pg_diagnostics::{ category, Advices, Category, Diagnostic, DiagnosticTags, LogCategory, Severity, Visit, }; use pg_fs::FileSystemDiagnostic; +use pg_schema_cache::SchemaCacheError; use serde::{Deserialize, Serialize}; use std::error::Error; use std::fmt; @@ -239,6 +240,19 @@ impl From for WorkspaceError { } } +impl From for WorkspaceError { + fn from(err: SchemaCacheError) -> Self { + match err { + SchemaCacheError::DatabaseConnectionError(db_err) => { + WorkspaceError::DatabaseConnectionError(DatabaseConnectionError { + message: db_err.message, + code: db_err.code, + }) + } + } + } +} + #[derive(Debug, Serialize, Deserialize, Diagnostic)] #[diagnostic( category = "internalError/fs", diff --git a/crates/pg_workspace_new/src/workspace/server.rs b/crates/pg_workspace_new/src/workspace/server.rs index 653cea65..a2ce0bbd 100644 --- a/crates/pg_workspace_new/src/workspace/server.rs +++ b/crates/pg_workspace_new/src/workspace/server.rs @@ -128,10 +128,8 @@ impl WorkspaceServer { tracing::info!("Reloading schema cache"); // TODO return error if db connection is not available if let Some(c) = self.connection.read().unwrap().get_pool() { - let schema_cache = run_async(async move { - // TODO load should return a Result - SchemaCache::load(&c).await - })?; + let maybe_schema_cache = run_async(async move { SchemaCache::load(&c).await })?; + let schema_cache = maybe_schema_cache?; let mut cache = self.schema_cache.write().unwrap(); *cache = schema_cache; From 59b8065a69bdd07446fe0edcfc3fcd51da8de5cd Mon Sep 17 00:00:00 2001 From: Julian Date: Fri, 13 Dec 2024 17:21:00 +0100 Subject: [PATCH 4/5] fix?: use test db in tests --- Cargo.lock | 2 ++ crates/pg_schema_cache/Cargo.toml | 4 +++- crates/pg_schema_cache/src/schema_cache.rs | 9 ++++----- crates/pg_typecheck/Cargo.toml | 1 + crates/pg_typecheck/src/lib.rs | 12 +++++------- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0919999b..f6a2c53a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2566,6 +2566,7 @@ dependencies = [ "futures-util", "pg_console", "pg_diagnostics", + "pg_test_utils", "serde", "serde_json", "sqlx", @@ -2634,6 +2635,7 @@ dependencies = [ "pg_query_ext", "pg_schema_cache", "pg_syntax", + "pg_test_utils", "sqlx", "text-size", ] diff --git a/crates/pg_schema_cache/Cargo.toml b/crates/pg_schema_cache/Cargo.toml index 83009283..534a6c16 100644 --- a/crates/pg_schema_cache/Cargo.toml +++ b/crates/pg_schema_cache/Cargo.toml @@ -19,8 +19,10 @@ serde.workspace = true serde_json.workspace = true pg_diagnostics.workspace = true pg_console.workspace = true - sqlx.workspace = true +[dev-dependencies] +pg_test_utils.workspace = true + [lib] doctest = false diff --git a/crates/pg_schema_cache/src/schema_cache.rs b/crates/pg_schema_cache/src/schema_cache.rs index 78d6584f..0b2cd290 100644 --- a/crates/pg_schema_cache/src/schema_cache.rs +++ b/crates/pg_schema_cache/src/schema_cache.rs @@ -75,17 +75,16 @@ pub trait SchemaCacheItem { #[cfg(test)] mod tests { - use sqlx::PgPool; + use async_std::task::block_on; + use pg_test_utils::test_database::get_new_test_db; use crate::SchemaCache; #[test] fn test_schema_cache() { - let conn_string = std::env::var("DATABASE_URL").unwrap(); + let test_db = block_on(get_new_test_db()); - let pool = async_std::task::block_on(PgPool::connect(conn_string.as_str())).unwrap(); - - async_std::task::block_on(SchemaCache::load(&pool)).expect("Couldn't load Schema Cache"); + block_on(SchemaCache::load(&test_db)).expect("Couldn't load Schema Cache"); assert!(true); } diff --git a/crates/pg_typecheck/Cargo.toml b/crates/pg_typecheck/Cargo.toml index e238dd46..a66023c9 100644 --- a/crates/pg_typecheck/Cargo.toml +++ b/crates/pg_typecheck/Cargo.toml @@ -24,6 +24,7 @@ async-std = "1.12.0" [dev-dependencies] +pg_test_utils.workspace = true [lib] doctest = false diff --git a/crates/pg_typecheck/src/lib.rs b/crates/pg_typecheck/src/lib.rs index 07d0716c..e160159f 100644 --- a/crates/pg_typecheck/src/lib.rs +++ b/crates/pg_typecheck/src/lib.rs @@ -85,23 +85,21 @@ pub async fn check_sql<'a>(params: TypecheckerParams<'a>) -> Vec { #[cfg(test)] mod tests { use async_std::task::block_on; - use sqlx::PgPool; + use pg_test_utils::test_database::get_new_test_db; use crate::{check_sql, TypecheckerParams}; #[test] - fn test_check_sql() { + fn test_basic_type() { let input = "select id, unknown from contact;"; - let conn_string = std::env::var("DATABASE_URL").unwrap(); - - let pool = block_on(PgPool::connect(conn_string.as_str())).unwrap(); + let test_db = block_on(get_new_test_db()); let root = pg_query_ext::parse(input).unwrap(); let ast = pg_syntax::parse_syntax(input, &root).ast; let errs = block_on(check_sql(TypecheckerParams { - conn: &pool, + conn: &test_db, sql: input, ast: &root, enriched_ast: Some(&ast), @@ -111,6 +109,6 @@ mod tests { let e = &errs[0]; - assert_eq!(&input[e.range.unwrap()], "unknown"); + assert_eq!(&input[e.range.unwrap()], "contact"); } } From d20e70f1d9cecab1567251f5b3350b7832cc23bc Mon Sep 17 00:00:00 2001 From: Julian Date: Sat, 14 Dec 2024 19:20:05 +0100 Subject: [PATCH 5/5] chore: remove schemacache error --- crates/pg_schema_cache/src/diagnostics.rs | 38 ---------------------- crates/pg_schema_cache/src/lib.rs | 2 -- crates/pg_schema_cache/src/schema_cache.rs | 3 +- crates/pg_workspace_new/src/diagnostics.rs | 14 -------- 4 files changed, 1 insertion(+), 56 deletions(-) delete mode 100644 crates/pg_schema_cache/src/diagnostics.rs diff --git a/crates/pg_schema_cache/src/diagnostics.rs b/crates/pg_schema_cache/src/diagnostics.rs deleted file mode 100644 index 36639f51..00000000 --- a/crates/pg_schema_cache/src/diagnostics.rs +++ /dev/null @@ -1,38 +0,0 @@ -use pg_diagnostics::Diagnostic; -use std::fmt::Debug; - -#[derive(Debug, Diagnostic)] -pub enum SchemaCacheError { - DatabaseConnectionError(DatabaseConnectionError), -} - -#[derive(Debug, Diagnostic)] -#[diagnostic( - category = "database/connection", - severity = Error, - message( - description = "Unable to Load Database Schema", - message("Database Error Message:: "{self.message}) - ) -)] -pub struct DatabaseConnectionError { - pub message: String, - pub code: Option, -} - -impl From for SchemaCacheError { - fn from(err: sqlx::Error) -> Self { - let db_err = err.as_database_error(); - if let Some(db_err) = db_err { - Self::DatabaseConnectionError(DatabaseConnectionError { - message: db_err.message().to_string(), - code: db_err.code().map(|c| c.to_string()), - }) - } else { - Self::DatabaseConnectionError(DatabaseConnectionError { - message: err.to_string(), - code: None, - }) - } - } -} diff --git a/crates/pg_schema_cache/src/lib.rs b/crates/pg_schema_cache/src/lib.rs index 85b204a0..ec1596c3 100644 --- a/crates/pg_schema_cache/src/lib.rs +++ b/crates/pg_schema_cache/src/lib.rs @@ -2,7 +2,6 @@ #![allow(dead_code)] -mod diagnostics; mod functions; mod schema_cache; mod schemas; @@ -10,7 +9,6 @@ mod tables; mod types; mod versions; -pub use diagnostics::SchemaCacheError; pub use functions::{Behavior, Function, FunctionArg, FunctionArgs}; pub use schema_cache::SchemaCache; pub use tables::{ReplicaIdentity, Table}; diff --git a/crates/pg_schema_cache/src/schema_cache.rs b/crates/pg_schema_cache/src/schema_cache.rs index 0b2cd290..b9762122 100644 --- a/crates/pg_schema_cache/src/schema_cache.rs +++ b/crates/pg_schema_cache/src/schema_cache.rs @@ -1,6 +1,5 @@ use sqlx::postgres::PgPool; -use crate::diagnostics::SchemaCacheError; use crate::functions::Function; use crate::schemas::Schema; use crate::tables::Table; @@ -21,7 +20,7 @@ impl SchemaCache { SchemaCache::default() } - pub async fn load(pool: &PgPool) -> Result { + pub async fn load(pool: &PgPool) -> Result { let (schemas, tables, functions, types, versions) = futures_util::try_join!( Schema::load(pool), Table::load(pool), diff --git a/crates/pg_workspace_new/src/diagnostics.rs b/crates/pg_workspace_new/src/diagnostics.rs index 88a81712..a1a11a1e 100644 --- a/crates/pg_workspace_new/src/diagnostics.rs +++ b/crates/pg_workspace_new/src/diagnostics.rs @@ -5,7 +5,6 @@ use pg_diagnostics::{ category, Advices, Category, Diagnostic, DiagnosticTags, LogCategory, Severity, Visit, }; use pg_fs::FileSystemDiagnostic; -use pg_schema_cache::SchemaCacheError; use serde::{Deserialize, Serialize}; use std::error::Error; use std::fmt; @@ -240,19 +239,6 @@ impl From for WorkspaceError { } } -impl From for WorkspaceError { - fn from(err: SchemaCacheError) -> Self { - match err { - SchemaCacheError::DatabaseConnectionError(db_err) => { - WorkspaceError::DatabaseConnectionError(DatabaseConnectionError { - message: db_err.message, - code: db_err.code, - }) - } - } - } -} - #[derive(Debug, Serialize, Deserialize, Diagnostic)] #[diagnostic( category = "internalError/fs",