Skip to content

Commit 2820bb5

Browse files
fix: do not attempt db connection if jsonc section is missing (#375)
* fix: do not attempt db connection if jsonc section is missing * comment * comment * whoopsndoopsn * whoopsndoopsn2 * whoopsndoopsn 3 * ok * ok * just readied * Update postgrestools.jsonc * ok * finally
1 parent e2fae69 commit 2820bb5

File tree

14 files changed

+58
-24
lines changed

14 files changed

+58
-24
lines changed

crates/pgt_cli/src/cli_options.rs

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

21-
/// Skip connecting to the database and only run checks that don't require a database connection.
22-
#[bpaf(long("skip-db"), switch, fallback(false))]
23-
pub skip_db: bool,
24-
2521
/// Print additional diagnostics, and some diagnostics show more information. Also, print out what files were processed and which ones were modified.
2622
#[bpaf(long("verbose"), switch, fallback(false))]
2723
pub verbose: bool,

crates/pgt_cli/src/commands/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,6 @@ pub(crate) trait CommandRunner: Sized {
307307
configuration,
308308
vcs_base_path,
309309
gitignore_matches,
310-
skip_db: cli_options.skip_db,
311310
})?;
312311

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

crates/pgt_configuration/src/database.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use serde::{Deserialize, Serialize};
1010
#[partial(serde(rename_all = "camelCase", default, deny_unknown_fields))]
1111
pub struct DatabaseConfiguration {
1212
/// The host of the database.
13+
/// Required if you want database-related features.
14+
/// All else falls back to sensible defaults.
1315
#[partial(bpaf(long("host")))]
1416
pub host: String,
1517

@@ -35,11 +37,17 @@ pub struct DatabaseConfiguration {
3537
/// The connection timeout in seconds.
3638
#[partial(bpaf(long("conn_timeout_secs"), fallback(Some(10)), debug_fallback))]
3739
pub conn_timeout_secs: u16,
40+
41+
/// Actively disable all database-related features.
42+
#[partial(bpaf(long("disable-db"), switch, fallback(Some(false))))]
43+
#[partial(cfg_attr(feature = "schema", schemars(skip)))]
44+
pub disable_connection: bool,
3845
}
3946

4047
impl Default for DatabaseConfiguration {
4148
fn default() -> Self {
4249
Self {
50+
disable_connection: false,
4351
host: "127.0.0.1".to_string(),
4452
port: 5432,
4553
username: "postgres".to_string(),

crates/pgt_configuration/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ impl PartialConfiguration {
110110
username: Some("postgres".to_string()),
111111
password: Some("postgres".to_string()),
112112
database: Some("postgres".to_string()),
113-
conn_timeout_secs: Some(10),
114113
allow_statement_executions_against: Default::default(),
114+
conn_timeout_secs: Some(10),
115+
disable_connection: Some(false),
115116
}),
116117
}
117118
}

crates/pgt_lsp/src/session.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,6 @@ impl Session {
449449
configuration: fs_configuration,
450450
vcs_base_path,
451451
gitignore_matches,
452-
skip_db: false,
453452
});
454453

455454
if let Err(error) = result {

crates/pgt_lsp/tests/server.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,14 +773,15 @@ async fn test_execute_statement() -> Result<()> {
773773
.to_string();
774774
let host = test_db.connect_options().get_host().to_string();
775775

776-
let conf = PartialConfiguration {
776+
let mut conf = PartialConfiguration::init();
777+
conf.merge_with(PartialConfiguration {
777778
db: Some(PartialDatabaseConfiguration {
778779
database: Some(database),
779780
host: Some(host),
780781
..Default::default()
781782
}),
782783
..Default::default()
783-
};
784+
});
784785

785786
fs.insert(
786787
url!("postgrestools.jsonc").to_file_path().unwrap(),

crates/pgt_workspace/src/settings.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ impl Default for LinterSettings {
268268
/// Database settings for the entire workspace
269269
#[derive(Debug)]
270270
pub struct DatabaseSettings {
271+
pub enable_connection: bool,
271272
pub host: String,
272273
pub port: u16,
273274
pub username: String,
@@ -280,6 +281,7 @@ pub struct DatabaseSettings {
280281
impl Default for DatabaseSettings {
281282
fn default() -> Self {
282283
Self {
284+
enable_connection: false,
283285
host: "127.0.0.1".to_string(),
284286
port: 5432,
285287
username: "postgres".to_string(),
@@ -295,6 +297,13 @@ impl From<PartialDatabaseConfiguration> for DatabaseSettings {
295297
fn from(value: PartialDatabaseConfiguration) -> Self {
296298
let d = DatabaseSettings::default();
297299

300+
// "host" is the minimum required setting for database features
301+
// to be enabled.
302+
let enable_connection = value
303+
.host
304+
.as_ref()
305+
.is_some_and(|_| value.disable_connection.is_none_or(|disabled| !disabled));
306+
298307
let database = value.database.unwrap_or(d.database);
299308
let host = value.host.unwrap_or(d.host);
300309

@@ -312,6 +321,8 @@ impl From<PartialDatabaseConfiguration> for DatabaseSettings {
312321
.unwrap_or(false);
313322

314323
Self {
324+
enable_connection,
325+
315326
port: value.port.unwrap_or(d.port),
316327
username: value.username.unwrap_or(d.username),
317328
password: value.password.unwrap_or(d.password),

crates/pgt_workspace/src/workspace.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ pub struct UpdateSettingsParams {
7373
pub vcs_base_path: Option<PathBuf>,
7474
pub gitignore_matches: Vec<String>,
7575
pub workspace_directory: Option<PathBuf>,
76-
pub skip_db: bool,
7776
}
7877

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

crates/pgt_workspace/src/workspace/server.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,10 @@ impl Workspace for WorkspaceServer {
168168

169169
tracing::info!("Updated settings in workspace");
170170

171-
if !params.skip_db {
172-
self.connection
173-
.write()
174-
.unwrap()
175-
.set_conn_settings(&self.settings().as_ref().db);
176-
}
171+
self.connection
172+
.write()
173+
.unwrap()
174+
.set_conn_settings(&self.settings().as_ref().db);
177175

178176
tracing::info!("Updated Db connection settings");
179177

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ impl DbConnection {
1616
}
1717

1818
pub(crate) fn set_conn_settings(&mut self, settings: &DatabaseSettings) {
19+
if !settings.enable_connection {
20+
tracing::info!("Database connection disabled.");
21+
return;
22+
}
23+
1924
let config = PgConnectOptions::new()
2025
.host(&settings.host)
2126
.port(settings.port)

0 commit comments

Comments
 (0)