Skip to content

Commit 2560923

Browse files
authored
Fix errors detected by sqlfluff (#6170)
* Fix errors detected by sqlfluff Fixes #2784 * ml * fix
1 parent 3d0aae0 commit 2560923

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

.automation/test/sql/sql_fix_1.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT name,email, age FROM users
2+
WHERE age>18 AND status='active'
3+
order by name asc;

.automation/test/sql/sql_fix_2.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
insert into products (name,price,category_id)
2+
VALUES('laptop',999.99,1),
3+
('mouse', 29.99, 2),
4+
('keyboard',79.99,2 );

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Note: Can be used with `oxsecurity/megalinter@beta` in your GitHub Action mega-l
3737
- PHP-CS-Fixer is able to run on PHP 8.4 without error (change default configuration) by @llaville
3838
- [cspell](https://megalinter.io/latest/descriptors/spell_cspell/): Filter output lines that do not contain found issues
3939
- [hadolint](https://megalinter.io/latest/descriptors/docker_hadolint/): Extend DOCKERFILE_HADOLINT_FILE_NAMES_REGEX to include the `purpose.Dockerfile` convention eg service.Dockerfile.
40+
- [sqlfluff](https://megalinter.io/beta/descriptors/sql_sqlfluff/): Handle fixing of issues
4041

4142
- Fixes
4243
- When linter is docker based, force `--platform=linux/amd64` so it works when running locally on Mac

megalinter/descriptors/sql.megalinter-descriptor.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ file_extensions:
88
linters:
99
# SQLFLUFF
1010
- linter_name: sqlfluff
11+
class: SqlFluffLinter
1112
linter_text: |
1213
**SQLFluff** is a dialect-flexible and configurable SQL linter designed for ELT applications that works with Jinja templating and dbt. It provides comprehensive SQL code analysis including syntax checking, style validation, and formatting with support for multiple SQL dialects.
1314
@@ -32,6 +33,9 @@ linters:
3233
active_only_if_file_found:
3334
- .sqlfluff
3435
cli_lint_mode: list_of_files
36+
cli_lint_fix_arg_name: fix
37+
cli_lint_fix_remove_args:
38+
- lint
3539
cli_lint_errors_count: regex_count
3640
cli_lint_errors_regex: "L:(.*)P:(.*)"
3741
cli_lint_extra_args:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Use SqlFluff to lint any type of file according to local config
4+
"""
5+
6+
import logging
7+
8+
from megalinter import Linter
9+
10+
11+
class SqlFluffLinter(Linter):
12+
13+
# Manage case when we want semgrep rulesets to be selected related to security
14+
def build_lint_command(self, file=None):
15+
cmd = super().build_lint_command(file)
16+
# if fix arg is in the arguments, add --show-lint-violations just after
17+
if (
18+
self.apply_fixes is True
19+
and self.cli_lint_fix_arg_name is not None
20+
and self.cli_lint_fix_arg_name in cmd
21+
):
22+
fix_index = cmd.index(self.cli_lint_fix_arg_name)
23+
cmd = (
24+
cmd[: fix_index + 1]
25+
+ ["--show-lint-violations"]
26+
+ cmd[fix_index + 1 :] # noqa: E203
27+
)
28+
logging.debug("[SqlFluffLinter] Added --show-lint-violations argument")
29+
return cmd

0 commit comments

Comments
 (0)