Skip to content

Commit 624046a

Browse files
authored
linter: fix regression in adding_not_null_field related to v2 rewrite (#520)
I think the changes from #412 / 360e3ee didn't make it into the v2 codebase. rel: #519
1 parent 0456dba commit 624046a

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

crates/squawk_linter/src/rules/adding_not_null_field.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ use squawk_syntax::{
33
Parse, SourceFile,
44
};
55

6-
use crate::{Linter, Rule, Version, Violation};
6+
use crate::{Linter, Rule, Violation};
77

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

41-
use crate::{Linter, Rule, Version};
38+
use crate::{Linter, Rule};
4239

4340
#[test]
4441
fn set_not_null() {
@@ -47,7 +44,6 @@ ALTER TABLE "core_recipe" ALTER COLUMN "foo" SET NOT NULL;
4744
"#;
4845
let file = squawk_syntax::SourceFile::parse(sql);
4946
let mut linter = Linter::from([Rule::AddingNotNullableField]);
50-
linter.settings.pg_version = Version::new(10, 0, 0);
5147
let errors = linter.lint(file, sql);
5248
assert!(!errors.is_empty());
5349
assert_debug_snapshot!(errors);
@@ -96,4 +92,20 @@ COMMIT;
9692
let errors = linter.lint(file, sql);
9793
assert!(errors.is_empty());
9894
}
95+
96+
#[test]
97+
fn regression_gh_issue_519() {
98+
let sql = r#"
99+
BEGIN;
100+
-- Running upgrade a -> b
101+
ALTER TABLE my_table ALTER COLUMN my_column SET NOT NULL;
102+
UPDATE alembic_version SET version_num='b' WHERE alembic_version.version_num = 'a';
103+
COMMIT;
104+
"#;
105+
let file = squawk_syntax::SourceFile::parse(sql);
106+
let mut linter = Linter::from([Rule::AddingNotNullableField]);
107+
let errors = linter.lint(file, sql);
108+
assert!(!errors.is_empty());
109+
assert_debug_snapshot!(errors);
110+
}
99111
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
source: crates/squawk_linter/src/rules/adding_not_null_field.rs
3+
expression: errors
4+
---
5+
[
6+
Violation {
7+
code: AddingNotNullableField,
8+
message: "Setting a column `NOT NULL` blocks reads while the table is scanned.",
9+
text_range: 78..90,
10+
help: Some(
11+
"Make the field nullable and use a `CHECK` constraint instead.",
12+
),
13+
},
14+
]

0 commit comments

Comments
 (0)