Skip to content

For dbt users

The sqlassert-dbt plugin, a separate package in this repository (integrations/dbt/), checks a compiled dbt project from its target/manifest.json (run dbt compile or dbt build first). It reads only the manifest, so dbt itself is not needed where it runs, and only the latest manifest schema (v12, written by dbt 1.8 through 1.12): compile with a current dbt. target/catalog.json (written by dbt docs generate), read when present, adds the columns and types of sources, seeds and other tables the manifest leaves unknown, except where the project shows the catalog stale; --catalog PATH names another.

$ pip install sqlassert-dbt   # depends on sqlassert; sqlassert does not depend on it
$ sqlassert-dbt check . --trust-upstream-tests
models/customers.sql:12: UNKNOWN /**ASSERT NOT NULL(first_name)**/ (customers)

Proved statically (a test here can be skipped):
  models/schema.yml  unique_stg_customers_customer_id  (test on stg_customers)

Not proved (the warehouse run still checks these):
  models/schema.yml  accepted_values_stg_orders_status__placed__shipped__returned  (test on stg_orders): its Property is not proved from the SQL

Proved but untested (schema.yml additions):
  models/schema.yml  customers.customer_id: not_null

Each model is analyzed from its compiled SQL. Its own tests are assertions: unique is a Unique Set, not_null Not Null, accepted_values an Enum (a declaration only of a column whose type compares it with its values by identity: text with text, an integer with integers), relationships Referential Coverage, dbt_utils.unique_combination_of_columns a NOT NULL UNIQUE (it counts NULL equal to NULL), and model contract primary_key, unique, not_null and foreign_key constraints likewise. The tests of the sources, seeds, snapshots and models it reads are declarations when they passed before it was built, as under dbt build: say so with --trust-upstream-tests (otherwise a dbt-untrusted-upstream-tests warning counts them) -- and only tests that fail on any failing row: not one with severity: warn, a where: config, or error_if/fail_calc thresholds; and a constraint only on a contracted table whose adapter enforces it. A test is never a declaration for its own model.

Markers written in model files are checked too, and reported at their line in the model file. An ephemeral model is analyzed where dbt inlined it; an incremental model in both is_incremental() branches, and a test of one holds only in both, and for a key only under a merge or delete+insert strategy on that unique_key with no incremental_predicates. The other branch is rendered without dbt only when the model uses ref, source, this, config and is_incremental() alone; otherwise it is reported unanalyzed. A model with markers but no compiled code is an error, not skipped. The exit code is 1 when a marker is UNKNOWN or an error is reported (tests not proved do not fail it, unless --require-proved), 2 when the manifest cannot be read. Only adapters whose dialect sqlassert models are analyzed; --dialect names one explicitly.

To check the markers as part of a dbt run, and skip the tests the SQL proves, run dbt through the plugin:

$ sqlassert-dbt build . -- --target prod   # or: sqlassert-dbt test

It runs dbt compile, prints the check, then runs dbt build (or dbt test) with every proved test passed as --exclude. dbt runs the upstream tests in that same invocation, so they are trusted, unless the arguments after -- select nodes (--select, --exclude, --selector, ...), which may leave one out. The exit code is dbt's when dbt fails, else that of the check.

To see what sqlassert can prove about the models you are editing, as sqlassert suggest would on plain SQL:

$ sqlassert-dbt suggest models/customers.sql models/stg_orders.sql --explain -- --target dev

It compiles those models and what they read, then lists the rewrites, markers and hazards for each model at its file's lines, with what the model reads declared as check declares it. In an incremental model, a rewrite or marker is listed only when both is_incremental() branches prove it.