-
Notifications
You must be signed in to change notification settings - Fork 66
linter: add require-table-schema rule #1046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| use squawk_syntax::{ | ||
| Parse, SourceFile, | ||
| ast::{self, AstNode}, | ||
| }; | ||
|
|
||
| use crate::{Linter, Rule, Violation}; | ||
|
|
||
| pub(crate) fn require_table_schema(ctx: &mut Linter, parse: &Parse<SourceFile>) { | ||
| let file = parse.tree(); | ||
| for stmt in file.stmts() { | ||
| match stmt { | ||
| ast::Stmt::CreateTable(create_table) => { | ||
| check_path(ctx, create_table.path(), create_table.syntax()); | ||
| } | ||
| ast::Stmt::CreateTableAs(create_table_as) => { | ||
| check_path(ctx, create_table_as.path(), create_table_as.syntax()); | ||
| } | ||
| ast::Stmt::AlterTable(alter_table) => { | ||
| let path = alter_table.relation_name().and_then(|r| r.path()); | ||
| check_path(ctx, path, alter_table.syntax()); | ||
| } | ||
| ast::Stmt::DropTable(drop_table) => { | ||
| check_path(ctx, drop_table.path(), drop_table.syntax()); | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn check_path(ctx: &mut Linter, path: Option<ast::Path>, syntax: &squawk_syntax::SyntaxNode) { | ||
| if let Some(path) = path { | ||
| if path.qualifier().is_none() { | ||
| ctx.report(Violation::for_node( | ||
| Rule::RequireTableSchema, | ||
| "Table name is not schema-qualified. Use schema.table (e.g., public.my_table)." | ||
| .into(), | ||
| syntax, | ||
| )); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use insta::assert_snapshot; | ||
|
|
||
| use crate::Rule; | ||
| use crate::test_utils::{lint_errors, lint_ok}; | ||
|
|
||
| #[test] | ||
| fn create_table_err() { | ||
| let sql = r#" | ||
| CREATE TABLE my_table (id int); | ||
| "#; | ||
| assert_snapshot!(lint_errors(sql, Rule::RequireTableSchema)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn create_table_ok() { | ||
| let sql = r#" | ||
| CREATE TABLE public.my_table (id int); | ||
| "#; | ||
| lint_ok(sql, Rule::RequireTableSchema); | ||
| } | ||
|
|
||
| #[test] | ||
| fn alter_table_err() { | ||
| let sql = r#" | ||
| ALTER TABLE my_table ADD COLUMN name text; | ||
| "#; | ||
| assert_snapshot!(lint_errors(sql, Rule::RequireTableSchema)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn alter_table_ok() { | ||
| let sql = r#" | ||
| ALTER TABLE public.my_table ADD COLUMN name text; | ||
| "#; | ||
| lint_ok(sql, Rule::RequireTableSchema); | ||
| } | ||
|
|
||
| #[test] | ||
| fn drop_table_err() { | ||
| let sql = r#" | ||
| DROP TABLE my_table; | ||
| "#; | ||
| assert_snapshot!(lint_errors(sql, Rule::RequireTableSchema)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn drop_table_ok() { | ||
| let sql = r#" | ||
| DROP TABLE public.my_table; | ||
| "#; | ||
| lint_ok(sql, Rule::RequireTableSchema); | ||
| } | ||
|
|
||
| #[test] | ||
| fn create_table_as_err() { | ||
| let sql = r#" | ||
| CREATE TABLE my_table AS SELECT 1; | ||
| "#; | ||
| assert_snapshot!(lint_errors(sql, Rule::RequireTableSchema)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn create_table_as_ok() { | ||
| let sql = r#" | ||
| CREATE TABLE public.my_table AS SELECT 1; | ||
| "#; | ||
| lint_ok(sql, Rule::RequireTableSchema); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
...rc/rules/snapshots/squawk_linter__rules__require_table_schema__test__alter_table_err.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| source: crates/squawk_linter/src/rules/require_table_schema.rs | ||
| expression: "lint_errors(sql, Rule::RequireTableSchema)" | ||
| --- | ||
| warning[require-table-schema]: Table name is not schema-qualified. Use schema.table (e.g., public.my_table). | ||
| ╭▸ | ||
| 2 │ ALTER TABLE my_table ADD COLUMN name text; | ||
| ╰╴━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ |
8 changes: 8 additions & 0 deletions
8
...ules/snapshots/squawk_linter__rules__require_table_schema__test__create_table_as_err.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| source: crates/squawk_linter/src/rules/require_table_schema.rs | ||
| expression: "lint_errors(sql, Rule::RequireTableSchema)" | ||
| --- | ||
| warning[require-table-schema]: Table name is not schema-qualified. Use schema.table (e.g., public.my_table). | ||
| ╭▸ | ||
| 2 │ CREATE TABLE my_table AS SELECT 1; | ||
| ╰╴━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ |
8 changes: 8 additions & 0 deletions
8
...c/rules/snapshots/squawk_linter__rules__require_table_schema__test__create_table_err.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| source: crates/squawk_linter/src/rules/require_table_schema.rs | ||
| expression: "lint_errors(sql, Rule::RequireTableSchema)" | ||
| --- | ||
| warning[require-table-schema]: Table name is not schema-qualified. Use schema.table (e.g., public.my_table). | ||
| ╭▸ | ||
| 2 │ CREATE TABLE my_table (id int); | ||
| ╰╴━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ |
8 changes: 8 additions & 0 deletions
8
...src/rules/snapshots/squawk_linter__rules__require_table_schema__test__drop_table_err.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| source: crates/squawk_linter/src/rules/require_table_schema.rs | ||
| expression: "lint_errors(sql, Rule::RequireTableSchema)" | ||
| --- | ||
| warning[require-table-schema]: Table name is not schema-qualified. Use schema.table (e.g., public.my_table). | ||
| ╭▸ | ||
| 2 │ DROP TABLE my_table; | ||
| ╰╴━━━━━━━━━━━━━━━━━━━ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How bad is this? I'm afraid that adding
require-table-schemawill affect many current squawk users (the linter will fail after the update).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think we'll want to have it disabled by default, I'm not 100% sure of people's usage patterns
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at where we call
with_all_rules, it's only used in the LSP Server & Playground (via Wasm)So I think we could skip this
is_opt_inmethodThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
without_rules? Just always insert RequireTableSchema intoexclude_set?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sbdchd ^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think so, without_rules is disabling the rules that are explicitly passed in