Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for add column using references #228

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion linter/src/rules/adding_foreign_key_constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::{
};

use squawk_parser::ast::{
AlterTableCmds, AlterTableDef, AlterTableType, ConstrType, RawStmt, Stmt, TableElt,
AlterTableCmds, AlterTableDef, AlterTableType, ColumnDefConstraint, ConstrType, RawStmt, Stmt,
TableElt,
};

/// Adding a foreign key constraint requires a table scan and a
Expand Down Expand Up @@ -52,6 +53,22 @@ pub fn adding_foreign_key_constraint(
));
}
}
} else if AlterTableType::AddColumn == command.subtype {
if let Some(AlterTableDef::ColumnDef(column_def)) = &command.def {
for ColumnDefConstraint::Constraint(constraint) in
&column_def.constraints
{
if !constraint.skip_validation
&& constraint.contype == ConstrType::Foreign
{
errs.push(RuleViolation::new(
RuleViolationKind::AddingForeignKeyConstraint,
raw_stmt.into(),
None,
));
}
}
}
}
}
}
Expand Down Expand Up @@ -122,6 +139,21 @@ ALTER TABLE "email" ADD CONSTRAINT "fk_user" FOREIGN KEY ("user_id") REFERENCES
COMMIT;
"#;

let violations = lint_sql(sql);
assert_eq!(violations.len(), 1);
assert_eq!(
violations[0].kind,
RuleViolationKind::AddingForeignKeyConstraint
);
}
#[test]
fn test_add_column_references_lock() {
let sql = r#"
BEGIN;
ALTER TABLE "emails" ADD COLUMN "user_id" INT REFERENCES "user" ("id");
COMMIT;
"#;

let violations = lint_sql(sql);
assert_eq!(violations.len(), 1);
assert_eq!(
Expand Down