Skip to content

Commit 388577c

Browse files
authored
linter: fix false positive with create temp table t() on commit drop; (#739)
rel: #738
1 parent 4f48877 commit 388577c

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

crates/squawk_linter/src/rules/prefer_robust_stmts.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,17 @@ pub(crate) fn prefer_robust_stmts(ctx: &mut Linter, parse: &Parse<SourceFile>) {
157157
ast::Stmt::CreateTable(create_table)
158158
if create_table.if_not_exists().is_none() && !inside_transaction =>
159159
{
160+
let is_temp =
161+
create_table.temp_token().is_some() || create_table.temporary_token().is_some();
162+
let on_commit_drop = create_table
163+
.on_commit()
164+
.and_then(|oc| oc.on_commit_action())
165+
.is_some_and(|action| matches!(action, ast::OnCommitAction::Drop(_)));
166+
167+
if is_temp && on_commit_drop {
168+
continue;
169+
}
170+
160171
let fix = create_table.table_token().map(|table_token| {
161172
let at = table_token.text_range().end();
162173
let edit = Edit::insert(" if not exists", at);
@@ -669,4 +680,46 @@ DROP INDEX CONCURRENTLY "email_idx";
669680
assert_ne!(errors.len(), 0);
670681
assert_debug_snapshot!(errors);
671682
}
683+
684+
#[test]
685+
fn create_temp_table_on_commit_drop_ok() {
686+
let sql = r#"
687+
select 1; -- so we don't skip checking
688+
CREATE TEMP TABLE test_table (id int) ON COMMIT DROP;
689+
"#;
690+
let errors = lint(sql, Rule::PreferRobustStmts);
691+
assert_eq!(errors.len(), 0);
692+
}
693+
694+
#[test]
695+
fn create_temporary_table_on_commit_drop_ok() {
696+
let sql = r#"
697+
select 1; -- so we don't skip checking
698+
CREATE TEMPORARY TABLE test_table (id int) ON COMMIT DROP;
699+
"#;
700+
let errors = lint(sql, Rule::PreferRobustStmts);
701+
assert_eq!(errors.len(), 0);
702+
}
703+
704+
#[test]
705+
fn create_temp_table_without_on_commit_drop_err() {
706+
let sql = r#"
707+
select 1; -- so we don't skip checking
708+
CREATE TEMP TABLE test_table (id int);
709+
"#;
710+
let errors = lint(sql, Rule::PreferRobustStmts);
711+
assert_ne!(errors.len(), 0);
712+
assert_debug_snapshot!(errors);
713+
}
714+
715+
#[test]
716+
fn create_table_with_on_commit_drop_err() {
717+
let sql = r#"
718+
select 1; -- so we don't skip checking
719+
CREATE TABLE test_table (id int) ON COMMIT DROP;
720+
"#;
721+
let errors = lint(sql, Rule::PreferRobustStmts);
722+
assert_ne!(errors.len(), 0);
723+
assert_debug_snapshot!(errors);
724+
}
672725
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 `IF NOT EXISTS`, the migration can't be rerun if it fails part way through.",
9+
text_range: 40..87,
10+
help: None,
11+
fix: Some(
12+
Fix {
13+
title: "Insert `if not exists`",
14+
edits: [
15+
Edit {
16+
text_range: 52..52,
17+
text: Some(
18+
" if not exists",
19+
),
20+
},
21+
],
22+
},
23+
),
24+
},
25+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 `IF NOT EXISTS`, the migration can't be rerun if it fails part way through.",
9+
text_range: 40..77,
10+
help: None,
11+
fix: Some(
12+
Fix {
13+
title: "Insert `if not exists`",
14+
edits: [
15+
Edit {
16+
text_range: 57..57,
17+
text: Some(
18+
" if not exists",
19+
),
20+
},
21+
],
22+
},
23+
),
24+
},
25+
]

0 commit comments

Comments
 (0)