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
26 changes: 18 additions & 8 deletions crates/squawk_linter/src/rules/adding_field_with_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use squawk_syntax::ast::AstNode;
use squawk_syntax::{Parse, SourceFile};

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

fn is_const_expr(expr: &ast::Expr) -> bool {
match expr {
Expand Down Expand Up @@ -55,8 +55,7 @@ fn is_non_volatile(expr: &ast::Expr) -> bool {
const NON_VOLATILE_BUILT_IN_FUNCTIONS: &str = include_str!("non_volatile_built_in_functions.txt");

pub(crate) fn adding_field_with_default(ctx: &mut Linter, parse: &Parse<SourceFile>) {
let message =
"Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock.";
let message = "Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock. In Postgres versions 11+, non-VOLATILE DEFAULTs can be added without a rewrite.";
let help = "Add the column as nullable, backfill existing rows, and add a trigger to update the column on write instead.";
let file = parse.tree();
// TODO: use match_ast! like in #api_walkthrough
Expand All @@ -70,7 +69,9 @@ pub(crate) fn adding_field_with_default(ctx: &mut Linter, parse: &Parse<SourceFi
let Some(expr) = default.expr() else {
continue;
};
if is_const_expr(&expr) || is_non_volatile(&expr) {
if ctx.settings.pg_version > Version::new(11, None, None)
&& (is_const_expr(&expr) || is_non_volatile(&expr))
{
continue;
}
ctx.report(
Expand Down Expand Up @@ -106,13 +107,10 @@ mod test {
use insta::assert_debug_snapshot;

use crate::Rule;
use crate::test_utils::lint;
use crate::test_utils::{lint, lint_with_postgres_version};

#[test]
fn docs_example_ok_post_pg_11() {
// TODO: differing from squawk because we aren't checking the postgres
// version, maybe we should be default to a more recent version like 15
// instead of 11?
let sql = r#"
-- instead of
ALTER TABLE "core_recipe" ADD COLUMN "foo" integer DEFAULT 10;
Expand Down Expand Up @@ -277,4 +275,16 @@ ADD COLUMN bar numeric GENERATED ALWAYS AS (bar + baz) STORED;
assert!(!errors.is_empty());
assert_debug_snapshot!(errors);
}

#[test]
fn docs_example_error_on_pg_11() {
let sql = r#"
-- instead of
ALTER TABLE "core_recipe" ADD COLUMN "foo" integer DEFAULT 10;
"#;

let errors = lint_with_postgres_version(sql, Rule::AddingFieldWithDefault, "11");
assert!(!errors.is_empty());
assert_debug_snapshot!(errors);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: errors
[
Violation {
code: AddingFieldWithDefault,
message: "Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock.",
message: "Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock. In Postgres versions 11+, non-VOLATILE DEFAULTs can be added without a rewrite.",
text_range: 62..67,
help: Some(
"Add the column as nullable, backfill existing rows, and add a trigger to update the column on write instead.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: errors
[
Violation {
code: AddingFieldWithDefault,
message: "Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock.",
message: "Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock. In Postgres versions 11+, non-VOLATILE DEFAULTs can be added without a rewrite.",
text_range: 74..83,
help: Some(
"Add the column as nullable, backfill existing rows, and add a trigger to update the column on write instead.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: errors
[
Violation {
code: AddingFieldWithDefault,
message: "Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock.",
message: "Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock. In Postgres versions 11+, non-VOLATILE DEFAULTs can be added without a rewrite.",
text_range: 80..88,
help: Some(
"Add the column as nullable, backfill existing rows, and add a trigger to update the column on write instead.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: errors
[
Violation {
code: AddingFieldWithDefault,
message: "Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock.",
message: "Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock. In Postgres versions 11+, non-VOLATILE DEFAULTs can be added without a rewrite.",
text_range: 60..66,
help: Some(
"Add the column as nullable, backfill existing rows, and add a trigger to update the column on write instead.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: errors
[
Violation {
code: AddingFieldWithDefault,
message: "Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock.",
message: "Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock. In Postgres versions 11+, non-VOLATILE DEFAULTs can be added without a rewrite.",
text_range: 56..62,
help: Some(
"Add the column as nullable, backfill existing rows, and add a trigger to update the column on write instead.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: errors
[
Violation {
code: AddingFieldWithDefault,
message: "Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock.",
message: "Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock. In Postgres versions 11+, non-VOLATILE DEFAULTs can be added without a rewrite.",
text_range: 76..84,
help: Some(
"Add the column as nullable, backfill existing rows, and add a trigger to update the column on write instead.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: crates/squawk_linter/src/rules/adding_field_with_default.rs
expression: errors
---
[
Violation {
code: AddingFieldWithDefault,
message: "Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock. In Postgres versions 11+, non-VOLATILE DEFAULTs can be added without a rewrite.",
text_range: 74..76,
help: Some(
"Add the column as nullable, backfill existing rows, and add a trigger to update the column on write instead.",
),
fix: None,
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: errors
[
Violation {
code: AddingFieldWithDefault,
message: "Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock.",
message: "Adding a generated column requires a table rewrite with an `ACCESS EXCLUSIVE` lock. In Postgres versions 11+, non-VOLATILE DEFAULTs can be added without a rewrite.",
text_range: 40..78,
help: Some(
"Add the column as nullable, backfill existing rows, and add a trigger to update the column on write instead.",
Expand Down
14 changes: 14 additions & 0 deletions crates/squawk_linter/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ pub(crate) fn lint(sql: &str, rule: Rule) -> Vec<Violation> {
linter.lint(&file, sql)
}

pub(crate) fn lint_with_postgres_version(
sql: &str,
rule: Rule,
postgres_version: &str,
) -> Vec<Violation> {
let file = squawk_syntax::SourceFile::parse(sql);
assert_eq!(file.errors().len(), 0);
let mut linter = Linter::from([rule]);
linter.settings.pg_version = postgres_version
.parse()
.expect("Invalid PostgreSQL version");
linter.lint(&file, sql)
}

pub(crate) fn lint_with_assume_in_transaction(sql: &str, rule: Rule) -> Vec<Violation> {
let file = squawk_syntax::SourceFile::parse(sql);
assert_eq!(file.errors().len(), 0);
Expand Down
Loading