pgrubic
pgrubic is a PostgreSQL linter and formatter for schema migrations and design best practices.
Features
- Over 100+ rules
- Automatic violation correction (e.g., automatically add
concurrentlyto index create statements) - River style code formatting for DML statements
- Almost identical styling with pg_dump for DDL statements
- Python 3.12+ compatibility
- Automatic caching to avoid reformatting unchanged files
- Violations suppression, statement level, and file level
Getting Started
For more, see the documentation.
Installation
pip install pgrubic
pgrubic is only supported on Python 3.12 or higher.
Usage
For linting, try any of the following:
pgrubic lint # Lint SQL files in the current directory (and any subdirectories)
pgrubic lint . # Lint SQL files in the current directory (and any subdirectories)
pgrubic lint directory # Lint SQL files in *directory* (and any subdirectories)
pgrubic lint directory/*.sql # Lint SQL files in *directory*
pgrubic lint directory/file.sql # Lint `file.sql` in *directory*
pgrubic lint file.sql # Lint `file.sql`
pgrubic lint directory/*.sql --fix # Lint SQL files in *directory* and fix violations automatically
pgrubic lint file.sql --fix # Lint `file.sql` and fix fixable violations automatically
Sample output from linting:
pgrubic lint *.sql
file.sql:1:38: TP017: Boolean field should be not be nullable
1 | ALTER TABLE public.example ADD COLUMN foo boolean DEFAULT false;
pgrubic file.sql
test.sql:1:38: TP017: Boolean field should be not be nullable
1 | ALTER TABLE public.example ADD COLUMN foo boolean DEFAULT false;
For formatting, try any of the following:
pgrubic format # Format SQL files in the current directory (and any subdirectories)
pgrubic format . # Format SQL files in the current directory (and any subdirectories)
pgrubic format directory # Format SQL files in *directory* (and any subdirectories)
pgrubic format directory/*.sql # Format SQL files in *directory*
pgrubic format directory/file.sql # Format `file.sql` in *directory*
pgrubic format file.sql # Format `file.sql`
pgrubic format directory/*.sql --check # Check if SQL files would have been modified, returning a non-zero exit code
pgrubic format file.sql --diff # Report if `file.sql` would have been modified, returning a non-zero exit code as well the difference between `file.sql` and how the formatted file would look like
pgrubic can also be used as a pre-commit hook:
- repo: https://github.com/bolajiwahab/pgrubic
rev: 2.0.0
hooks:
- id: pgrubic-lint
- id: pgrubic-format
Configuration
pgrubic can be configured via the [pgrubic.toml] file in either the current directory, up to the root directory or the path set by the PGRUBIC_CONFIG_PATH environment variable.
The following configuration options are available in the [pgrubic.toml] with the following defaults:
cache-dir = ".pgrubic_cache"
include = []
exclude = []
respect-gitignore = true
[lint]
target-postgres-version = 14
additional-non-volatile-functions = []
select = []
ignore = []
include = []
exclude = []
ignore-noqa = false
disallowed-schemas = []
allowed-extensions = []
allowed-languages = []
fix = false
fixable = []
unfixable = []
disallowed-data-types = []
required-columns = []
timestamp-column-suffix = "_at"
date-column-suffix = "_date"
regex-partition = "^.+$"
regex-index = "^.+$"
regex-constraint-primary-key = "^.+$"
regex-constraint-unique-key = "^.+$"
regex-constraint-foreign-key = "^.+$"
regex-constraint-check = "^.+$"
regex-constraint-exclusion = "^.+$"
regex-sequence = "^.+$"
[format]
include = []
exclude = []
comma-at-beginning = true
compact-parenthesized-lists-margin = 90
uppercase-keywords = true
type-casting-style = "standard"
rewrite-function-calls-as-equivalent-syntax = true
new-line-before-semicolon = false
remove-pg-catalog-from-functions = true
remove-default-index-access-method = true
lines-between-statements = 1
check = false
diff = false
no-cache = false
Some configuration options can be supplied via CLI arguments such as --check, --diff, --fix.
pgrubic format --check
pgrubic format --diff
pgrubic lint --fix
Rules
There are 100+ rules. All rules are enabled by default. For a complete list, see here.
Formatting style
pgrubic uses River style code formatting.
Contributing
We welcome and greatly appreciate contributions. If you would like to contribute, please see the contributing guidelines.
Support
Encountering issues? Take a look at the existing GitHub issues, and don't hesitate to open a new one.
Acknowledgments
pgrubic is inspired by a number of similar tools such as Strong Migrations, squabble, squawk, pgextwlist, Don't_Do_This and schemalint.
pgrubic is built upon the shoulders of:
- pglast - Python bindings to libpg_query
- libpg_query - PostgreSQL parser outside of the server environment
License
pgrubic is released under GPL-3.0 license.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pgrubic-2.0.0.tar.gz.
File metadata
- Download URL: pgrubic-2.0.0.tar.gz
- Upload date:
- Size: 123.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47d13094d8a0f293591d4a2a15532d57565f90ba7dff1b253af82f4e58011a64
|
|
| MD5 |
dd9e6fdefcd7e0ee5115ff11e4bc00d0
|
|
| BLAKE2b-256 |
6f2e1131685ac6f2ab4c2e7858ea3d3f84f721285ceb9cb1e76b8cb796f3b04c
|
Provenance
The following attestation bundles were made for pgrubic-2.0.0.tar.gz:
Publisher:
release.yml on bolajiwahab/pgrubic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pgrubic-2.0.0.tar.gz -
Subject digest:
47d13094d8a0f293591d4a2a15532d57565f90ba7dff1b253af82f4e58011a64 - Sigstore transparency entry: 2566268965
- Sigstore integration time:
-
Permalink:
bolajiwahab/pgrubic@732d47dfb3bbb0032b3f98a392281a1e8d6a4993 -
Branch / Tag:
refs/tags/2.0.0 - Owner: https://github.com/bolajiwahab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@732d47dfb3bbb0032b3f98a392281a1e8d6a4993 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pgrubic-2.0.0-py3-none-any.whl.
File metadata
- Download URL: pgrubic-2.0.0-py3-none-any.whl
- Upload date:
- Size: 197.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3e8a441b9e5a4fb4536abb2024fb887bb48f6745124d45f23e9ea7bdd4b07a7
|
|
| MD5 |
8e326596a67bba27cb4dba10ef6aa361
|
|
| BLAKE2b-256 |
8d27dc3a80f70629af23a5761e8b391db66ccc40e78b5e4a29ed0e9a2f3379b7
|
Provenance
The following attestation bundles were made for pgrubic-2.0.0-py3-none-any.whl:
Publisher:
release.yml on bolajiwahab/pgrubic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pgrubic-2.0.0-py3-none-any.whl -
Subject digest:
a3e8a441b9e5a4fb4536abb2024fb887bb48f6745124d45f23e9ea7bdd4b07a7 - Sigstore transparency entry: 2566269016
- Sigstore integration time:
-
Permalink:
bolajiwahab/pgrubic@732d47dfb3bbb0032b3f98a392281a1e8d6a4993 -
Branch / Tag:
refs/tags/2.0.0 - Owner: https://github.com/bolajiwahab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@732d47dfb3bbb0032b3f98a392281a1e8d6a4993 -
Trigger Event:
push
-
Statement type: