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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/squawk_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ insta.workspace = true
enum-iterator.workspace = true
line-index.workspace = true
serde_plain.workspace = true
annotate-snippets.workspace = true


[lints]
Expand Down
2 changes: 1 addition & 1 deletion crates/squawk_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ impl Linter {
errors: vec![],
ignores: vec![],
rules: rules.into(),
settings: LinterSettings::default(),
settings: Default::default(),
}
}
}
Expand Down
88 changes: 31 additions & 57 deletions crates/squawk_linter/src/rules/adding_field_with_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,14 @@ pub(crate) fn adding_field_with_default(ctx: &mut Linter, parse: &Parse<SourceFi

#[cfg(test)]
mod test {
use insta::assert_debug_snapshot;
use insta::assert_snapshot;

use crate::Rule;
use crate::test_utils::{lint, lint_with_postgres_version};
use crate::test_utils::{lint_errors, lint_ok};
use crate::{LinterSettings, Rule};

fn lint_errors_with(sql: &str, settings: LinterSettings) -> String {
crate::test_utils::lint_errors_with(sql, settings, Rule::AddingFieldWithDefault)
}

#[test]
fn docs_example_ok_post_pg_11() {
Expand All @@ -135,9 +139,7 @@ mod test {
ALTER TABLE "core_recipe" ADD COLUMN "foo" integer DEFAULT 10;
"#;

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

#[test]
Expand All @@ -150,9 +152,7 @@ ALTER TABLE "core_recipe" ALTER COLUMN "foo" SET DEFAULT 10;
-- remove nullability
"#;

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

#[test]
Expand All @@ -161,9 +161,7 @@ ALTER TABLE "core_recipe" ALTER COLUMN "foo" SET DEFAULT 10;
alter table t set logged, add column c integer default uuid();
"#;

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

#[test]
Expand All @@ -172,9 +170,7 @@ alter table t set logged, add column c integer default uuid();
ALTER TABLE "core_recipe" ADD COLUMN "foo" integer DEFAULT uuid();
"#;

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

#[test]
Expand All @@ -184,9 +180,7 @@ ALTER TABLE "core_recipe" ADD COLUMN "foo" integer DEFAULT uuid();
ALTER TABLE "core_recipe" ADD COLUMN "foo" boolean DEFAULT random();
"#;

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

#[test]
Expand All @@ -196,9 +190,7 @@ ALTER TABLE "core_recipe" ADD COLUMN "foo" boolean DEFAULT random();
ALTER TABLE "core_recipe" ADD COLUMN "foo" boolean DEFAULT true;
"#;

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

#[test]
Expand All @@ -211,9 +203,7 @@ 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);
lint_ok(sql, Rule::AddingFieldWithDefault);
}

#[test]
Expand All @@ -223,9 +213,7 @@ 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);
lint_ok(sql, Rule::AddingFieldWithDefault);
}

#[test]
Expand All @@ -235,9 +223,7 @@ ADD COLUMN statistics_last_updated_at timestamptz NOT NULL DEFAULT now() - inter
ALTER TABLE "core_recipe" ADD COLUMN "foo" text DEFAULT 'some-str';
"#;

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

#[test]
Expand All @@ -247,9 +233,7 @@ ALTER TABLE "core_recipe" ADD COLUMN "foo" text DEFAULT 'some-str';
ALTER TABLE "core_recipe" ADD COLUMN "foo" some_enum_type DEFAULT 'my-enum-variant';
"#;

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

#[test]
Expand All @@ -259,9 +243,7 @@ ALTER TABLE "core_recipe" ADD COLUMN "foo" some_enum_type DEFAULT 'my-enum-varia
ALTER TABLE "core_recipe" ADD COLUMN "foo" jsonb DEFAULT '{}'::jsonb;
"#;

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

#[test]
Expand All @@ -271,9 +253,7 @@ ALTER TABLE "core_recipe" ADD COLUMN "foo" jsonb DEFAULT '{}'::jsonb;
ALTER TABLE "core_recipe" ADD COLUMN "foo" jsonb DEFAULT myjsonb();
"#;

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

#[test]
Expand All @@ -283,9 +263,7 @@ ALTER TABLE "core_recipe" ADD COLUMN "foo" jsonb DEFAULT myjsonb();
ALTER TABLE "core_recipe" ADD COLUMN "foo" timestamptz DEFAULT now(123);
"#;

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

#[test]
Expand All @@ -295,9 +273,7 @@ ALTER TABLE "core_recipe" ADD COLUMN "foo" timestamptz DEFAULT now(123);
ALTER TABLE "core_recipe" ADD COLUMN "foo" timestamptz DEFAULT now();
"#;

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

#[test]
Expand All @@ -306,9 +282,7 @@ ALTER TABLE "core_recipe" ADD COLUMN "foo" timestamptz DEFAULT now();
alter table t add column c timestamptz default current_timestamp;
"#;

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

#[test]
Expand All @@ -317,9 +291,7 @@ alter table t add column c timestamptz default current_timestamp;
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);
lint_ok(sql, Rule::AddingFieldWithDefault);
}

#[test]
Expand All @@ -329,9 +301,7 @@ ALTER TABLE foo
ADD COLUMN bar numeric GENERATED ALWAYS AS (bar + baz) STORED;
"#;

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

