Skip to content

Commit 3a58214

Browse files
authored
fix(dblint): respect --error-on-warnings (#679)
1 parent 6cc978f commit 3a58214

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

crates/pgls_cli/src/commands/dblint.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{CliDiagnostic, CliSession, VcsIntegration};
66
use pgls_analyse::RuleCategoriesBuilder;
77
use pgls_configuration::PartialConfiguration;
88
use pgls_diagnostics::Error;
9+
use pgls_diagnostics::category;
910
use pgls_workspace::features::diagnostics::{PullDatabaseDiagnosticsParams, PullDiagnosticsResult};
1011

1112
pub fn dblint(
@@ -44,5 +45,23 @@ pub fn dblint(
4445
None,
4546
);
4647

47-
session.report("dblint", cli_options, &report)
48+
let exit_result = enforce_exit_codes(cli_options, &report);
49+
session.report("dblint", cli_options, &report)?;
50+
exit_result
51+
}
52+
53+
fn enforce_exit_codes(cli_options: &CliOptions, payload: &Report) -> Result<(), CliDiagnostic> {
54+
let errors = payload.errors;
55+
let warnings = payload.warnings;
56+
let category = category!("check");
57+
58+
if errors > 0 {
59+
return Err(CliDiagnostic::check_error(category));
60+
}
61+
62+
if warnings > 0 && cli_options.error_on_warnings {
63+
return Err(CliDiagnostic::check_warnings(category));
64+
}
65+
66+
Ok(())
4867
}

crates/pgls_cli/tests/assert_dblint.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ async fn dblint_empty_database_snapshot(test_db: PgPool) {
2828
assert_snapshot!(output);
2929
}
3030

31+
#[sqlx::test(migrator = "pgls_test_utils::MIGRATIONS")]
32+
#[cfg_attr(
33+
target_os = "windows",
34+
ignore = "snapshot expectations only validated on unix-like platforms"
35+
)]
36+
async fn dblint_error_on_warnings_empty_database_snapshot(test_db: PgPool) {
37+
let url = get_database_url(&test_db);
38+
let output = run_dblint(&url, &["--error-on-warnings"]);
39+
assert_snapshot!(output);
40+
}
41+
3142
#[sqlx::test(migrator = "pgls_test_utils::MIGRATIONS")]
3243
#[cfg_attr(
3344
target_os = "windows",
@@ -45,6 +56,27 @@ async fn dblint_detects_issues_snapshot(test_db: PgPool) {
4556
assert_snapshot!(output);
4657
}
4758

59+
#[sqlx::test(migrator = "pgls_test_utils::MIGRATIONS")]
60+
#[cfg_attr(
61+
target_os = "windows",
62+
ignore = "snapshot expectations only validated on unix-like platforms"
63+
)]
64+
async fn dblint_error_on_warnings_detects_issues_snapshot(test_db: PgPool) {
65+
// Setup: create duplicate non-unique indexes (triggers duplicateIndex warning)
66+
sqlx::raw_sql(
67+
"CREATE TABLE test_duplicate_idx (id int primary key, email text);
68+
CREATE INDEX idx_duplicate_a ON test_duplicate_idx (email);
69+
CREATE INDEX idx_duplicate_b ON test_duplicate_idx (email);",
70+
)
71+
.execute(&test_db)
72+
.await
73+
.expect("Failed to create test schema");
74+
75+
let url = get_database_url(&test_db);
76+
let output = run_dblint(&url, &["--error-on-warnings"]);
77+
assert_snapshot!(output);
78+
}
79+
4880
#[test]
4981
#[cfg_attr(
5082
target_os = "windows",
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
source: crates/pgls_cli/tests/assert_dblint.rs
3+
expression: output
4+
snapshot_kind: text
5+
---
6+
status: failure
7+
stdout:
8+
Command completed in <duration>.
9+
Found 1 warning(s).
10+
stderr:
11+
splinter/performance/duplicateIndex ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
12+
13+
! Table \`public.test_duplicate_idx\` has identical indexes {idx_duplicate_a,idx_duplicate_b}. Drop all except one of them
14+
15+
Detects cases where two ore more identical indexes exist.
16+
17+
i table: public.test_duplicate_idx
18+
19+
{"indexes":["idx_duplicate_a","idx_duplicate_b"]}
20+
21+
i Remediation: https://supabase.com/docs/guides/database/database-linter?lint=0009_duplicate_index
22+
23+
24+
splinter/performance/unusedIndex ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
25+
26+
i Index \`idx_duplicate_a\` on table \`public.test_duplicate_idx\` has not been used
27+
28+
Detects if an index has never been used and may be a candidate for removal.
29+
30+
i table: public.test_duplicate_idx
31+
32+
i Remediation: https://supabase.com/docs/guides/database/database-linter?lint=0005_unused_index
33+
34+
35+
splinter/performance/unusedIndex ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
36+
37+
i Index \`idx_duplicate_b\` on table \`public.test_duplicate_idx\` has not been used
38+
39+
Detects if an index has never been used and may be a candidate for removal.
40+
41+
i table: public.test_duplicate_idx
42+
43+
i Remediation: https://supabase.com/docs/guides/database/database-linter?lint=0005_unused_index
44+
45+
46+
check ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
47+
48+
× Some warnings were emitted while running checks.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: crates/pgls_cli/tests/assert_dblint.rs
3+
expression: output
4+
snapshot_kind: text
5+
---
6+
status: success
7+
stdout:
8+
Command completed in <duration>.
9+
stderr:

0 commit comments

Comments
 (0)