Skip to content
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

Add a sqlfluff format CLI command #4473

Merged
merged 6 commits into from Mar 7, 2023
Merged

Add a sqlfluff format CLI command #4473

merged 6 commits into from Mar 7, 2023

Conversation

alanmcruickshank
Copy link
Member

@alanmcruickshank alanmcruickshank commented Mar 6, 2023

Ok this is a long time coming. This resolves #2436 and adds the sqlfluff format CLI command.

It's basically a variant on sqlfluff fix, but with a hardcoded set of rules. This has a nice upside that if someone is using exclude_rules, they can still exclude rules - but they can't enable additional ones, so it remains fairly safe.

As a first pass I've added the following rules to the command:

  • All of the capitalisation rules: capitalisation
  • All of the layout rules: layout
  • ambiguous.union
  • convention.not_equal
  • convention.coalesce
  • convention.select_trailing_comma
  • convention.is_null
  • jinja.padding
  • structure.distinct

I haven't added extensive testing, because the majority of the machinery is the same as sqlfluff fix. There should be enough here to get coverage.

How to review (@barrywhart):

  1. I've moved several common click arguments into @lint_options. No changes to the functionality, just a reorg. Those options are --processes and --disable-progress-bar. That happens in the first commit in the PR afc6ec3
  2. I've broken out the original fix command into two branches (and two methods), one for stdin fixing (_stdin_fix()) and one for file based fixing (_paths_fix()) - both are used in fix and format. No functionality changes - just refactor.
  3. Introduce the cli_format() method to support sqlfluff format cli command. It's mostly a copy paste of sqlfluff fix but with several things removed or changed to simplify things. Specifically, it hard codes the rules argument and always sets the force argument.

@alanmcruickshank alanmcruickshank marked this pull request as ready for review March 6, 2023 21:30
@alanmcruickshank alanmcruickshank requested review from a team, barrywhart, dmateusp and tunetheweb and removed request for a team and dmateusp March 6, 2023 21:30
@coveralls
Copy link

coveralls commented Mar 6, 2023

Pull Request Test Coverage Report for Build 4353649815

  • 72 of 72 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 4347827868: 0.0%
Covered Lines: 17345
Relevant Lines: 17345

💛 - Coveralls

@codecov
Copy link

codecov bot commented Mar 6, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change

Comparison is base (9c2ce9e) 100.00% compared to head (5c10bde) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #4473   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          204       204           
  Lines        15484     15514   +30     
=========================================
+ Hits         15484     15514   +30     
Impacted Files Coverage Δ
src/sqlfluff/rules/jinja/JJ01.py 100.00% <ø> (ø)
src/sqlfluff/cli/commands.py 100.00% <100.00%> (ø)
src/sqlfluff/rules/capitalisation/CP05.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@alanmcruickshank alanmcruickshank mentioned this pull request Mar 6, 2023
12 tasks
@@ -695,154 +704,67 @@ def do_fixes(lnt, result, formatter=None, **kwargs):
return False # pragma: no cover


@cli.command(cls=DeprecatedOptionsCommand)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alanmcruickshank: Can you suggest how to review this? It looks like a lot of changes starting here, but I'm guessing some of this was code reorg?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'll add a summary in the PR above.

help="An optional suffix to add to fixed files.",
)
@click.option(
"--FIX-EVEN-UNPARSABLE",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the spirit of format being a simpler, safer command, I wonder if we should consider omitting this option, which is known to be unsafe.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea

),
)
@click.option(
"--show-lint-violations",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this option relevant for format?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not.

@@ -48,6 +48,9 @@ class Rule_JJ01(BaseRule):
# sections.
crawl_behaviour = SegmentSeekerCrawler({"raw"})
targets_templated = True
# NOTE: This rule does return fixes, but when is_fix_compatible
# is set, it spirals.
# is_fix_compatible = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

???

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made a separate issue for this: #4474.

I left the comment there in case someone else tries to do the same as what I tried.

Copy link
Member

@barrywhart barrywhart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉💯

@alanmcruickshank alanmcruickshank merged commit 70e2d45 into main Mar 7, 2023
@alanmcruickshank alanmcruickshank deleted the ac/format_cli branch March 7, 2023 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add 'reflow' capability for configurable, black-like deterministic formatting
3 participants