#[test]
Expand All @@ -341,8 +311,12 @@ ADD COLUMN bar numeric GENERATED ALWAYS AS (bar + baz) STORED;
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);
assert_snapshot!(lint_errors_with(
sql,
LinterSettings {
pg_version: "11".parse().expect("Invalid PostgreSQL version"),
..Default::default()
},
));
}
}
38 changes: 21 additions & 17 deletions crates/squawk_linter/src/rules/adding_foreign_key_constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,14 @@ pub(crate) fn adding_foreign_key_constraint(ctx: &mut Linter, parse: &Parse<Sour

#[cfg(test)]
mod test {
use crate::Rule;
use crate::test_utils::{lint, lint_with_assume_in_transaction};
use insta::assert_snapshot;

use crate::test_utils::{lint_errors, lint_ok};
use crate::{LinterSettings, Rule};

fn lint_ok_with(sql: &str, settings: LinterSettings) {
crate::test_utils::lint_ok_with(sql, settings, Rule::AddingForeignKeyConstraint);
}

#[test]
fn create_table_with_foreign_key_constraint() {
Expand All @@ -91,14 +97,13 @@ mod test {
email TEXT,
PRIMARY KEY(id),
CONSTRAINT fk_user
FOREIGN KEY ("user_id")
FOREIGN KEY ("user_id")
REFERENCES "user" ("id")
);
COMMIT;
"#;

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

#[test]
Expand All @@ -108,8 +113,13 @@ CREATE TABLE "emails" ("id" UUID NOT NULL, "user_id" UUID NOT NULL);
ALTER TABLE "emails" ADD CONSTRAINT "fk_user" FOREIGN KEY ("user_id") REFERENCES "users" ("id");
"#;

let errors = lint_with_assume_in_transaction(sql, Rule::AddingForeignKeyConstraint);
assert!(errors.is_empty());
lint_ok_with(
sql,
LinterSettings {
assume_in_transaction: true,
..Default::default()
},
);
}

#[test]
Expand All @@ -121,8 +131,7 @@ ALTER TABLE "emails" ADD CONSTRAINT "fk_user" FOREIGN KEY ("user_id") REFERENCES
COMMIT;
"#;

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

#[test]
Expand All @@ -135,8 +144,7 @@ ALTER TABLE "email" VALIDATE CONSTRAINT "fk_user";
COMMIT;
"#;

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

#[test]
Expand All @@ -148,9 +156,7 @@ ALTER TABLE "email" ADD CONSTRAINT "fk_user" FOREIGN KEY ("user_id") REFERENCES
COMMIT;
"#;

let errors = lint(sql, Rule::AddingForeignKeyConstraint);
assert_eq!(errors.len(), 1);
assert_eq!(errors[0].code, Rule::AddingForeignKeyConstraint);
assert_snapshot!(lint_errors(sql, Rule::AddingForeignKeyConstraint));
}

#[test]
Expand All @@ -161,8 +167,6 @@ ALTER TABLE "emails" ADD COLUMN "user_id" INT REFERENCES "user" ("id");
COMMIT;
"#;

let errors = lint(sql, Rule::AddingForeignKeyConstraint);
assert_eq!(errors.len(), 1);
assert_eq!(errors[0].code, Rule::AddingForeignKeyConstraint);
assert_snapshot!(lint_errors(sql, Rule::AddingForeignKeyConstraint));
}
}
21 changes: 7 additions & 14 deletions crates/squawk_linter/src/rules/adding_not_null_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,17 @@ pub(crate) fn adding_not_null_field(ctx: &mut Linter, parse: &Parse<SourceFile>)

#[cfg(test)]
mod test {
use insta::assert_debug_snapshot;
use insta::assert_snapshot;

use crate::Rule;
use crate::test_utils::lint;
use crate::test_utils::{lint_errors, lint_ok};

#[test]
fn set_not_null() {
let sql = r#"
ALTER TABLE "core_recipe" ALTER COLUMN "foo" SET NOT NULL;
"#;
let errors = lint(sql, Rule::AddingNotNullableField);
assert!(!errors.is_empty());
assert_debug_snapshot!(errors);
assert_snapshot!(lint_errors(sql, Rule::AddingNotNullableField));
}

#[test]
Expand All @@ -56,8 +54,7 @@ ALTER TABLE "core_recipe" ADD COLUMN "foo" integer DEFAULT 10 NOT NULL;
ALTER TABLE "core_recipe" ALTER COLUMN "foo" DROP DEFAULT;
COMMIT;
"#;
let errors = lint(sql, Rule::AddingNotNullableField);
assert!(errors.is_empty());
lint_ok(sql, Rule::AddingNotNullableField);
}

#[test]
Expand All @@ -66,8 +63,7 @@ COMMIT;
-- This won't work if the table is populated, but that error is caught by adding-required-field.
ALTER TABLE "core_recipe" ADD COLUMN "foo" integer NOT NULL;
"#;
let errors = lint(sql, Rule::AddingNotNullableField);
assert!(errors.is_empty());
lint_ok(sql, Rule::AddingNotNullableField);
}

#[test]
Expand All @@ -80,8 +76,7 @@ BEGIN;
ALTER TABLE "core_recipe" ADD COLUMN "foo" integer NOT NULL DEFAULT 10;
COMMIT;
"#;
let errors = lint(sql, Rule::AddingNotNullableField);
assert!(errors.is_empty());
lint_ok(sql, Rule::AddingNotNullableField);
}

#[test]
Expand All @@ -93,8 +88,6 @@ 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 errors = lint(sql, Rule::AddingNotNullableField);
assert!(!errors.is_empty());
assert_debug_snapshot!(errors);
assert_snapshot!(lint_errors(sql, Rule::AddingNotNullableField));
}
}
Loading
Loading