Skip to content

Commit 221e9cc

Browse files
skip db checks via flag
1 parent 8bd878b commit 221e9cc

File tree

7 files changed

+35
-16
lines changed

7 files changed

+35
-16
lines changed

crates/pg_cli/src/cli_options.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ pub struct CliOptions {
1818
#[bpaf(long("use-server"), switch, fallback(false))]
1919
pub use_server: bool,
2020

21+
/// Skip over files containing syntax errors instead of emitting an error diagnostic.
22+
#[bpaf(long("skip-db"), switch, fallback(false))]
23+
pub skip_db: bool,
24+
2125
/// Print additional diagnostics, and some diagnostics show more information. Also, print out what files were processed and which ones were modified.
2226
#[bpaf(long("verbose"), switch, fallback(false))]
2327
pub verbose: bool,

crates/pg_cli/src/commands/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ pub enum PgLspCommand {
3535
Check {
3636
#[bpaf(external(partial_configuration), hide_usage, optional)]
3737
configuration: Option<PartialConfiguration>,
38+
3839
#[bpaf(external, hide_usage)]
3940
cli_options: CliOptions,
41+
4042
/// Use this option when you want to format code piped from `stdin`, and print the output to `stdout`.
4143
///
4244
/// The file doesn't need to exist on disk, what matters is the extension of the file. Based on the extension, we know how to check the code.
@@ -286,6 +288,7 @@ pub(crate) trait CommandRunner: Sized {
286288
configuration,
287289
vcs_base_path,
288290
gitignore_matches,
291+
skip_db: cli_options.skip_db,
289292
})?;
290293

291294
let execution = self.get_execution(cli_options, console, workspace)?;

crates/pg_cli/src/execute/process_file/check.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub(crate) fn check_with_guard<'ctx>(
3535
max_diagnostics,
3636
only,
3737
skip,
38+
true,
3839
)
3940
.with_file_path_and_code(
4041
workspace_file.path.display().to_string(),

crates/pg_lsp/src/session.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ impl Session {
464464
configuration: fs_configuration,
465465
vcs_base_path,
466466
gitignore_matches,
467+
skip_db: false,
467468
});
468469

469470
if let Err(error) = result {

crates/pg_workspace/src/workspace.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pub struct UpdateSettingsParams {
9090
pub vcs_base_path: Option<PathBuf>,
9191
pub gitignore_matches: Vec<String>,
9292
pub workspace_directory: Option<PathBuf>,
93+
pub skip_db: bool,
9394
}
9495

9596
#[derive(Debug, serde::Serialize, serde::Deserialize)]

crates/pg_workspace/src/workspace/server.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,12 @@ impl Workspace for WorkspaceServer {
161161

162162
tracing::info!("Updated settings in workspace");
163163

164-
self.connection
165-
.write()
166-
.unwrap()
167-
.set_conn_settings(&self.settings().as_ref().db);
164+
if !params.skip_db {
165+
self.connection
166+
.write()
167+
.unwrap()
168+
.set_conn_settings(&self.settings().as_ref().db);
169+
}
168170

169171
tracing::info!("Updated Db connection settings");
170172

@@ -298,10 +300,12 @@ impl Workspace for WorkspaceServer {
298300

299301
let mut diagnostics: Vec<SDiagnostic> = vec![];
300302

301-
// run diagnostics for each statement in parallel if its mostly i/o work
302-
if let Ok(connection) = self.connection.read() {
303-
let pool = connection.get_pool();
304-
303+
if let Some(pool) = self
304+
.connection
305+
.read()
306+
.expect("DbConnection RwLock panicked")
307+
.get_pool()
308+
{
305309
let typecheck_params: Vec<_> = doc
306310
.iter_statements_with_text_and_range()
307311
.map(|(stmt, range, text)| {
@@ -311,12 +315,12 @@ impl Workspace for WorkspaceServer {
311315
})
312316
.collect();
313317

314-
let pool_clone = pool.clone();
318+
// run diagnostics for each statement in parallel if its mostly i/o work
315319
let path_clone = params.path.clone();
316320
let async_results = run_async(async move {
317321
stream::iter(typecheck_params)
318322
.map(|(text, ast, tree, range)| {
319-
let pool = pool_clone.clone();
323+
let pool = pool.clone();
320324
let path = path_clone.clone();
321325
async move {
322326
if let Some(ast) = ast {
@@ -412,6 +416,11 @@ impl Workspace for WorkspaceServer {
412416
&params.position
413417
);
414418

419+
let pool = match self.connection.read().unwrap().get_pool() {
420+
Some(pool) => pool,
421+
None => return Ok(pg_completions::CompletionResult::default()),
422+
};
423+
415424
let doc = self
416425
.documents
417426
.get(&params.path)
@@ -439,7 +448,6 @@ impl Workspace for WorkspaceServer {
439448

440449
tracing::debug!("Found the statement. We're looking for position {:?}. Statement Range {:?} to {:?}. Statement: {}", position, stmt_range.start(), stmt_range.end(), text);
441450

442-
let pool = self.connection.read().unwrap().get_pool();
443451
let schema_cache = self.schema_cache.load(pool)?;
444452

445453
let result = pg_completions::complete(pg_completions::CompletionParams {

crates/pg_workspace/src/workspace/server/db_connection.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::time::Duration;
2+
13
use sqlx::{pool::PoolOptions, postgres::PgConnectOptions, PgPool, Postgres};
24

35
use crate::settings::DatabaseSettings;
@@ -8,11 +10,9 @@ pub struct DbConnection {
810
}
911

1012
impl DbConnection {
11-
/// Requires that you call `set_conn_settings` at least once before getting a pool.
12-
pub(crate) fn get_pool(&self) -> PgPool {
13-
self.pool
14-
.clone()
15-
.expect("The database has never been properly initialized.")
13+
/// There might be no pool available if the user decides to skip db checks.
14+
pub(crate) fn get_pool(&self) -> Option<PgPool> {
15+
self.pool.clone()
1616
}
1717

1818
pub(crate) fn set_conn_settings(&mut self, settings: &DatabaseSettings) {
@@ -27,6 +27,7 @@ impl DbConnection {
2727

2828
let pool = PoolOptions::<Postgres>::new()
2929
.acquire_timeout(timeout)
30+
.acquire_slow_threshold(Duration::from_secs(2))
3031
.connect_lazy_with(config);
3132

3233
self.pool = Some(pool);

0 commit comments

Comments
 (0)