Giter VIP home page Giter VIP logo

flymake-pgsanity's Introduction

flymake-pgsanity and SQL linting beyond Emacs

Lint your SQL (and even funky templated SQL!) in Emacs with flymake. AFAIK this is the only implementation of an SQL linter for flymake (even though it's very simple). Also included here is a recipe for running such linters in CI (eg, Github Actions). And some bonus syntax highlighting tips.

This does not attempt to edit code -- just to identify (with squiggly lines) problems as you type them.

You'll need pgsanity for any of this to work. It's broadly available via any package manager. And this is PostgreSQL-only.

Emacs linting

There are already similar SQL linters available for flycheck, but I've been trying to get everything I use onto the built-in flymake.

To use with your Emacs, put flymake-pgsanity.el onto your load-path, and:

;; (add-to-list 'load-path "~/.../vendor") ; wherever you keep non-melpa additions
(require 'flymake-pgsanity)
(add-hook 'sql-mode-hook 'flymake-pgsanity-setup)

Then freshly open a .sql file and it should start highlighting any errors.

If you want to use a different linter/script, customize flymake-pgsanity-program. Eg, set it to hugslint (after putting it on your path) if you use Hugs.

SQL-like files (HugSQL, PugSQL, etc)

(If you are only interested in editing/checking of straight SQL files, ignore this section.)

The whole reason I started this effort was for some silly mistakes I'd been making in tweaking HugSQL .sql files. The errors would have been immediately caught by a linter (instead of at runtime!), if only there was one.

The trick is having a very simple preprocessor (sed one-liner script, included) that can convert the special :foo-bar parameters into something that a standard SQL linter can handle. I tried converting them all to basic strings like 'foo-bar-XXX' and it worked! Yes, it also supports those weird params like :v*:so-weird.

The other necessary bits to make pgsanity happy involve you manually "improving" your Hugs files:

  • manually add semicolons (;) to the ends of each SQL statement, which pgsanity needs and Hugs doesn't mind

  • don't end with a dangling WHERE

For that last case, here's an example:

problem:
WHERE
--~ (if ... "foo = :foo" "bar = :bar")

fix:
WHERE TRUE
--~ (if ... "AND foo = :foo" "AND bar = :bar")

In CI

Here's a recipe for running pgsanity in Github Actions. This installs the dependencies, ecpg and pgsanity, and a custom pgsanity-wrapper linter (which you'll edit to suit your needs) that will reject the build.

jobs:
  checks:

    - name: Install ecpg Postgres FE and pgsanity SQL linter
      run: |
        sudo apt-get install libecpg-dev
        sudo pip install pgsanity

    ...

    - name: Check for any lint warnings/errors in sql files (pgsanity)
      run: ./deploy/bin/pgsanity-ci.sh

Emacs syntax highlighting

You can make your special :foo-bar params in SQL files stand out (bold blue) with this:

(defface sql-field '((t (:foreground "#528fd1" :weight ultra-bold))) "My SQL Field")
(font-lock-add-keywords 'sql-mode '((" :\\(v\\*:\\)?[-a-z0-9?]+"  0 'sql-field t)))

;; Other Hugs goodies
(font-lock-add-keywords 'sql-mode '(("-- :doc .*" 0 'doc-field t)))
(font-lock-add-keywords 'sql-mode '(("-- :name [^:]+" 0 'special-comment t)))
(font-lock-add-keywords 'sql-mode '((" \\(:\\*\\|:!\\|:n\\|:\\?\\|:1\\)" 0 'boolean-true t)))

I suppose it'd be nice to color the list (:v*:...) types differently.

Other related/interesting projects

flymake-pgsanity's People

Contributors

micahelliott avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.