akoutmos / sql_fmt

Format and pretty print SQL queries
https://hex.pm/packages/sql_fmt
MIT License
48 stars 2 forks source link

Add a `mix sql_fmt.format` task #2

Closed pnezis closed 1 month ago

pnezis commented 1 month ago

Similar to mix format but for SQL queries 😄 .

image

The code is mostly "stolen" from mix format. We already use pgformatter in our CI pipelines, we could replace it with this.

If you are ok with adding the mix task, I will add tests and CLI flags for the supported formatting options.

akoutmos commented 1 month ago

That would be a really cool use case (and one that I didn't think about)! Definitely would be interested in merging this one when you button it up.

I have also used pgformatter in the past as well.....but it is much slower given that it is written in perl and also chokes on very large queries.

pnezis commented 1 month ago

@akoutmos ready

akoutmos commented 1 month ago

This hit me just as I was reviewing the code. How about instead of copying in the mix format mix task we instead make this a mix format plugin? https://hexdocs.pm/mix/main/Mix.Tasks.Format.html#module-plugins

That way we lean on the existing mix tooling and --check-formatted will work just the same. That and we can also create a ~SQL """...""" sigil for formatting SQL statements which would be quite handy in migrations. I always hate manually formatting SQL statements in migrations and a sigil would tie in nicely with the mix format plugin.

Thoughts?

pnezis commented 1 month ago

I like it, new PR coming soon. My only concern with the ~SQL sigil is that it will make :sql_fmt a required prod dependency as well, I planned to have it only as :dev, but this is minor.

pnezis commented 1 month ago

Closing in favour of #3