需要了解导致 sqlfluff 的预提交挂钩部分出现问题的原因
虽然安装了 sqlfluff-templater-dbt,但安装了 sqlfluff,但出现错误,模板程序“dbt”当前不可用。
这是我的 sqlfluff 和重新提交挂钩文件
pre-commit run sqlfluff-lint
sqlfluff-lint............................................................Failed
- hook id: sqlfluff-lint
- exit code: 2
Starting in sqlfluff version 0.7.0 the dbt templater is distributed as a separate python package. Please pip install sqlfluff-templater-dbt to use it.
Error loading config: Requested templater 'dbt' which is not currently available. Try one of raw, jinja, python, placeholder
#.sqlfluff
[sqlfluff]
# Supported dialects https://docs.sqlfluff.com/en/stable/dialects.html
# Or run 'sqlfluff dialects'
dialect = postgres
# One of [raw|jinja|python|placeholder]
templater = dbt
sql_file_exts = .sql,.sql.j2,.dml,.ddl
# Comma separated list of rules to exclude, or None
# See https://docs.sqlfluff.com/en/stable/configuration.html#enabling-and-disabling-rules
# AM04 (ambiguous.column_count) and ST06 (structure.column_order) are
# two of the more controversial rules included to illustrate usage.
exclude_rules = ambiguous.column_count, structure.column_order
# The standard max_line_length is 80 in line with the convention of
# other tools and several style guides. Many projects however prefer
# something a little longer.
# Set to zero or negative to disable checks.
max_line_length = 140
# CPU processes to use while linting.
# The default is "single threaded" to allow easy debugging, but this
# is often undesirable at scale.
# If positive, just implies number of processes.
# If negative or zero, implies number_of_cpus - specified_number.
# e.g. -1 means use all processors but one. 0 means all cpus.
processes = 0
# If using the dbt templater, we recommend setting the project dir.
[sqlfluff:templater:dbt]
project_dir = ./
profiles_dir = .dbt/
profile = my_transformation
target = prod
[sqlfluff:templater:jinja]
apply_dbt_builtins = True
#.pre-commit-config.yaml
repos:
- repo: https://github.com/sqlfluff/sqlfluff
rev: 3.0.7 # Use the version you want
hooks:
- id: sqlfluff-lint
name: SQLFluff lint
entry: sqlfluff lint
language: python
files: \.sql$
- id: sqlfluff-fix
name: SQLFluff fix
entry: sqlfluff fix
language: python
files: \.sql$
为什么这么说
Error loading config: Requested templater 'dbt' which is not currently available. Try one of raw, jinja, python, placeholder
但是跑步时
sqlfluff lint my_sql_file.sql
或
sqlfluff fix my_sql_file.sql
它工作正常,请在预提交挂钩部分解释问题是什么
预提交在隔离环境(不是您的环境!)中安装工具,以避免干扰您的项目
在这种情况下,它会将
sqlfluff
安装到自己的隔离环境中,因此您正在运行的 sqlfluff
命令与其运行的命令无关
为了使用
dbt
,您需要按照 sqlfluff 的 文档 来配置 additional_dependencies
以安装您需要的任何辅助 sqlfluff 插件(如 dbt
的插件!)
免责声明:我写了预提交