Skip to content

Commit 20162f5

Browse files
authored
rule: add back transaction nesting to v2 (#450)
wasn't ported from v1, oops
1 parent 0f406ec commit 20162f5

12 files changed

+430
-8
lines changed

.vscode/linter.code-snippets

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
// Place your tea workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
3+
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
4+
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
5+
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
6+
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
7+
// Placeholders with the same ids are connected.
8+
// Example:
9+
// "Print to console": {
10+
// "scope": "javascript,typescript",
11+
// "prefix": "log",
12+
// "body": [
13+
// "console.log('$1');",
14+
// "$2"
15+
// ],
16+
// "description": "Log output to console"
17+
// }
18+
19+
"linttest": {
20+
"scope": "rust",
21+
"prefix": "linttest",
22+
"body": [
23+
"#[test]",
24+
"fn $1() {",
25+
" let sql = r#\"",
26+
"$2",
27+
" \"#;",
28+
"",
29+
" let file = SourceFile::parse(&sql);",
30+
" let mut linter = Linter::new([Rule::$3]);",
31+
" let errors = linter.lint(file, sql);",
32+
" assert_debug_snapshot!(errors);",
33+
"}",
34+
35+
]
36+
}
37+
}

.vscode/syntax.code-snippets

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
// Place your tea workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
3+
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
4+
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
5+
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
6+
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
7+
// Placeholders with the same ids are connected.
8+
// Example:
9+
// "Print to console": {
10+
// "scope": "javascript,typescript",
11+
// "prefix": "log",
12+
// "body": [
13+
// "console.log('$1');",
14+
// "$2"
15+
// ],
16+
// "description": "Log output to console"
17+
// }
18+
"astnode": {
19+
"scope": "rust",
20+
"prefix": "astnode",
21+
"body": [
22+
23+
"#[derive(Debug, Clone, PartialEq, Eq, Hash)]",
24+
"pub struct $1 {",
25+
" pub(crate) syntax: SyntaxNode,",
26+
"}",
27+
"",
28+
"impl AstNode for $1 {",
29+
" #[inline]",
30+
" fn can_cast(kind: SyntaxKind) -> bool {",
31+
" kind == SyntaxKind::$2",
32+
" }",
33+
" #[inline]",
34+
" fn cast(syntax: SyntaxNode) -> Option<Self> {",
35+
" if Self::can_cast(syntax.kind()) {",
36+
" Some(Self { syntax })",
37+
" } else {",
38+
" None",
39+
" }",
40+
" }",
41+
" #[inline]",
42+
" fn syntax(&self) -> &SyntaxNode {",
43+
" &self.syntax",
44+
" }",
45+
"}",
46+
47+
]
48+
},
49+
"astenum": {
50+
"scope": "rust",
51+
"prefix": "astenum",
52+
"body": [
53+
"#[derive(Debug, Clone, PartialEq, Eq, Hash)]",
54+
"pub enum $1 {",
55+
" $3($3),",
56+
"}",
57+
"",
58+
"impl AstNode for $1 {",
59+
" #[inline]",
60+
" fn can_cast(kind: SyntaxKind) -> bool {",
61+
" matches!(kind, SyntaxKind::$2)",
62+
" }",
63+
" #[inline]",
64+
" fn cast(syntax: SyntaxNode) -> Option<Self> {",
65+
" let res = match syntax.kind() {",
66+
" SyntaxKind::DEFAULT_CONSTRAINT => {",
67+
" $1::$3($3 { syntax })",
68+
" }",
69+
" _ => return None,",
70+
" };",
71+
" Some(res)",
72+
" }",
73+
" #[inline]",
74+
" fn syntax(&self) -> &SyntaxNode {",
75+
" match self {",
76+
" $1::$3(it) => &it.syntax,",
77+
" }",
78+
" }",
79+
"}",
80+
81+
]
82+
}
83+
}

crates/squawk_linter/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use rules::renaming_column;
4747
use rules::renaming_table;
4848
use rules::require_concurrent_index_creation;
4949
use rules::require_concurrent_index_deletion;
50+
use rules::transaction_nesting;
5051
// xtask:new-lint:rule-import
5152

5253
#[derive(Debug, PartialEq, Clone, Copy, Serialize, Hash, Eq, Deserialize, Sequence)]
@@ -340,6 +341,9 @@ impl Linter {
340341
if self.rules.contains(&Rule::BanAlterDomainWithAddConstraint) {
341342
ban_alter_domain_with_add_constraint(self, &file);
342343
}
344+
if self.rules.contains(&Rule::TransactionNesting) {
345+
transaction_nesting(self, &file);
346+
}
343347
// xtask:new-lint:rule-call
344348

345349
// locate any ignores in the file

crates/squawk_linter/src/rules/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub(crate) mod renaming_column;
2424
pub(crate) mod renaming_table;
2525
pub(crate) mod require_concurrent_index_creation;
2626
pub(crate) mod require_concurrent_index_deletion;
27+
pub(crate) mod transaction_nesting;
2728
// xtask:new-lint:mod-decl
2829

2930
pub(crate) use adding_field_with_default::adding_field_with_default;
@@ -52,4 +53,5 @@ pub(crate) use renaming_column::renaming_column;
5253
pub(crate) use renaming_table::renaming_table;
5354
pub(crate) use require_concurrent_index_creation::require_concurrent_index_creation;
5455
pub(crate) use require_concurrent_index_deletion::require_concurrent_index_deletion;
56+
pub(crate) use transaction_nesting::transaction_nesting;
5557
// xtask:new-lint:export
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
source: crates/squawk_linter/src/rules/transaction_nesting.rs
3+
expression: errors
4+
---
5+
[
6+
Violation {
7+
code: TransactionNesting,
8+
message: "There is an existing transaction already in progress, managed by your migration tool.",
9+
text_range: 1..6,
10+
help: Some(
11+
"Put migration statements in separate files to have them be in separate transactions or don't use the assume-in-transaction setting.",
12+
),
13+
},
14+
Violation {
15+
code: TransactionNesting,
16+
message: "There is an existing transaction already in progress, managed by your migration tool.",
17+
text_range: 8..13,
18+
help: Some(
19+
"Put migration statements in separate files to have them be in separate transactions or don't use the assume-in-transaction setting.",
20+
),
21+
},
22+
Violation {
23+
code: TransactionNesting,
24+
message: "Attempting to end the transaction that is managed by your migration tool",
25+
text_range: 25..31,
26+
help: Some(
27+
"Put migration statements in separate files to have them be in separate transactions or don't use the assume-in-transaction setting.",
28+
),
29+
},
30+
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
source: crates/squawk_linter/src/rules/transaction_nesting.rs
3+
expression: errors
4+
---
5+
[
6+
Violation {
7+
code: TransactionNesting,
8+
message: "There is an existing transaction already in progress.",
9+
text_range: 8..13,
10+
help: Some(
11+
"Put migration statements in separate files to have them be in separate transactions or don't use the assume-in-transaction setting.",
12+
),
13+
},
14+
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
source: crates/squawk_linter/src/rules/transaction_nesting.rs
3+
expression: errors
4+
---
5+
[
6+
Violation {
7+
code: TransactionNesting,
8+
message: "There is no transaction to `COMMIT` or `ROLLBACK`.",
9+
text_range: 26..32,
10+
help: Some(
11+
"`BEGIN` a transaction at an earlier point in the migration or remove this statement.",
12+
),
13+
},
14+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
source: crates/squawk_linter/src/rules/transaction_nesting.rs
3+
expression: errors
4+
---
5+
[
6+
Violation {
7+
code: TransactionNesting,
8+
message: "Attempting to end the transaction that is managed by your migration tool",
9+
text_range: 11..17,
10+
help: Some(
11+
"Put migration statements in separate files to have them be in separate transactions or don't use the assume-in-transaction setting.",
12+
),
13+
},
14+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
source: crates/squawk_linter/src/rules/transaction_nesting.rs
3+
expression: errors
4+
---
5+
[
6+
Violation {
7+
code: TransactionNesting,
8+
message: "Attempting to end the transaction that is managed by your migration tool",
9+
text_range: 92..100,
10+
help: Some(
11+
"Put migration statements in separate files to have them be in separate transactions or don't use the assume-in-transaction setting.",
12+
),
13+
},
14+
]

0 commit comments

Comments
 (0)