Skip to content

Commit 556f0d3

Browse files
authored
linter: improve prefer-robust-stmts error messages (#461)
also update internal vscode extension readme and fix typo in playground linter source name `tea` instead of `squawk`
1 parent 6412404 commit 556f0d3

10 files changed

+85
-13
lines changed

.vscode/extensions/squawk-dev/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22

33
Extension to make developing squawk a little nicer.
44

5+
## Install
6+
7+
1. install deps and build
8+
9+
```sh
10+
cd .vscode/extensions/squawk-dev
11+
pnpm install
12+
pnpm run compile
13+
```
14+
15+
2. enable extension in vscode
16+
17+
In the command palette run:
18+
19+
```
20+
Extensions: Show Recommended Extensions
21+
```
22+
23+
You should then see the `recipeyak-dev` extension in the results.
24+
25+
Press the `Install Workspace Extension` button.
26+
27+
Done!
28+
529
## Features
630

731
### Jump from snapshot to source file

crates/squawk_linter/src/rules/prefer_robust_stmts.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ pub(crate) fn prefer_robust_stmts(ctx: &mut Linter, parse: &Parse<SourceFile>) {
3030
return;
3131
}
3232

33+
enum ActionErrorMessage {
34+
IfExists,
35+
IfNotExists,
36+
None,
37+
}
38+
3339
for item in file.items() {
3440
match item {
3541
ast::Item::Begin(_) => {
@@ -40,7 +46,7 @@ pub(crate) fn prefer_robust_stmts(ctx: &mut Linter, parse: &Parse<SourceFile>) {
4046
}
4147
ast::Item::AlterTable(alter_table) => {
4248
for action in alter_table.actions() {
43-
match &action {
49+
let message_type = match &action {
4450
ast::AlterTableAction::DropConstraint(drop_constraint) => {
4551
if let Some(constraint_name) = drop_constraint.name_ref() {
4652
constraint_names.insert(
@@ -51,11 +57,13 @@ pub(crate) fn prefer_robust_stmts(ctx: &mut Linter, parse: &Parse<SourceFile>) {
5157
if drop_constraint.if_exists().is_some() {
5258
continue;
5359
}
60+
ActionErrorMessage::IfExists
5461
}
5562
ast::AlterTableAction::AddColumn(add_column) => {
5663
if add_column.if_not_exists().is_some() {
5764
continue;
5865
}
66+
ActionErrorMessage::IfNotExists
5967
}
6068
ast::AlterTableAction::ValidateConstraint(validate_constraint) => {
6169
if let Some(constraint_name) = validate_constraint.name_ref() {
@@ -65,6 +73,7 @@ pub(crate) fn prefer_robust_stmts(ctx: &mut Linter, parse: &Parse<SourceFile>) {
6573
continue;
6674
}
6775
}
76+
ActionErrorMessage::None
6877
}
6978
ast::AlterTableAction::AddConstraint(add_constraint) => {
7079
let constraint = add_constraint.constraint();
@@ -78,22 +87,36 @@ pub(crate) fn prefer_robust_stmts(ctx: &mut Linter, parse: &Parse<SourceFile>) {
7887
}
7988
}
8089
}
90+
ActionErrorMessage::None
8191
}
8292
ast::AlterTableAction::DropColumn(drop_column) => {
8393
if drop_column.if_exists().is_some() {
8494
continue;
8595
}
96+
ActionErrorMessage::IfExists
8697
}
87-
_ => (),
88-
}
98+
_ => ActionErrorMessage::None,
99+
};
89100

90101
if inside_transaction {
91102
continue;
92103
}
93104

105+
let message = match message_type {
106+
ActionErrorMessage::IfExists => {
107+
"Missing `IF EXISTS`, the migration can't be rerun if it fails part way through.".to_string()
108+
},
109+
ActionErrorMessage::IfNotExists => {
110+
"Missing `IF NOT EXISTS`, the migration can't be rerun if it fails part way through.".to_string()
111+
},
112+
ActionErrorMessage::None => {
113+
"Missing transaction, the migration can't be rerun if it fails part way through.".to_string()
114+
},
115+
};
116+
94117
ctx.report(Violation::new(
95118
Rule::PreferRobustStmts,
96-
"Missing `IF NOT EXISTS`, the migration can't be rerun if it fails part way through.".into(),
119+
message,
97120
action.syntax().text_range(),
98121
None,
99122
));
@@ -547,7 +570,20 @@ ALTER TABLE IF EXISTS test DISABLE ROW LEVEL SECURITY;
547570
ALTER TABLE "app_email" DROP CONSTRAINT IF EXISTS "email_uniq";
548571
ALTER TABLE "app_email" ADD CONSTRAINT "email_uniq" UNIQUE USING INDEX "email_idx";
549572
-- this second add constraint should error because it's not robust
550-
ALTER TABLE "app_email" ADD CONSTRAINT "email_uniq" UNIQUE USING INDEX "email_idx";
573+
ALTER TABLE "app_email" ADD CONSTRAIN "email_uniq" UNIQUE USING INDEX "email_idx";
574+
"#;
575+
let file = squawk_syntax::SourceFile::parse(sql);
576+
let mut linter = Linter::from([Rule::PreferRobustStmts]);
577+
let errors = linter.lint(file, sql);
578+
assert_ne!(errors.len(), 0);
579+
assert_debug_snapshot!(errors);
580+
}
581+
582+
#[test]
583+
fn alter_column_set_not_null() {
584+
let sql = r#"
585+
select 1; -- so we don't skip checking
586+
alter table t alter column c set not null;
551587
"#;
552588
let file = squawk_syntax::SourceFile::parse(sql);
553589
let mut linter = Linter::from([Rule::PreferRobustStmts]);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: crates/squawk_linter/src/rules/prefer_robust_stmts.rs
3+
expression: errors
4+
---
5+
[
6+
Violation {
7+
code: PreferRobustStmts,
8+
message: "Missing transaction, the migration can't be rerun if it fails part way through.",
9+
text_range: 54..81,
10+
help: None,
11+
},
12+
]

crates/squawk_linter/src/rules/snapshots/squawk_linter__rules__prefer_robust_stmts__test__alter_table_drop_column_err.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ expression: errors
55
[
66
Violation {
77
code: PreferRobustStmts,
8-
message: "Missing `IF NOT EXISTS`, the migration can't be rerun if it fails part way through.",
8+
message: "Missing `IF EXISTS`, the migration can't be rerun if it fails part way through.",
99
text_range: 54..75,
1010
help: None,
1111
},

crates/squawk_linter/src/rules/snapshots/squawk_linter__rules__prefer_robust_stmts__test__alter_table_drop_constraint_err.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ expression: errors
55
[
66
Violation {
77
code: PreferRobustStmts,
8-
message: "Missing `IF NOT EXISTS`, the migration can't be rerun if it fails part way through.",
8+
message: "Missing `IF EXISTS`, the migration can't be rerun if it fails part way through.",
99
text_range: 63..93,
1010
help: None,
1111
},

crates/squawk_linter/src/rules/snapshots/squawk_linter__rules__prefer_robust_stmts__test__disable_row_level_security_err.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ expression: errors
55
[
66
Violation {
77
code: PreferRobustStmts,
8-
message: "Missing `IF NOT EXISTS`, the migration can't be rerun if it fails part way through.",
8+
message: "Missing transaction, the migration can't be rerun if it fails part way through.",
99
text_range: 63..89,
1010
help: None,
1111
},

crates/squawk_linter/src/rules/snapshots/squawk_linter__rules__prefer_robust_stmts__test__double_add_after_drop_err.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ expression: errors
66
Violation {
77
code: PreferRobustStmts,
88
message: "Missing `IF NOT EXISTS`, the migration can't be rerun if it fails part way through.",
9-
text_range: 240..298,
9+
text_range: 240..297,
1010
help: None,
1111
},
1212
]

crates/squawk_linter/src/rules/snapshots/squawk_linter__rules__prefer_robust_stmts__test__enable_row_level_security_err.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ expression: errors
55
[
66
Violation {
77
code: PreferRobustStmts,
8-
message: "Missing `IF NOT EXISTS`, the migration can't be rerun if it fails part way through.",
8+
message: "Missing transaction, the migration can't be rerun if it fails part way through.",
99
text_range: 63..88,
1010
help: None,
1111
},

crates/squawk_linter/src/rules/snapshots/squawk_linter__rules__prefer_robust_stmts__test__enable_row_level_security_without_exists_check_err.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ expression: errors
55
[
66
Violation {
77
code: PreferRobustStmts,
8-
message: "Missing `IF NOT EXISTS`, the migration can't be rerun if it fails part way through.",
8+
message: "Missing transaction, the migration can't be rerun if it fails part way through.",
99
text_range: 53..78,
1010
help: None,
1111
},

playground/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ function Editor({
246246
}
247247
const model = editorRef.current?.getModel()
248248
if (model != null) {
249-
monaco.editor.setModelMarkers(model, "tea", markers)
249+
monaco.editor.setModelMarkers(model, "squawk", markers)
250250
}
251251
}, [markers])
252252

@@ -375,7 +375,7 @@ function useMarkers(text: string): Array<Marker> {
375375
// https://github.com/microsoft/monaco-editor/issues/1264
376376
// https://stackoverflow.com/questions/62362741/using-markdown-in-imarkerdata
377377
message: x.message,
378-
source: "tea",
378+
source: "squawk",
379379
}
380380
})
381381
}

0 commit comments

Comments
 (0)