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 sql linter #390

Open
mxfactorial opened this issue Nov 8, 2024 · 0 comments
Open

add sql linter #390

mxfactorial opened this issue Nov 8, 2024 · 0 comments

Comments

@mxfactorial
Copy link
Contributor

current

the sql files in migrations/schema, migrations/seed and migrations/testseed are not linted

expected

1. lint sql files with https://hub.docker.com/r/sqlfluff/sqlfluff

example commands:

i) single file

docker run -it --rm -v $PWD:/sql sqlfluff/sqlfluff:3.2.5 lint --dialect postgres migrations/schema/000001_account.up.sql

ii) directories

docker run -it --rm -v $PWD:/sql sqlfluff/sqlfluff:3.2.5 lint --dialect postgres migrations/schema

2. using the --fix flag adds zero changes to linted sql files

example:
assuming migrations/schema/test.sql

CREATE TABLE account (
    name character varying(255) NOT NULL PRIMARY KEY,
    password text,
    created_by character varying(255), -- NOT NULL == subaccount
    created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP
);

docker run -it --rm -v $PWD:/sql sqlfluff/sqlfluff:3.2.5 lint --dialect postgres migrations/schema/test.sql returns 0 errors

3. add a migrations/.sqlfluff config file with contents:

[sqlfluff]

dialect = postgres

now commands require a --config instead of a --dialect flag

docker run -it --rm -v $PWD:/sql sqlfluff/sqlfluff:3.2.5 lint --config migrations/.sqlfluff migrations/schema/test.sql

doc: https://docs.sqlfluff.com/en/stable/reference/cli.html

4. add "sql linting" section in migrations/README.md documenting example lint commands

example:

docker run -it --rm -v $PWD:/sql sqlfluff/sqlfluff:3.2.5 lint --config migrations/.sqlfluff migrations/schema
docker run -it --rm -v $PWD:/sql sqlfluff/sqlfluff:3.2.5 lint --config migrations/.sqlfluff migrations/seed
docker run -it --rm -v $PWD:/sql sqlfluff/sqlfluff:3.2.5 lint --config migrations/.sqlfluff migrations/testseed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

1 participant