Skip to content

Commit

Permalink
add new "ban-drop-column" rule (#132)
Browse files Browse the repository at this point in the history
fixes #128
  • Loading branch information
chdsbd authored Apr 21, 2021
1 parent 6fecfd1 commit f6f54d0
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- link to website for tty reporter when there are lint errors. (#120)
- `DROP INDEX` support for "require-concurrent-index-creation" and "prefer-robust-stmts". (#124)
- github comment now includes Squawk's version number. (#131)
- new `ban-drop-column` rule (#132)

### Changed

Expand Down
6 changes: 6 additions & 0 deletions docs/docs/ban-drop-column.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
id: ban-drop-column
title: ban-drop-column
---

Dropping a column may break existing clients.
19 changes: 15 additions & 4 deletions linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ extern crate lazy_static;
use crate::errors::CheckSQLError;
use crate::rules::{
adding_field_with_default, adding_foreign_key_constraint, adding_not_nullable_field,
adding_primary_key_constraint, ban_char_type, ban_drop_database, changing_column_type,
constraint_missing_not_valid, disallow_unique_constraint, prefer_robust_stmts,
prefer_text_field, renaming_column, renaming_table, require_concurrent_index_creation,
adding_primary_key_constraint, ban_char_type, ban_drop_column, ban_drop_database,
changing_column_type, constraint_missing_not_valid, disallow_unique_constraint,
prefer_robust_stmts, prefer_text_field, renaming_column, renaming_table,
require_concurrent_index_creation,
};
use crate::violations::{RuleViolation, RuleViolationKind, ViolationMessage};
use squawk_parser::ast::RootStmt;
Expand Down Expand Up @@ -92,6 +93,16 @@ lazy_static! {
),
]
},
SquawkRule {
id: "ban-drop-column".into(),
name: RuleViolationKind::BanDropColumn,
func: ban_drop_column,
messages: vec![
ViolationMessage::Note(
"Dropping a column may break existing clients.".into()
),
],
},
SquawkRule {
id: "ban-drop-database".into(),
name: RuleViolationKind::BanDropDatabase,
Expand Down Expand Up @@ -234,7 +245,7 @@ lazy_static! {
"Create the index CONCURRENTLY.".into()
),
],
},
}
];

}
Expand Down
51 changes: 51 additions & 0 deletions linter/src/rules/ban_drop_column.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::violations::{RuleViolation, RuleViolationKind};
use squawk_parser::ast::{AlterTableCmds, AlterTableType, ObjectType, RootStmt, Stmt};

#[must_use]
pub fn ban_drop_column(tree: &[RootStmt]) -> Vec<RuleViolation> {
let mut errs = vec![];
for RootStmt::RawStmt(raw_stmt) in tree {
match &raw_stmt.stmt {
Stmt::RenameStmt(stmt) => match stmt.rename_type {
ObjectType::Table => {
errs.push(RuleViolation::new(
RuleViolationKind::RenamingTable,
raw_stmt.into(),
None,
));
}
_ => continue,
},
Stmt::AlterTableStmt(stmt) => {
for cmd in &stmt.cmds {
if let AlterTableCmds::AlterTableCmd(cmd) = cmd {
if cmd.subtype == AlterTableType::DropColumn {
errs.push(RuleViolation::new(
RuleViolationKind::BanDropColumn,
raw_stmt.into(),
None,
))
}
}
}
}
_ => continue,
}
}
errs
}

#[cfg(test)]
mod test_rules {
use crate::check_sql;
use insta::assert_debug_snapshot;

#[test]
fn test_drop_column() {
let sql = r#"
ALTER TABLE "bar_tbl" DROP COLUMN "foo_col" CASCADE;
"#;

assert_debug_snapshot!(check_sql(sql, &["prefer-robust-stmts".into()]));
}
}
2 changes: 2 additions & 0 deletions linter/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub mod renaming_column;
pub use renaming_column::*;
pub mod renaming_table;
pub use renaming_table::*;
pub mod ban_drop_column;
pub use ban_drop_column::*;
pub mod require_concurrent_index_creation;
pub use require_concurrent_index_creation::*;
pub mod ban_char_field;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: linter/src/rules/ban_drop_column.rs
expression: "check_sql(sql, &[\"prefer-robust-stmts\".into()])"
---
Ok(
[
RuleViolation {
kind: BanDropColumn,
span: Span {
start: 0,
len: Some(
52,
),
},
messages: [
Note(
"Dropping a column may break existing clients.",
),
],
},
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,19 @@ Ok(
),
],
},
RuleViolation {
kind: RenamingTable,
span: Span {
start: 0,
len: Some(
52,
),
},
messages: [
Note(
"Renaming a table may break existing clients.",
),
],
},
],
)
1 change: 1 addition & 0 deletions linter/src/violations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum RuleViolationKind {
PreferTextField,
PreferRobustStmts,
BanCharField,
BanDropColumn,
}

impl std::fmt::Display for RuleViolationKind {
Expand Down

0 comments on commit f6f54d0

Please sign in to comment.