From f4a79f1b80c40b3a350ac3cf063157e9278bb32a Mon Sep 17 00:00:00 2001 From: Steve Dignam Date: Fri, 30 May 2025 17:49:52 -0400 Subject: [PATCH] linter: fix regression in adding_not_null_field related to v2 rewrite I think the changes from https://github.com/sbdchd/squawk/pull/412 / https://github.com/sbdchd/squawk/commit/360e3ee53edddfcfd63d0944699c7c7ab31ab7f3 didn't make it into the v2 codebase. rel: https://github.com/sbdchd/squawk/issues/519 --- .../src/rules/adding_not_null_field.rs | 24 ++++++++++++++----- ..._field__test__regression_gh_issue_519.snap | 14 +++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 crates/squawk_linter/src/rules/snapshots/squawk_linter__rules__adding_not_null_field__test__regression_gh_issue_519.snap diff --git a/crates/squawk_linter/src/rules/adding_not_null_field.rs b/crates/squawk_linter/src/rules/adding_not_null_field.rs index fb5ddf5c..c3a4e2a9 100644 --- a/crates/squawk_linter/src/rules/adding_not_null_field.rs +++ b/crates/squawk_linter/src/rules/adding_not_null_field.rs @@ -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) { - 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 { @@ -38,7 +35,7 @@ pub(crate) fn adding_not_null_field(ctx: &mut Linter, parse: &Parse) mod test { use insta::assert_debug_snapshot; - use crate::{Linter, Rule, Version}; + use crate::{Linter, Rule}; #[test] fn set_not_null() { @@ -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); @@ -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); + } } diff --git a/crates/squawk_linter/src/rules/snapshots/squawk_linter__rules__adding_not_null_field__test__regression_gh_issue_519.snap b/crates/squawk_linter/src/rules/snapshots/squawk_linter__rules__adding_not_null_field__test__regression_gh_issue_519.snap new file mode 100644 index 00000000..54cbadd9 --- /dev/null +++ b/crates/squawk_linter/src/rules/snapshots/squawk_linter__rules__adding_not_null_field__test__regression_gh_issue_519.snap @@ -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.", + ), + }, +]