Skip to content

Commit 97679c2

Browse files
authored
Merge pull request #154 from juleswritescode/cache/result
2 parents 652da2b + d20e70f commit 97679c2

File tree

17 files changed

+56
-68
lines changed

17 files changed

+56
-68
lines changed

Cargo.lock

Lines changed: 7 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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@ version = "0.0.0"
1212

1313

1414
[dependencies]
15+
anyhow.workspace = true
1516
async-std = { version = "1.12.0" }
17+
futures-util = "0.3.31"
1618
serde.workspace = true
1719
serde_json.workspace = true
18-
20+
pg_diagnostics.workspace = true
21+
pg_console.workspace = true
1922
sqlx.workspace = true
2023

24+
[dev-dependencies]
25+
pg_test_utils.workspace = true
26+
2127
[lib]
2228
doctest = false

crates/pg_schema_cache/src/functions.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub struct Function {
6969
impl SchemaCacheItem for Function {
7070
type Item = Function;
7171

72-
async fn load(pool: &PgPool) -> Vec<Function> {
72+
async fn load(pool: &PgPool) -> Result<Vec<Function>, sqlx::Error> {
7373
sqlx::query_as!(
7474
Function,
7575
r#"
@@ -179,6 +179,5 @@ from
179179
)
180180
.fetch_all(pool)
181181
.await
182-
.unwrap()
183182
}
184183
}

crates/pg_schema_cache/src/lib.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! The schema cache
22
33
#![allow(dead_code)]
4-
#![feature(future_join)]
54

65
mod functions;
76
mod schema_cache;
@@ -10,25 +9,6 @@ mod tables;
109
mod types;
1110
mod versions;
1211

13-
use sqlx::postgres::PgPool;
14-
1512
pub use functions::{Behavior, Function, FunctionArg, FunctionArgs};
1613
pub use schema_cache::SchemaCache;
1714
pub use tables::{ReplicaIdentity, Table};
18-
19-
#[derive(Debug, Clone)]
20-
struct SchemaCacheManager {
21-
pub cache: SchemaCache,
22-
}
23-
24-
impl SchemaCacheManager {
25-
pub async fn init(pool: &PgPool) -> Self {
26-
SchemaCacheManager {
27-
cache: SchemaCache::load(pool).await,
28-
}
29-
}
30-
31-
pub async fn reload_cache(&mut self, pool: &PgPool) {
32-
self.cache = SchemaCache::load(pool).await;
33-
}
34-
}

crates/pg_schema_cache/src/schema_cache.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::future::join;
2-
31
use sqlx::postgres::PgPool;
42

53
use crate::functions::Function;
@@ -22,23 +20,22 @@ impl SchemaCache {
2220
SchemaCache::default()
2321
}
2422

25-
pub async fn load(pool: &PgPool) -> SchemaCache {
26-
let (schemas, tables, functions, types, versions) = join!(
23+
pub async fn load(pool: &PgPool) -> Result<SchemaCache, sqlx::Error> {
24+
let (schemas, tables, functions, types, versions) = futures_util::try_join!(
2725
Schema::load(pool),
2826
Table::load(pool),
2927
Function::load(pool),
3028
PostgresType::load(pool),
3129
Version::load(pool),
32-
)
33-
.await;
30+
)?;
3431

35-
SchemaCache {
32+
Ok(SchemaCache {
3633
schemas,
3734
tables,
3835
functions,
3936
types,
4037
versions,
41-
}
38+
})
4239
}
4340

4441
/// Applies an AST node to the repository
@@ -72,22 +69,21 @@ impl SchemaCache {
7269
pub trait SchemaCacheItem {
7370
type Item;
7471

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

7875
#[cfg(test)]
7976
mod tests {
80-
use sqlx::PgPool;
77+
use async_std::task::block_on;
78+
use pg_test_utils::test_database::get_new_test_db;
8179

8280
use crate::SchemaCache;
8381

8482
#[test]
8583
fn test_schema_cache() {
86-
let conn_string = std::env::var("DATABASE_URL").unwrap();
87-
88-
let pool = async_std::task::block_on(PgPool::connect(conn_string.as_str())).unwrap();
84+
let test_db = block_on(get_new_test_db());
8985

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

9288
assert!(true);
9389
}

0 commit comments

Comments
 (0)