Skip to content

Commit 2820bb5

Browse files
authoredApr 25, 2025
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

-4
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

-1
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

+8
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

+2-1
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

-1
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

+3-2
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

+11
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

-1
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

+4-6
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

+5
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)

‎docs/schemas/0.0.0/schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
]
101101
},
102102
"host": {
103-
"description": "The host of the database.",
103+
"description": "The host of the database. Required if you want database-related features. All else falls back to sensible defaults.",
104104
"type": [
105105
"string",
106106
"null"

‎docs/schemas/latest/schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
]
101101
},
102102
"host": {
103-
"description": "The host of the database.",
103+
"description": "The host of the database. Required if you want database-related features. All else falls back to sensible defaults.",
104104
"type": [
105105
"string",
106106
"null"

‎justfile

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ serve-docs:
100100
uv run mkdocs serve
101101

102102
# When you finished coding, run this command. Note that you should have already committed your changes.
103+
# If you haven't run `sqlx prepare` at least once, you need to run `docker compose up`
104+
# to lint the queries.
103105
ready:
104106
git diff --exit-code --quiet
105107
cargo run -p xtask_codegen -- configuration

‎packages/@postgrestools/backend-jsonrpc/src/workspace.ts

+21-6
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,36 @@ export interface GetCompletionsParams {
179179
*/
180180
position: TextSize;
181181
}
182-
export interface CompletionResult {
182+
export interface CompletionsResult {
183183
items: CompletionItem[];
184184
}
185185
export interface CompletionItem {
186+
completion_text?: CompletionText;
186187
description: string;
187188
kind: CompletionItemKind;
188189
label: string;
189190
preselected: boolean;
190-
score: number;
191+
/**
192+
* String used for sorting by LSP clients.
193+
*/
194+
sort_text: string;
195+
}
196+
/**
197+
* The text that the editor should fill in. If `None`, the `label` should be used. Tables, for example, might have different completion_texts:
198+
199+
label: "users", description: "Schema: auth", completion_text: "auth.users".
200+
*/
201+
export interface CompletionText {
202+
/**
203+
* A `range` is required because some editors replace the current token, others naively insert the text. Having a range where start == end makes it an insertion.
204+
*/
205+
range: TextRange;
206+
text: string;
191207
}
192-
export type CompletionItemKind = "table" | "function" | "column";
208+
export type CompletionItemKind = "table" | "function" | "column" | "schema";
193209
export interface UpdateSettingsParams {
194210
configuration: PartialConfiguration;
195211
gitignore_matches: string[];
196-
skip_db: boolean;
197212
vcs_base_path?: string;
198213
workspace_directory?: string;
199214
}
@@ -240,7 +255,7 @@ export interface PartialDatabaseConfiguration {
240255
*/
241256
database?: string;
242257
/**
243-
* The host of the database.
258+
* The host of the database. Required if you want database-related features. All else falls back to sensible defaults.
244259
*/
245260
host?: string;
246261
/**
@@ -414,7 +429,7 @@ export interface Workspace {
414429
pullDiagnostics(
415430
params: PullDiagnosticsParams,
416431
): Promise<PullDiagnosticsResult>;
417-
getCompletions(params: GetCompletionsParams): Promise<CompletionResult>;
432+
getCompletions(params: GetCompletionsParams): Promise<CompletionsResult>;
418433
updateSettings(params: UpdateSettingsParams): Promise<void>;
419434
openFile(params: OpenFileParams): Promise<void>;
420435
changeFile(params: ChangeFileParams): Promise<void>;

0 commit comments

Comments
 (0)