Editor and CI Integrations

Copy-paste recipes for using sqlriver in your development workflow.

Pre-commit Hooks

Husky (npm projects)

npm install --save-dev husky
npx husky init

Add to .husky/pre-commit:

npx sqlriver --check $(git diff --cached --name-only --diff-filter=ACM -- '*.sql')

pre-commit framework

Add to .pre-commit-config.yaml:

repos:
  - repo: https://github.com/vinsidious/sqlriver
    rev: ""  # run `pre-commit autoupdate` to pin the latest version
    hooks:
      - id: sqlriver
        name: sqlriver
        entry: npx sqlriver --write
        language: node
        types: [sql]
      - id: sqlriver-check
        name: sqlriver (check only)
        entry: npx sqlriver --check
        language: node
        types: [sql]

Then run:

pre-commit install

Use sqlriver to auto-fix files on commit, or sqlriver-check to fail without modifying files (useful in CI).

GitHub Actions

name: SQL Format Check
on: [push, pull_request]

jobs:
  sqlriver:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - run: npm install -g sqlriver
      - run: sqlriver --check "**/*.sql"

Note: Quote the glob pattern ("**/*.sql") to prevent shell expansion. Without quotes, your shell may expand the glob before sqlriver sees it, which can miss files in subdirectories or fail if no .sql files exist in the current directory.

GitLab CI

sqlriver:
  image: node:20
  stage: lint
  script:
    - npm install -g sqlriver
    - sqlriver --check "**/*.sql"
  rules:
    - changes:
        - "**/*.sql"

VS Code

Run on Save (recommended)

Use the Run on Save extension to format SQL files automatically:

{
  "emeraldwalk.runonsave": {
    "commands": [
      {
        "match": "\\.sql$",
        "cmd": "npx sqlriver --write ${file}"
      }
    ]
  }
}

Vim / Neovim

formatprg

" In .vimrc or init.vim
autocmd FileType sql setlocal formatprg=npx\ sqlriver

Then use gq to format a selection, or gggqG to format the entire file.

ALE

" In .vimrc or init.vim
function! FormatWithSqlriver(buffer) abort
    return { 'command': 'npx sqlriver' }
endfunction

execute ale#fix#registry#Add('sqlriver', 'FormatWithSqlriver', ['sql'], 'sqlriver formatter')

let g:ale_fixers = {
\   'sql': ['sqlriver'],
\}
let g:ale_fix_on_save = 1

Neovim (conform.nvim)

require("conform").setup({
  formatters_by_ft = {
    sql = { "sqlriver" },
  },
  formatters = {
    sqlriver = {
      command = "npx",
      args = { "sqlriver" },
      stdin = true,
    },
  },
})

IntelliJ / DataGrip

Configure as an External Tool:

  1. Go to Settings > Tools > External Tools
  2. Click + to add a new tool:
    • Name: sqlriver
    • Program: npx
    • Arguments: sqlriver --write $FilePath$
    • Working directory: $ProjectFileDir$
  3. Optionally assign a keyboard shortcut under Settings > Keymap > External Tools > sqlriver

To check formatting without modifying files:

  • Arguments: sqlriver --check $FilePath$

npm Scripts

Add these to your project's package.json:

{
  "scripts": {
    "sql:format": "sqlriver --write \"**/*.sql\"",
    "sql:check": "sqlriver --check \"**/*.sql\""
  }
}

Note: Quote glob patterns inside npm scripts to prevent shell expansion on Linux/macOS. The escaped double quotes (\"**/*.sql\") ensure the pattern is passed to sqlriver as-is.

Then run:

npm run sql:format   # format all SQL files in place
npm run sql:check    # check formatting (CI-friendly, exits non-zero if unformatted)