Commit cdc18e59 authored by Cédric OLIVIER's avatar Cédric OLIVIER
Browse files

Merge branch 'sqlfluff' into 'master'

feat: add SQLFluff for dbt project

See merge request to-be-continuous/dbt!4
parents 9fb8bfa2 a91edfa6
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -132,6 +132,19 @@ This job performs **build, doc generation** and documentation **coverage**.
`dbt-build` generates executable SQL from source model, test, and analysis files


### `dbt-sqlfluff-lint` job

This job performs **SQL Lint**.

`dbt-sqlfluff-lint` execute [sqlfluff linter with dbt plugin](https://pypi.org/project/sqlfluff-templater-dbt/) to lint SQL and uses the following variables:

| Name                     | description                            | default value     |
| ------------------------ | -------------------------------------- | ----------------- |
| `DBT_SQLFLUFF_ENABLED`   | set to `true` to enable SQLFluff lint  | _none_ (disabled) |
| `DBT_SQLFLUFF_LINT_ARGS` | Lint [options and arguments](https://docs.sqlfluff.com/en/stable/cli.html#sqlfluff-lint) | _none_ |

:warning: this jobs read [SQLFluff configuration files](https://docs.sqlfluff.com/en/stable/configuration.html) in `DBT_PROJECT_DIR` directory.

### `dbt-deploy` job

This job performs **deployment**.
+19 −0
Original line number Diff line number Diff line
@@ -35,6 +35,25 @@
    }
  ],
  "features": [
    {
      "id": "dbt-sqlfluff-lint",
      "name": "SQLFluff lint",
      "description": "Lint SQL from dbt project",
      "enable_with": "DBT_SQLFLUFF_ENABLED",
      "variables": [
        {
          "name": "DBT_SQLFLUFF_LINT_ARGS",
          "description": "Lint [options and arguments](https://docs.sqlfluff.com/en/stable/cli.html#sqlfluff-lint)",
          "advanced": true
        },
        {
          "name": "SQLFLUFF_WORKING_DIR",
          "description": "Sqlfluff working directory, scope of configuration and sql files used",
          "default": ".",
          "advanced": true
        }    
      ]
    },
    {
      "id": "dbt-deploy",
      "name": "dbt deploy",
+18 −0
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ variables:
  DBT_PROJECT_DIR: "."
  DBT_PROFILES_DIR: "."


  # default production ref name (pattern)
  PROD_REF: '/^(master|main)$/'
  # default integration ref name (pattern)
@@ -296,6 +297,11 @@ stages:
    then
      pip install "dbt-${DBT_ADAPTER}" dbt-coverage
    fi

    if ! command -v sqlfluff > /dev/null
    then
      pip install sqlfluff-templater-dbt
    fi
  }

  unscope_variables
@@ -323,6 +329,18 @@ stages:
    paths:
      - .cache/

dbt-sqlfluff-lint:
  extends: .dbt-base
  stage: build
  script: 
    - cd "${DBT_PROJECT_DIR}"
    - sqlfluff lint ${TRACE+--verbose} --format human $DBT_SQLFLUFF_LINT_ARGS
  rules:
    - if: '$DBT_SQLFLUFF_ENABLED != "true"'
      when: never
    - !reference [.test-policy, rules]

    

# compile job
.dbt-build: