-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add(rules): adding_primary_key rule to check for blocking pk creation (…
…#64) As outlined in the citusdata post, adding primary keys to existing tables is a blocking operation. As such, Squawk should warn about them. https://www.citusdata.com/blog/2018/02/22/seven-tips-for-dealing-with-postgres-locks/ rel: #60
- Loading branch information
Showing
9 changed files
with
212 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use crate::violations::{RuleViolation, RuleViolationKind}; | ||
use squawk_parser::ast::{ | ||
AlterTableCmds, AlterTableDef, ColumnDefConstraint, ConstrType, RootStmt, Stmt, | ||
}; | ||
|
||
#[must_use] | ||
pub fn adding_primary_key_constraint(tree: &[RootStmt]) -> Vec<RuleViolation> { | ||
let mut errs = vec![]; | ||
for RootStmt::RawStmt(raw_stmt) in tree { | ||
match &raw_stmt.stmt { | ||
Stmt::AlterTableStmt(stmt) => { | ||
for AlterTableCmds::AlterTableCmd(cmd) in &stmt.cmds { | ||
match &cmd.def { | ||
Some(AlterTableDef::ColumnDef(def)) => { | ||
for ColumnDefConstraint::Constraint(constraint) in &def.constraints { | ||
if constraint.contype == ConstrType::Primary { | ||
errs.push(RuleViolation::new( | ||
RuleViolationKind::AddingSerialPrimaryKeyField, | ||
raw_stmt, | ||
None, | ||
)); | ||
} | ||
} | ||
} | ||
Some(AlterTableDef::Constraint(constraint)) => { | ||
if constraint.contype == ConstrType::Primary { | ||
errs.push(RuleViolation::new( | ||
RuleViolationKind::AddingSerialPrimaryKeyField, | ||
raw_stmt, | ||
None, | ||
)); | ||
} | ||
} | ||
_ => continue, | ||
} | ||
} | ||
} | ||
_ => continue, | ||
} | ||
} | ||
errs | ||
} | ||
|
||
#[cfg(test)] | ||
mod test_rules { | ||
use crate::check_sql; | ||
use insta::assert_debug_snapshot; | ||
|
||
#[test] | ||
fn test_serial_primary_key() { | ||
let bad_sql = r#" | ||
ALTER TABLE a ADD COLUMN b SERIAL PRIMARY KEY; | ||
"#; | ||
|
||
let expected_bad_res = | ||
check_sql(bad_sql, &["prefer-robust-stmts".into()]).unwrap_or_default(); | ||
assert_ne!(expected_bad_res, vec![]); | ||
assert_debug_snapshot!(expected_bad_res); | ||
} | ||
|
||
#[test] | ||
fn test_plain_primary_key() { | ||
let bad_sql = r#" | ||
ALTER TABLE items ADD PRIMARY KEY (id); | ||
"#; | ||
|
||
let expected_bad_res = | ||
check_sql(bad_sql, &["prefer-robust-stmts".into()]).unwrap_or_default(); | ||
assert_ne!(expected_bad_res, vec![]); | ||
assert_debug_snapshot!(expected_bad_res); | ||
|
||
let ok_sql = r#" | ||
ALTER TABLE items ADD CONSTRAINT items_pk PRIMARY KEY USING INDEX items_pk; | ||
"#; | ||
assert_debug_snapshot!(check_sql(ok_sql, &["prefer-robust-stmts".into()])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...squawk_linter__rules__adding_primary_key_constraint__test_rules__plain_primary_key-2.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
source: linter/src/rules/adding_primary_key_constraint.rs | ||
expression: "check_sql(ok_sql, &[\"prefer-robust-stmts\".into()])" | ||
--- | ||
Ok( | ||
[ | ||
RuleViolation { | ||
kind: AddingSerialPrimaryKeyField, | ||
span: Span { | ||
start: 0, | ||
len: Some( | ||
75, | ||
), | ||
}, | ||
messages: [ | ||
Note( | ||
"Adding a PRIMARY KEY constraint results in locks and table rewrites", | ||
), | ||
Help( | ||
"Add the PRIMARY KEY constraint USING an index.", | ||
), | ||
], | ||
}, | ||
], | ||
) |
23 changes: 23 additions & 0 deletions
23
...s/squawk_linter__rules__adding_primary_key_constraint__test_rules__plain_primary_key.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
source: linter/src/rules/adding_primary_key_constraint.rs | ||
expression: expected_bad_res | ||
--- | ||
[ | ||
RuleViolation { | ||
kind: AddingSerialPrimaryKeyField, | ||
span: Span { | ||
start: 0, | ||
len: Some( | ||
39, | ||
), | ||
}, | ||
messages: [ | ||
Note( | ||
"Adding a PRIMARY KEY constraint results in locks and table rewrites", | ||
), | ||
Help( | ||
"Add the PRIMARY KEY constraint USING an index.", | ||
), | ||
], | ||
}, | ||
] |
23 changes: 23 additions & 0 deletions
23
.../squawk_linter__rules__adding_primary_key_constraint__test_rules__serial_primary_key.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
source: linter/src/rules/adding_primary_key_constraint.rs | ||
expression: expected_bad_res | ||
--- | ||
[ | ||
RuleViolation { | ||
kind: AddingSerialPrimaryKeyField, | ||
span: Span { | ||
start: 0, | ||
len: Some( | ||
46, | ||
), | ||
}, | ||
messages: [ | ||
Note( | ||
"Adding a PRIMARY KEY constraint results in locks and table rewrites", | ||
), | ||
Help( | ||
"Add the PRIMARY KEY constraint USING an index.", | ||
), | ||
], | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters