Skip to main content

build status Documentation Status PyPI version

Catch unsafe SQL migrations.

$ squabble sql/migration.sql
sql/migration.sql:4:46 ERROR: column "uh_oh" has a disallowed constraint [1004]
ALTER TABLE big_table ADD COLUMN uh_oh integer DEFAULT 0;
                                               ^
# Use --explain to get more information on a lint violation
$ squabble --explain 1004
ConstraintNotAllowed
     When adding a column to an existing table, certain constraints can have
     unintentional side effects, like locking the table or introducing
     performance issues.
     ...

Squabble can also be integrated with your editor to catch errors in SQL files.

$ echo 'SELECT * FROM WHERE x = y;' | squabble --reporter=plain
stdin:1:15 CRITICAL: syntax error at or near "WHERE"

Currently, most of the rules have been focused on Postgres and its quirks. However, squabble can parse any ANSI SQL and new rules that are specific to other databases are appreciated!

Installation

$ pip3 install squabble
$ squabble --help

If you’d like to install from source:

$ git clone https://github.com/erik/squabble.git && cd squabble
$ python3 -m venv ve && source ve/bin/activate
$ python setup.py install
$ squabble --help

Configuration

To see a list of rules, try

$ squabble --list-rules

Then, to show more verbose information about a rule (such as rationale and configuration options)

$ squabble --show-rule AddColumnsDisallowConstraints

Once a configuration file is in place, it can be passed explicitly on the command line, or automatically looked up.

$ squabble -c path/to/config ...

If not explicitly given on the command line, squabble will look for a file named .squabblerc in the following places (in order):

  • ./.squabblerc

  • (git_repo_root)/.squabblerc

  • ~/.squabblerc

Per-File Configuration

Configuration can also be applied at the file level by using SQL line comments in the form -- enable:RuleName or -- disable:RuleName.

For example, to disable RuleA and enable RuleB just for one file, this could be done:

-- disable:RuleA
-- enable:RuleB config=value array=1,2,3
SELECT email FROM users WHERE ...;

Example Configuration

{
  "reporter": "color",

  "plugins": [
    "/some/directory/with/custom/rules"
  ],

  "rules": {
    "AddColumnsDisallowConstraints": {
      "disallowed": ["DEFAULT", "FOREIGN", "NOT NULL"]
    }
  }
}

Prior Art

squabble is of course not the first tool in this space. If it doesn’t fit your needs, consider one of these tools:

  • sqlcheck - regular expression based (rather than parsing), focuses more on SELECT statements than migrations.

  • sqlint - checks that the syntax of a file is valid. Uses the same parsing library as squabble.

  • sqlfluff - focused more on style and formatting, seems to still be a work in progress.

Acknowledgments

This project would not be possible without:

  • libpg_query - Postgres query parser

  • pglast - Python bindings to libpg_query

  • Postgres - …obviously

The logo image used in the documentation is created by Gianni - Dolce Merda from the Noun Project.

Release files for squabble 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for squabble 1.3.0
File Size Uploaded
squabble-1.3.0.tar.gz 30.4 kB Details

Release files / squabble-1.3.0.tar.gz

Download URL squabble-1.3.0.tar.gz
Size 30.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8d99357678c95c33f4b31996b9d641cb4f9ef1c317ba1218a13fe333b09ee4e6
BLAKE2b-256 checksum
How to use checksums
786af6aa23f91d55181f2da27107415198bdfe4eca2f4b210be305647dad3cee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.7
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page