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
83 changes: 71 additions & 12 deletions crates/squawk_linter/src/rules/adding_field_with_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@ use lazy_static::lazy_static;
use std::collections::HashSet;

use squawk_syntax::ast::AstNode;
use squawk_syntax::{Parse, SourceFile};
use squawk_syntax::{Parse, SourceFile, SyntaxKind};
use squawk_syntax::{ast, identifier::Identifier};

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

fn is_const_expr(expr: &ast::Expr) -> bool {
match expr {
ast::Expr::Literal(_) => true,
ast::Expr::CastExpr(cast) => matches!(cast.expr(), Some(ast::Expr::Literal(_))),
_ => false,
}
}

lazy_static! {
static ref NON_VOLATILE_FUNCS: HashSet<Identifier> = {
NON_VOLATILE_BUILT_IN_FUNCTIONS
Expand All @@ -26,8 +18,18 @@ lazy_static! {
};
}

fn is_non_volatile(expr: &ast::Expr) -> bool {
fn is_non_volatile_or_const(expr: &ast::Expr) -> bool {
match expr {
ast::Expr::Literal(_) => true,
ast::Expr::ArrayExpr(_) => true,
ast::Expr::BinExpr(bin_expr) => {
if let Some(lhs) = bin_expr.lhs() {
if let Some(rhs) = bin_expr.rhs() {
return is_non_volatile_or_const(&lhs) && is_non_volatile_or_const(&rhs);
}
}
false
}
ast::Expr::CallExpr(call_expr) => {
if let Some(arglist) = call_expr.arg_list() {
let no_args = arglist.args().count() == 0;
Expand All @@ -45,6 +47,24 @@ fn is_non_volatile(expr: &ast::Expr) -> bool {
false
}
}
// array[]::t[] is non-volatile. We don't check for a plain array expr
// since postgres will reject it as a default unless it's cast to a type.
ast::Expr::CastExpr(cast_expr) => {
if let Some(inner_expr) = cast_expr.expr() {
is_non_volatile_or_const(&inner_expr)
} else {
false
}
}
// current_timestamp is the same as calling now()
ast::Expr::NameRef(name_ref) => {
if let Some(child) = name_ref.syntax().first_child_or_token() {
if child.kind() == SyntaxKind::CURRENT_TIMESTAMP_KW {
return true;
}
}
false
}
_ => false,
}
}
Expand All @@ -69,7 +89,7 @@ pub(crate) fn adding_field_with_default(ctx: &mut Linter, parse: &Parse<SourceFi
continue;
};
if ctx.settings.pg_version > Version::new(11, None, None)
&& (is_const_expr(&expr) || is_non_volatile(&expr))
&& is_non_volatile_or_const(&expr)
{
continue;
}
Expand Down Expand Up @@ -181,6 +201,33 @@ ALTER TABLE "core_recipe" ADD COLUMN "foo" boolean DEFAULT true;
assert_debug_snapshot!(errors);
}

#[test]
fn default_empty_array_ok() {
let sql = r#"
alter table t add column a double precision[] default array[]::double precision[];

alter table t add column b bigint[] default cast(array[] as bigint[]);

alter table t add column c text[] default array['foo', 'bar']::text[];
"#;

let errors = lint(sql, Rule::AddingFieldWithDefault);
assert!(errors.is_empty());
assert_debug_snapshot!(errors);
}

#[test]
fn default_with_const_bin_expr() {
let sql = r#"
ALTER TABLE assessments
ADD COLUMN statistics_last_updated_at timestamptz NOT NULL DEFAULT now() - interval '100 years';
"#;

let errors = lint(sql, Rule::AddingFieldWithDefault);
assert!(errors.is_empty());
assert_debug_snapshot!(errors);
}

#[test]
fn default_str_ok() {
let sql = r#"
Expand Down Expand Up @@ -240,6 +287,7 @@ ALTER TABLE "core_recipe" ADD COLUMN "foo" timestamptz DEFAULT now(123);
assert!(!errors.is_empty());
assert_debug_snapshot!(errors);
}

#[test]
fn default_func_now_ok() {
let sql = r#"
Expand All @@ -252,14 +300,25 @@ ALTER TABLE "core_recipe" ADD COLUMN "foo" timestamptz DEFAULT now();
assert_debug_snapshot!(errors);
}

#[test]
fn default_func_current_timestamp_ok() {
let sql = r#"
alter table t add column c timestamptz default current_timestamp;
"#;

let errors = lint(sql, Rule::AddingFieldWithDefault);
assert!(errors.is_empty());
assert_debug_snapshot!(errors);
}

#[test]
fn add_numbers_ok() {
// This should be okay, but we don't handle expressions like this at the moment.
let sql = r#"
alter table account_metadata add column blah integer default 2 + 2;
"#;

let errors = lint(sql, Rule::AddingFieldWithDefault);
assert!(errors.is_empty());
assert_debug_snapshot!(errors);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,4 @@
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: 62..67,
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
@@ -0,0 +1,5 @@
---
source: crates/squawk_linter/src/rules/adding_field_with_default.rs
expression: errors
---
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/squawk_linter/src/rules/adding_field_with_default.rs
expression: errors
---
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/squawk_linter/src/rules/adding_field_with_default.rs
expression: errors
---
[]
Loading