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

Adding SQL format checking via pg_format / pgFormatter #3740

Merged
merged 13 commits into from
Aug 2, 2023
Merged

Conversation

dessalines
Copy link
Member

This adds:

  • A command to format all SQL files at the end of fix-clippy.sh . (pg_format must be installed on your system for that)
  • A script called sql_format_check.sh that uses diff to check the before and after run. (since pg_format has no --check)
  • A woodpecker job to install pgformatter on alpine and run that script.

cc @phiresky

@phiresky
Copy link
Collaborator

phiresky commented Jul 27, 2023

Awesome! pg_format has a few flags that could make it align more with the current style of most SQL queries in the repo.

My preferred params are --keyword-case 1 --nogrouping but idk.

Some of the queries look uglier now than before, but it should be worth the price.

@dessalines
Copy link
Member Author

I tried out --nogrouping, it only affected a few trigger creation line wraps, so not too necessary.

The case one tho, I'm torn, because lower case keywords do look better IMO. I usually prefer to go with the "opinionated defaults" of these formatters, so that people can just run them without any additional config. @Nutomic wanna be the tie-breaker?

@phiresky
Copy link
Collaborator

I don't care too much tbh about the specific formatting.

It does also have a --config config file format https://github.com/darold/pgFormatter/blob/master/doc/pg_format.conf.sample though it doesn't automatically use a file in the current dir.

@Nutomic
Copy link
Member

Nutomic commented Jul 28, 2023

I have no problem with uppercase keywords, it can also make queries a bit easier to read.

FROM
comment
WHERE
comment.post_id = p.id) AS number_of_comments,
Copy link
Member

Choose a reason for hiding this comment

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

However this kind of formatting isnt good at all. Makes queries almost unreadable with every word on a separate line. Anyway this can be changed?

Copy link
Member Author

Choose a reason for hiding this comment

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

It might be able to be tweaked, but I do find it readable. More than anything I'd like to keep the defaults that pg_format uses, just like we don't tweak prettier or any other formatter.

@Nutomic
Copy link
Member

Nutomic commented Aug 1, 2023

@phiresky You need to approve this to merge.

TMP_FILE="/tmp/tmp_pg_format.sql"
pg_format $FILE > $TMP_FILE
diff $FILE $TMP_FILE
done
Copy link
Member

Choose a reason for hiding this comment

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

Leave a comment that this is only used for ci.

taplo format

# Format sql files
find migrations -type f -name '*.sql' -exec pg_format -i {} +
Copy link
Member

Choose a reason for hiding this comment

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

Bit confusing to have this formatting stuff in a file named fix-clippy. Would make sense to rename it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good. I'll rename it lint.sh for now.

Copy link
Collaborator

@phiresky phiresky left a comment

Choose a reason for hiding this comment

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

Looks good. The way the files are found is different in fix-clippy and in the CI script now which might be non-ideal

@dessalines
Copy link
Member Author

dessalines commented Aug 2, 2023

I'll just get rid of that git-ls to keep them uniform. We don't have any sql files outside there anyway.

@dessalines dessalines enabled auto-merge (squash) August 2, 2023 16:11
@dessalines dessalines merged commit be13894 into main Aug 2, 2023
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.

3 participants