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 SQLFluff /format endpoint to the server #4

Merged
merged 7 commits into from
Feb 7, 2024

Conversation

barrywhart
Copy link

@barrywhart barrywhart commented Dec 12, 2023

How to use it:

# Launch the server
python src/dbt_core_interface/project.py

# Register the dbt project with the server (one-time only). Providing absolute file
# paths to the project and profile directory is best.
curl -X POST \
     -H "Content-Type: application/json" \
     -H "X-dbt-Project: dbt_project" \
     -d '{
       "project_dir": "/Users/barry/dev/dbt-core-interface/tests/sqlfluff_templater/fixtures/dbt/dbt_project",
       "profiles_dir": "/Users/barry/dev/dbt-core-interface/tests/sqlfluff_templater/fixtures/dbt/profiles_yml",
       "target": "dev"
     }' \
     http://localhost:8581/register

# Format the file. Providing an absolute file path is best.
curl -X POST \
     -H "Content-Type: application/json" \
     -H "X-dbt-Project: dbt_project" \
     "http://localhost:8581/format?sql_path=/Users/barry/dev/dbt-core-interface/tests/sqlfluff_templater/fixtures/dbt/dbt_project/models/my_new_project/issue_1608.sql"

@barrywhart barrywhart marked this pull request as draft December 12, 2023 20:23
@barrywhart barrywhart requested a review from noel December 12, 2023 20:56
@BAntonellini
Copy link

@barrywhart I can't make /format work, it says local variable 'res' referenced before assignment, any clue?

image

@barrywhart
Copy link
Author

barrywhart commented Dec 18, 2023

@BAntonellini: I see the issue. I have this code:

        num_fixable = lint_result.num_violations(types=SQLLintError, fixable=True)
        if num_fixable > 0:
            res = lint_result.persist_changes(
                formatter=formatter, fixed_file_suffix=""
            )
        success = all(res.values())

The success = line accesses res, but if num_fixable is 0, the variable is undefined. Easy fix -- I'll push that later today.

@barrywhart
Copy link
Author

@BAntonellini: I fixed the bug. The behavior you saw would occur if the "format" endpoint found there were no changes to be made (or more precisely, no automated changes).

@barrywhart barrywhart marked this pull request as ready for review December 18, 2023 17:17
@BAntonellini BAntonellini merged commit f55bec1 into datacoves:main Feb 7, 2024
16 checks passed
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.

2 participants