Skip to content
Merged
Show file tree
Hide file tree
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
24 changes: 18 additions & 6 deletions crates/squawk_linter/src/rules/adding_not_null_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ use squawk_syntax::{
Parse, SourceFile,
};

use crate::{Linter, Rule, Version, Violation};
use crate::{Linter, Rule, Violation};

pub(crate) fn adding_not_null_field(ctx: &mut Linter, parse: &Parse<SourceFile>) {
if ctx.settings.pg_version >= Version::new(11, 0, 0) {
return;
}
let file = parse.tree();
for stmt in file.stmts() {
if let ast::Stmt::AlterTable(alter_table) = stmt {
Expand Down Expand Up @@ -38,7 +35,7 @@ pub(crate) fn adding_not_null_field(ctx: &mut Linter, parse: &Parse<SourceFile>)
mod test {
use insta::assert_debug_snapshot;

use crate::{Linter, Rule, Version};
use crate::{Linter, Rule};

#[test]
fn set_not_null() {
Expand All @@ -47,7 +44,6 @@ ALTER TABLE "core_recipe" ALTER COLUMN "foo" SET NOT NULL;
"#;
let file = squawk_syntax::SourceFile::parse(sql);
let mut linter = Linter::from([Rule::AddingNotNullableField]);
linter.settings.pg_version = Version::new(10, 0, 0);
let errors = linter.lint(file, sql);
assert!(!errors.is_empty());
assert_debug_snapshot!(errors);
Expand Down Expand Up @@ -96,4 +92,20 @@ COMMIT;
let errors = linter.lint(file, sql);
assert!(errors.is_empty());
}

#[test]
fn regression_gh_issue_519() {
let sql = r#"
BEGIN;
-- Running upgrade a -> b
ALTER TABLE my_table ALTER COLUMN my_column SET NOT NULL;
UPDATE alembic_version SET version_num='b' WHERE alembic_version.version_num = 'a';
COMMIT;
"#;
let file = squawk_syntax::SourceFile::parse(sql);
let mut linter = Linter::from([Rule::AddingNotNullableField]);
let errors = linter.lint(file, sql);
assert!(!errors.is_empty());
assert_debug_snapshot!(errors);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: crates/squawk_linter/src/rules/adding_not_null_field.rs
expression: errors
---
[
Violation {
code: AddingNotNullableField,
message: "Setting a column `NOT NULL` blocks reads while the table is scanned.",
text_range: 78..90,
help: Some(
"Make the field nullable and use a `CHECK` constraint instead.",
),
},
]
Loading