Skip to content

fix(schema_cache): return results #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/pg_completions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ 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

sqlx.workspace = true

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

[dev-dependencies]
pg_test_utils.workspace = true

[lib]
doctest = false

Expand Down
12 changes: 9 additions & 3 deletions crates/pg_completions/src/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions crates/pg_inlay_hints/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ tree_sitter_sql.workspace = true

[dev-dependencies]
async-std = "1.12.0"
pg_test_utils.workspace = true


[lib]
doctest = false
Expand Down
11 changes: 4 additions & 7 deletions crates/pg_inlay_hints/src/functions_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion crates/pg_lsp/src/db_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}
Expand Down
8 changes: 7 additions & 1 deletion crates/pg_schema_cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ 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

pg_diagnostics.workspace = true
pg_console.workspace = true
sqlx.workspace = true

[dev-dependencies]
pg_test_utils.workspace = true

[lib]
doctest = false
3 changes: 1 addition & 2 deletions crates/pg_schema_cache/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub struct Function {
impl SchemaCacheItem for Function {
type Item = Function;

async fn load(pool: &PgPool) -> Vec<Function> {
async fn load(pool: &PgPool) -> Result<Vec<Function>, sqlx::Error> {
sqlx::query_as!(
Function,
r#"
Expand Down Expand Up @@ -179,6 +179,5 @@ from
)
.fetch_all(pool)
.await
.unwrap()
}
}
20 changes: 0 additions & 20 deletions crates/pg_schema_cache/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! The schema cache

#![allow(dead_code)]
#![feature(future_join)]

mod functions;
mod schema_cache;
Expand All @@ -10,25 +9,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;
}
}
24 changes: 10 additions & 14 deletions crates/pg_schema_cache/src/schema_cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::future::join;

use sqlx::postgres::PgPool;

use crate::functions::Function;
Expand All @@ -22,23 +20,22 @@ impl SchemaCache {
SchemaCache::default()
}

pub async fn load(pool: &PgPool) -> SchemaCache {
let (schemas, tables, functions, types, versions) = join!(
pub async fn load(pool: &PgPool) -> Result<SchemaCache, sqlx::Error> {
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;
)?;

SchemaCache {
Ok(SchemaCache {
schemas,
tables,
functions,
types,
versions,
}
})
}

/// Applies an AST node to the repository
Expand Down Expand Up @@ -72,22 +69,21 @@ impl SchemaCache {
pub trait SchemaCacheItem {
type Item;

async fn load(pool: &PgPool) -> Vec<Self::Item>;
async fn load(pool: &PgPool) -> Result<Vec<Self::Item>, sqlx::Error>;
}

#[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 pool = async_std::task::block_on(PgPool::connect(conn_string.as_str())).unwrap();
let test_db = block_on(get_new_test_db());

async_std::task::block_on(SchemaCache::load(&pool));
block_on(SchemaCache::load(&test_db)).expect("Couldn't load Schema Cache");

assert!(true);
}
Expand Down
3 changes: 1 addition & 2 deletions crates/pg_schema_cache/src/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Schema {
impl SchemaCacheItem for Schema {
type Item = Schema;

async fn load(pool: &PgPool) -> Vec<Schema> {
async fn load(pool: &PgPool) -> Result<Vec<Schema>, sqlx::Error> {
sqlx::query_as!(
Schema,
r#"select
Expand All @@ -33,6 +33,5 @@ where
)
.fetch_all(pool)
.await
.unwrap()
}
}
3 changes: 1 addition & 2 deletions crates/pg_schema_cache/src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct Table {
impl SchemaCacheItem for Table {
type Item = Table;

async fn load(pool: &PgPool) -> Vec<Table> {
async fn load(pool: &PgPool) -> Result<Vec<Table>, sqlx::Error> {
sqlx::query_as!(
Table,
r#"SELECT
Expand Down Expand Up @@ -87,6 +87,5 @@ group by
)
.fetch_all(pool)
.await
.unwrap()
}
}
3 changes: 1 addition & 2 deletions crates/pg_schema_cache/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub struct PostgresType {
impl SchemaCacheItem for PostgresType {
type Item = PostgresType;

async fn load(pool: &PgPool) -> Vec<PostgresType> {
async fn load(pool: &PgPool) -> Result<Vec<PostgresType>, sqlx::Error> {
sqlx::query_as!(
PostgresType,
r#"select
Expand Down Expand Up @@ -103,6 +103,5 @@ where
)
.fetch_all(pool)
.await
.unwrap()
}
}
3 changes: 1 addition & 2 deletions crates/pg_schema_cache/src/versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Version {
impl SchemaCacheItem for Version {
type Item = Version;

async fn load(pool: &PgPool) -> Vec<Version> {
async fn load(pool: &PgPool) -> Result<Vec<Version>, sqlx::Error> {
sqlx::query_as!(
Version,
r#"select
Expand All @@ -29,7 +29,6 @@ impl SchemaCacheItem for Version {
)
.fetch_all(pool)
.await
.unwrap()
}

/*
Expand Down
1 change: 1 addition & 0 deletions crates/pg_typecheck/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async-std = "1.12.0"


[dev-dependencies]
pg_test_utils.workspace = true

[lib]
doctest = false
Expand Down
12 changes: 5 additions & 7 deletions crates/pg_typecheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,21 @@ pub async fn check_sql<'a>(params: TypecheckerParams<'a>) -> Vec<TypeError> {
#[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),
Expand All @@ -111,6 +109,6 @@ mod tests {

let e = &errs[0];

assert_eq!(&input[e.range.unwrap()], "unknown");
assert_eq!(&input[e.range.unwrap()], "contact");
}
}
6 changes: 2 additions & 4 deletions crates/pg_workspace_new/src/workspace/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading