Skip to content

Commit f9dc2b0

Browse files
authored
linter: detect invalid uppercase types (#600)
Keywords in PostgreSQL are always case-insensitive[1], therefore `BIGSERIAL` and `BiGsErIaL` types should be considered the same as `bigserial`. [1] https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
1 parent fecb7c8 commit f9dc2b0

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

crates/squawk_linter/src/rules/prefer_identity.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,22 @@ create table users (
6666
);
6767
create table users (
6868
id bigserial
69+
);
70+
create table users (
71+
id BIGSERIAL
6972
);
7073
"#;
7174
let file = squawk_syntax::SourceFile::parse(sql);
7275
let mut linter = Linter::from([Rule::PreferIdentity]);
7376
let errors = linter.lint(file, sql);
7477
assert_ne!(errors.len(), 0);
75-
assert_eq!(errors.len(), 6);
78+
assert_eq!(errors.len(), 7);
7679
assert_eq!(
7780
errors
7881
.iter()
7982
.filter(|x| x.code == Rule::PreferIdentity)
8083
.count(),
81-
6
84+
7
8285
);
8386
assert_debug_snapshot!(errors);
8487
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
source: crates/squawk_linter/src/rules/prefer_identity.rs
3+
assertion_line: 86
34
expression: errors
45
---
56
[
@@ -51,4 +52,12 @@ expression: errors
5152
"Use an `IDENTITY` column instead.",
5253
),
5354
},
55+
Violation {
56+
code: PreferIdentity,
57+
message: "Serial types make schema, dependency, and permission management difficult.",
58+
text_range: 268..277,
59+
help: Some(
60+
"Use an `IDENTITY` column instead.",
61+
),
62+
},
5463
]

crates/squawk_linter/src/visitors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(crate) fn is_not_valid_int_type(ty: &ast::Type, invalid_type_names: &HashSet
2424
return false;
2525
};
2626
let name = trim_quotes(ty_name.as_str());
27-
invalid_type_names.contains(name)
27+
invalid_type_names.contains(name.to_lowercase().as_str())
2828
}
2929
ast::Type::CharType(_) => false,
3030
ast::Type::BitType(_) => false,

0 commit comments

Comments
 (0)