Skip to content

Commit e3b8f78

Browse files
authored
fix(prefer-text-field): allow varchar without a length (#138)
We shouldn't warn about varchar without a length specified since it is equivalent to text, which the docs outline: > The notations varchar(n) and char(n) are aliases for > character varying(n) and character(n), respectively. character without > length specifier is equivalent to character(1). If character varying > is used without length specifier, the type accepts strings of any > size. The latter is a PostgreSQL extension. https://www.postgresql.org/docs/current/datatype-character.html rel: #137
1 parent f6f54d0 commit e3b8f78

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

linter/src/rules/ban_drop_column.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ pub fn ban_drop_column(tree: &[RootStmt]) -> Vec<RuleViolation> {
1818
},
1919
Stmt::AlterTableStmt(stmt) => {
2020
for cmd in &stmt.cmds {
21-
if let AlterTableCmds::AlterTableCmd(cmd) = cmd {
22-
if cmd.subtype == AlterTableType::DropColumn {
23-
errs.push(RuleViolation::new(
24-
RuleViolationKind::BanDropColumn,
25-
raw_stmt.into(),
26-
None,
27-
))
28-
}
21+
let AlterTableCmds::AlterTableCmd(cmd) = cmd;
22+
if cmd.subtype == AlterTableType::DropColumn {
23+
errs.push(RuleViolation::new(
24+
RuleViolationKind::BanDropColumn,
25+
raw_stmt.into(),
26+
None,
27+
))
2928
}
3029
}
3130
}

linter/src/rules/prefer_text_field.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn prefer_text_field(tree: &[RootStmt]) -> Vec<RuleViolation> {
3737
fn check_column_def(errs: &mut Vec<RuleViolation>, raw_stmt: &RawStmt, column_def: &ColumnDef) {
3838
let ColumnDefTypeName::TypeName(type_name) = &column_def.type_name;
3939
for QualifiedName::String(field_type_name) in &type_name.names {
40-
if field_type_name.str == "varchar" {
40+
if field_type_name.str == "varchar" && !type_name.typmods.is_empty() {
4141
errs.push(RuleViolation::new(
4242
RuleViolationKind::PreferTextField,
4343
raw_stmt.into(),
@@ -192,4 +192,13 @@ COMMIT;
192192
assert!(!data.is_empty());
193193
assert_debug_snapshot!(data);
194194
}
195+
196+
#[test]
197+
fn allow_varchar_without_specified_limit() {
198+
let ok_sql = r#"
199+
CREATE TABLE IF NOT EXISTS foo_table(bar_col varchar);
200+
"#;
201+
let res = check_sql(ok_sql, &[]);
202+
assert_eq!(res, Ok(vec![]));
203+
}
195204
}

0 commit comments

Comments
 (0)