bolajiwahab/pgrubic
GitHub: bolajiwahab/pgrubic
pgrubic 是一个用于 PostgreSQL schema 迁移的 SQL 检查与格式化工具,帮助团队在数据库变更中自动执行最佳实践并保持代码风格一致。
Stars: 16 | Forks: 3
# pgrubic
[](https://github.com/bolajiwahab/pgrubic/)
[](https://pypi.org/project/pgrubic/)
[](https://pypi.org/project/pgrubic/)
[](https://github.com/bolajiwahab/pgrubic/blob/main/LICENSE)
[](https://pypi.org/project/pgrubic/)
[](https://github.com/bolajiwahab/pgrubic/actions/workflows/ci.yml)
[](https://github.com/bolajiwahab/pgrubic/tree/python-coverage-comment-action-data)
[](https://github.com/bolajiwahab/pgrubic/actions/workflows/doc.yml)
[](https://github.com/bolajiwahab/pgrubic/actions/workflows/release.yml)
[](https://pepy.tech/projects/pgrubic)
[](https://github.com/bolajiwahab/pgrubic/actions/workflows/github-code-scanning/codeql)
[](https://github.com/pre-commit/pre-commit)
[](https://github.com/charliermarsh/ruff)
[](https://github.com/python/mypy)
[](https://github.com/PyCQA/bandit)
[](https://socket.dev/pypi/package/pgrubic/overview/)
[](https://github.com/bolajiwahab/pgrubic/actions/workflows/dependency-review.yml)
pgrubic 是一个用于 schema 迁移和设计最佳实践的 PostgreSQL linter 和格式化工具。
## 功能
- 100 多条规则
- 自动修正违规项(例如,自动为 index create 语句添加 `concurrently`)
- 针对 DML 语句的 River 风格代码格式化
- 针对 DDL 语句的样式与 **pg_dump** 几乎完全相同
- 兼容 Python 3.12+
- 自动缓存以避免重新格式化未更改的文件
- 支持抑制违规项,包括语句级别和文件级别
## 快速开始
了解更多信息,请参阅[文档](https://bolajiwahab.github.io/pgrubic/)。
## 安装
```
pip install pgrubic
```
**pgrubic 仅支持在 Python 3.12 或更高版本上运行**。
## 用法
要进行 linting,请尝试以下任一命令:
```
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
```
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;
```
要进行格式化,请尝试以下任一命令:
```
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 还可以用作 pre-commit hook:
```
- repo: https://github.com/bolajiwahab/pgrubic
rev: 1.2.1
hooks:
- id: pgrubic-lint
- id: pgrubic-format
```
## 配置
pgrubic 可以通过 [`pgrubic.toml`] 文件进行配置,该文件可以位于当前目录、根目录,或者通过 `PGRUBIC_CONFIG_PATH` 环境变量设置的路径中。
以下配置选项可在 [`pgrubic.toml`] 中使用,并具有以下默认值:
```
# 缓存目录的路径
cache-dir = ".pgrubic_cache"
# 默认包含所有文件
include = []
# 默认不排除任何文件
exclude = []
[lint]
# 默认目标版本为 PostgreSQL 14
target-postgres-version = 14
# 额外的非易失性函数
additional-non-volatile-functions = []
# 默认启用所有规则
select = []
# 默认不禁用任何规则
ignore = []
# 默认包含所有文件
include = []
# 默认不排除任何文件
exclude = []
# 默认忽略标记为 `noqa` 的抑制违规
ignore-noqa = false
# 不允许的 schemas
disallowed-schemas = []
# 允许的 extensions
allowed-extensions = []
# 允许的 languages
allowed-languages = []
# 不自动修复违规
fix = false
# 将所有规则视为可修复
fixable = []
# 将所有规则视为可修复
unfixable = []
# 不允许的数据类型
disallowed-data-types = []
# 必需的列
required-columns = []
# 默认为 Timestamp 列添加后缀 `_at`
timestamp-column-suffix = "_at"
# 默认为 Date 列添加后缀 `_date`
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
# 默认在分号前不换行
new-line-before-semicolon = false
# 默认从函数中移除 pg_catalog
remove-pg-catalog-from-functions = true
# 按一定数量的空行分隔语句,默认为 1
lines-between-statements = 1
# 检查文件是否会被修改,并返回非零退出代码
check = false
# 报告文件是否会被修改,返回非零退出代码以及当前文件与格式化后文件之间的差异
diff = false
# 是否读取缓存。
no-cache = false
```
某些配置选项可以通过 CLI 参数提供,例如 `--check`、`--diff`、`--fix`。
```
pgrubic format --check
```
```
pgrubic format --diff
```
```
pgrubic lint --fix
```
## 规则
共有 100 多条规则。所有规则默认启用。完整列表请参见[这里](https://bolajiwahab.github.io/pgrubic/rules/)。
## 格式化样式
pgrubic 使用 **River** 风格进行代码格式化。
## 支持
遇到问题?请查看现有的 GitHub [issues](https://github.com/bolajiwahab/pgrubic/issues),并随时提出新的 issue。
## 许可证
pgrubic 基于 GPL-3.0 许可证发布。
标签:PostgreSQL, Schema迁移, SOC Prime, 代码格式化, 开发工具, 数据库, 测试用例, 逆向工具