sqlassert-dbt 0.3.1__tar.gz → 0.3.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {sqlassert_dbt-0.3.1/sqlassert_dbt.egg-info → sqlassert_dbt-0.3.3}/PKG-INFO +108 -2
- sqlassert_dbt-0.3.3/README.md +234 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/pyproject.toml +2 -1
- sqlassert_dbt-0.3.3/sqlassert_dbt/__init__.py +21 -0
- sqlassert_dbt-0.3.3/sqlassert_dbt/__main__.py +350 -0
- sqlassert_dbt-0.3.3/sqlassert_dbt/connect.py +135 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/sqlassert_dbt/jinja.py +130 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/sqlassert_dbt/manifest.py +56 -2
- sqlassert_dbt-0.3.3/sqlassert_dbt/program.py +1139 -0
- sqlassert_dbt-0.3.3/sqlassert_dbt/project.py +622 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/sqlassert_dbt/report.py +114 -4
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3/sqlassert_dbt.egg-info}/PKG-INFO +108 -2
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/sqlassert_dbt.egg-info/SOURCES.txt +4 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/sqlassert_dbt.egg-info/requires.txt +2 -1
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/sqlassert_dbt.egg-info/scm_file_list.json +5 -1
- sqlassert_dbt-0.3.3/sqlassert_dbt.egg-info/scm_version.json +8 -0
- sqlassert_dbt-0.3.3/tests/conftest.py +43 -0
- sqlassert_dbt-0.3.3/tests/test_catalog.py +146 -0
- sqlassert_dbt-0.3.3/tests/test_connect.py +101 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/tests/test_dbt.py +93 -4
- sqlassert_dbt-0.3.3/tests/test_suggest.py +325 -0
- sqlassert_dbt-0.3.1/README.md +0 -129
- sqlassert_dbt-0.3.1/sqlassert_dbt/__init__.py +0 -19
- sqlassert_dbt-0.3.1/sqlassert_dbt/__main__.py +0 -115
- sqlassert_dbt-0.3.1/sqlassert_dbt/program.py +0 -686
- sqlassert_dbt-0.3.1/sqlassert_dbt/project.py +0 -340
- sqlassert_dbt-0.3.1/sqlassert_dbt.egg-info/scm_version.json +0 -8
- sqlassert_dbt-0.3.1/tests/conftest.py +0 -16
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/LICENSE +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/setup.cfg +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/sqlassert_dbt/expectations.py +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/sqlassert_dbt.egg-info/dependency_links.txt +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/sqlassert_dbt.egg-info/entry_points.txt +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/sqlassert_dbt.egg-info/top_level.txt +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/tests/dbt_project/dbt_project.yml +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/tests/dbt_project/models/active_customers.sql +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/tests/dbt_project/models/customer_keys.sql +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/tests/dbt_project/models/customer_orders.sql +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/tests/dbt_project/models/customer_signups.sql +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/tests/dbt_project/models/customers.sql +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/tests/dbt_project/models/orders_appended.sql +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/tests/dbt_project/models/orders_incremental.sql +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/tests/dbt_project/models/schema.yml +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/tests/dbt_project/models/stg_customers.sql +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/tests/dbt_project/models/stg_orders.sql +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/tests/dbt_project/profiles.yml +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/tests/dbt_project/seeds/raw_payments.csv +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/tests/dbt_runs.py +0 -0
- {sqlassert_dbt-0.3.1 → sqlassert_dbt-0.3.3}/tests/test_contracts.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sqlassert-dbt
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.3
|
|
4
4
|
Summary: Check a dbt project's tests and sqlassert markers from its compiled manifest.
|
|
5
5
|
Author: Fred Israel
|
|
6
6
|
License-Expression: MIT
|
|
@@ -14,11 +14,12 @@ Classifier: Topic :: Software Development :: Quality Assurance
|
|
|
14
14
|
Requires-Python: >=3.13
|
|
15
15
|
Description-Content-Type: text/markdown
|
|
16
16
|
License-File: LICENSE
|
|
17
|
-
Requires-Dist: sqlassert
|
|
17
|
+
Requires-Dist: sqlassert>=0.3.1
|
|
18
18
|
Requires-Dist: sqlglot<31,>=30.30
|
|
19
19
|
Provides-Extra: test
|
|
20
20
|
Requires-Dist: dbt-core>=1.12; extra == "test"
|
|
21
21
|
Requires-Dist: dbt-duckdb>=1.11; extra == "test"
|
|
22
|
+
Requires-Dist: duckdb; extra == "test"
|
|
22
23
|
Requires-Dist: pytest; extra == "test"
|
|
23
24
|
Dynamic: license-file
|
|
24
25
|
|
|
@@ -59,6 +60,10 @@ facts, since `dbt build` runs them first in the same invocation. If the
|
|
|
59
60
|
arguments after `--` select nodes (`--select`, `--exclude`, `--selector`, ...),
|
|
60
61
|
an upstream test may not run, so none is trusted.
|
|
61
62
|
|
|
63
|
+
A `profiles.yml` kept in the project directory is found without
|
|
64
|
+
`--profiles-dir` (by `build`, `test` and `suggest`), unless the working
|
|
65
|
+
directory has one or `DBT_PROFILES_DIR` is set.
|
|
66
|
+
|
|
62
67
|
## Check without dbt
|
|
63
68
|
|
|
64
69
|
`check` reads only `target/manifest.json`, so dbt does not need to be installed
|
|
@@ -94,6 +99,10 @@ Three lists come out:
|
|
|
94
99
|
| `--explain` | Print each proof, and each failed proof. |
|
|
95
100
|
| `--dialect NAME` | Analyze under this dialect instead of the adapter's. |
|
|
96
101
|
| `--manifest PATH` | Read another manifest (default `PROJECT/target/manifest.json`). |
|
|
102
|
+
| `--catalog PATH` | Read column lists and types from this `catalog.json` instead of `PROJECT/target/catalog.json`, which is read when present (also on `build`, `test` and `suggest`). See below. |
|
|
103
|
+
| `--no-catalog` | Read no `catalog.json`: the manifest's columns only. |
|
|
104
|
+
| `--connect` | `build`, `test` and `suggest`: read the tables models read from the warehouse, through dbt's own connection. See below. |
|
|
105
|
+
| `--warning-limit N` | Print at most N warnings of each code, then say the rest are suppressed (default 10; 0 prints all). |
|
|
97
106
|
|
|
98
107
|
Exit codes: `0` every marker proved; `1` a marker is `UNKNOWN` or an error was
|
|
99
108
|
reported (unproved tests fail it only with `--require-proved`); `2` the
|
|
@@ -115,6 +124,41 @@ only when they fail on any failing row: not a test with `severity: warn`, a
|
|
|
115
124
|
`where:` config, or `error_if`/`fail_calc` thresholds. A constraint counts only
|
|
116
125
|
on a contracted table whose adapter enforces it.
|
|
117
126
|
|
|
127
|
+
## Suggest from the models you're editing
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
$ sqlassert-dbt suggest models/customers.sql models/stg_orders.sql --trust-upstream-tests -- --target dev
|
|
131
|
+
customers:
|
|
132
|
+
models/customers.sql:7 mid CTE orders Unique Set /**ASSERT UNIQUE(customer_id)**/
|
|
133
|
+
models/customers.sql:11 mid Root Select Not Null /**ASSERT NOT NULL(customer_id)**/
|
|
134
|
+
|
|
135
|
+
stg_orders:
|
|
136
|
+
models/stg_orders.sql:2 mid Root Select Not Null /**ASSERT NOT NULL(order_id)**/
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Pass model files, a directory of models, or the project directory for every
|
|
140
|
+
model (`sqlassert-dbt suggest .`). It runs `dbt compile` on those models and
|
|
141
|
+
what they read (arguments after `--` go to dbt; a `--select` of your own
|
|
142
|
+
replaces that selection), then lists
|
|
143
|
+
what [`sqlassert suggest`](https://github.com/polvoazul/sqlassert#readme)
|
|
144
|
+
finds in each model, with the relations it reads declared as `check` declares
|
|
145
|
+
them by type: `rewrite`s that keep the result, `rewrite with caveat`s that
|
|
146
|
+
keep its rows but may change a column's type or precision (the message says which),
|
|
147
|
+
each with its unified diff against the model's compiled SQL (its hunks number
|
|
148
|
+
the compiled file's lines),
|
|
149
|
+
`marker`s that will be
|
|
150
|
+
`PROVED` when pasted at the end of their line, `hazard`s. `--explain`
|
|
151
|
+
prints each one's evidence. A marker on the model's own output that its
|
|
152
|
+
tests or contract already state is left out, or cut to the part none states
|
|
153
|
+
(a `unique` test leaves `NOT NULL` to suggest). A line inside SQL that dbt inlined or that a tag
|
|
154
|
+
wrote is given in the compiled file instead. In an incremental model, a
|
|
155
|
+
rewrite or marker is listed only when both `is_incremental()` branches prove
|
|
156
|
+
it. dbt's own output goes to stderr, and its `target/` and `logs/` go to a
|
|
157
|
+
temporary directory, leaving the project's alone (`--dbt-compile-inplace`
|
|
158
|
+
compiles there instead, keeping dbt's partial-parse cache; a `--target-path`
|
|
159
|
+
or `--log-path` after `--` is kept). Each model's suggestions print as soon as
|
|
160
|
+
it is analyzed.
|
|
161
|
+
|
|
118
162
|
## Markers in models
|
|
119
163
|
|
|
120
164
|
You can also write sqlassert markers straight into a model, and they are checked
|
|
@@ -134,6 +178,68 @@ the join fans out in production. See the
|
|
|
134
178
|
[sqlassert README](https://github.com/polvoazul/sqlassert#readme) for every
|
|
135
179
|
marker.
|
|
136
180
|
|
|
181
|
+
## Columns from `catalog.json`
|
|
182
|
+
|
|
183
|
+
The manifest lists no columns for sources, seeds, snapshots or uncontracted
|
|
184
|
+
incremental models, so `SELECT *` over them cannot be expanded, an unqualified
|
|
185
|
+
column may be ambiguous, and their types are unknown (a `dbt-unknown-columns`
|
|
186
|
+
warning). `dbt docs generate` writes `target/catalog.json` with every
|
|
187
|
+
relation's columns and types, as the warehouse had them:
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
$ dbt docs generate
|
|
191
|
+
$ sqlassert-dbt check . # reads target/catalog.json
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
It is read whenever it is there (`--catalog PATH` names another) and trusted like a connection: it can prove more, never less. Without one, a `dbt-unknown-columns` warning says how to get it.
|
|
195
|
+
It has no keys and no nullability, so those still come from tests and
|
|
196
|
+
contracts. A catalog is a snapshot, so an entry is not used where the project
|
|
197
|
+
shows it stale:
|
|
198
|
+
|
|
199
|
+
- **Another target** (`dbt-catalog-other-relation`): the catalog names the
|
|
200
|
+
relation in another database or schema than the manifest does.
|
|
201
|
+
- **A changed seed** (`dbt-catalog-stale`): the seed's CSV header lists other
|
|
202
|
+
columns.
|
|
203
|
+
- **An enforced contract** (`dbt-catalog-stale`): a contract that lists other
|
|
204
|
+
columns wins.
|
|
205
|
+
|
|
206
|
+
A source whose warehouse table changed after `docs generate` cannot be told
|
|
207
|
+
apart; regenerate the catalog when sources change, or connect.
|
|
208
|
+
|
|
209
|
+
## Columns, keys and NOT NULLs from the warehouse
|
|
210
|
+
|
|
211
|
+
`--connect` on `build`, `test` and `suggest` reads every table the models read
|
|
212
|
+
(sources, seeds, snapshots, tables) from the warehouse, current: columns,
|
|
213
|
+
types, nullability, primary and unique keys, foreign keys. It opens the
|
|
214
|
+
connection dbt opens, from the same profile and target (`-- --target prod`
|
|
215
|
+
picks another), and closes it before dbt runs again. No `catalog.json` is
|
|
216
|
+
read then. An enforced contract's table is still declared from the contract,
|
|
217
|
+
since the build may not have made it yet; every other table comes from the
|
|
218
|
+
warehouse.
|
|
219
|
+
As with `analyze_database`, a connection only adds proofs. A key the
|
|
220
|
+
warehouse does not enforce is trusted as a Property Declaration, as the SQL's
|
|
221
|
+
own are.
|
|
222
|
+
|
|
223
|
+
```bash
|
|
224
|
+
$ sqlassert-dbt build --connect .
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
Adapters: DuckDB (tested end to end), Postgres, Redshift, Snowflake, MySQL
|
|
228
|
+
and Trino. It uses dbt's adapter API, which is internal and changes across dbt
|
|
229
|
+
versions; when it fails, or for BigQuery and Databricks, pass a fetcher over
|
|
230
|
+
your own connection from Python:
|
|
231
|
+
|
|
232
|
+
```python
|
|
233
|
+
import duckdb
|
|
234
|
+
from sqlassert.discovery.duckdb import DuckDBKnowledgeFetcher
|
|
235
|
+
from sqlassert_dbt import analyze_project
|
|
236
|
+
|
|
237
|
+
report = analyze_project(project, fetcher=DuckDBKnowledgeFetcher(duckdb.connect("warehouse.duckdb")))
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
`suggest_models(..., fetcher=...)` takes one too. `check` stays
|
|
241
|
+
connectionless: it runs where dbt is not installed.
|
|
242
|
+
|
|
137
243
|
## Details
|
|
138
244
|
|
|
139
245
|
- **Manifest:** only the latest schema (v12, written by dbt 1.8 to 1.12).
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
# sqlassert-dbt
|
|
2
|
+
|
|
3
|
+
Prove your dbt tests from the SQL, and skip the ones that can't fail.
|
|
4
|
+
|
|
5
|
+
A `unique` or `not_null` test scans a table in the warehouse on every run, and
|
|
6
|
+
it only tells you about today's data. Many of those tests follow from the SQL
|
|
7
|
+
itself: `customer_id` is unique because the model groups by it, `status` is
|
|
8
|
+
never null because of a `coalesce`. [sqlassert](https://github.com/polvoazul/sqlassert)
|
|
9
|
+
proves them from the compiled model before anything runs, so dbt can leave them
|
|
10
|
+
out: faster CI, a smaller warehouse bill, and the same guarantee for *any*
|
|
11
|
+
data, not just the rows there today.
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install sqlassert-dbt
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
_Alpha: the markers and dbt test mappings are stable; the CLI output may
|
|
18
|
+
change._
|
|
19
|
+
|
|
20
|
+
## Skip the tests the SQL proves
|
|
21
|
+
|
|
22
|
+
Run dbt through the plugin:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
sqlassert-dbt build . -- --target prod # or: sqlassert-dbt test
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
It runs `dbt compile`, checks every model, then runs `dbt build` (or
|
|
29
|
+
`dbt test`) with each proved test passed as `--exclude`. Arguments after `--`
|
|
30
|
+
go to dbt unchanged. The exit code is dbt's when dbt fails, otherwise the
|
|
31
|
+
check's.
|
|
32
|
+
|
|
33
|
+
The tests of the sources, seeds and models a model reads are trusted as
|
|
34
|
+
facts, since `dbt build` runs them first in the same invocation. If the
|
|
35
|
+
arguments after `--` select nodes (`--select`, `--exclude`, `--selector`, ...),
|
|
36
|
+
an upstream test may not run, so none is trusted.
|
|
37
|
+
|
|
38
|
+
A `profiles.yml` kept in the project directory is found without
|
|
39
|
+
`--profiles-dir` (by `build`, `test` and `suggest`), unless the working
|
|
40
|
+
directory has one or `DBT_PROFILES_DIR` is set.
|
|
41
|
+
|
|
42
|
+
## Check without dbt
|
|
43
|
+
|
|
44
|
+
`check` reads only `target/manifest.json`, so dbt does not need to be installed
|
|
45
|
+
where it runs (a CI step after `dbt compile`, say):
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
$ dbt compile
|
|
49
|
+
$ sqlassert-dbt check . --trust-upstream-tests
|
|
50
|
+
models/customers.sql:12: UNKNOWN /**ASSERT NOT NULL(first_name)**/ (customers)
|
|
51
|
+
|
|
52
|
+
Proved statically (a test here can be skipped):
|
|
53
|
+
models/schema.yml unique_stg_customers_customer_id (test on stg_customers)
|
|
54
|
+
|
|
55
|
+
Not proved (the warehouse run still checks these):
|
|
56
|
+
models/schema.yml accepted_values_stg_orders_status__placed__shipped__returned (test on stg_orders): its Property is not proved from the SQL
|
|
57
|
+
|
|
58
|
+
Proved but untested (schema.yml additions):
|
|
59
|
+
models/schema.yml customers.customer_id: not_null
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Three lists come out:
|
|
63
|
+
|
|
64
|
+
- **Proved statically:** tests the SQL already guarantees. Skip them.
|
|
65
|
+
- **Not proved:** tests that still need the warehouse. Nothing is wrong; the
|
|
66
|
+
SQL alone can't show the property.
|
|
67
|
+
- **Proved but untested:** properties sqlassert proved that no test states.
|
|
68
|
+
Free `schema.yml` additions, which cost nothing to keep.
|
|
69
|
+
|
|
70
|
+
| Option | |
|
|
71
|
+
|---|---|
|
|
72
|
+
| `--trust-upstream-tests` | Treat passing tests of upstream nodes as facts (true under `dbt build`). Without it, a `dbt-untrusted-upstream-tests` warning lists them. |
|
|
73
|
+
| `--require-proved` | Fail when any test is not proved, not only when a marker is `UNKNOWN`. |
|
|
74
|
+
| `--explain` | Print each proof, and each failed proof. |
|
|
75
|
+
| `--dialect NAME` | Analyze under this dialect instead of the adapter's. |
|
|
76
|
+
| `--manifest PATH` | Read another manifest (default `PROJECT/target/manifest.json`). |
|
|
77
|
+
| `--catalog PATH` | Read column lists and types from this `catalog.json` instead of `PROJECT/target/catalog.json`, which is read when present (also on `build`, `test` and `suggest`). See below. |
|
|
78
|
+
| `--no-catalog` | Read no `catalog.json`: the manifest's columns only. |
|
|
79
|
+
| `--connect` | `build`, `test` and `suggest`: read the tables models read from the warehouse, through dbt's own connection. See below. |
|
|
80
|
+
| `--warning-limit N` | Print at most N warnings of each code, then say the rest are suppressed (default 10; 0 prints all). |
|
|
81
|
+
|
|
82
|
+
Exit codes: `0` every marker proved; `1` a marker is `UNKNOWN` or an error was
|
|
83
|
+
reported (unproved tests fail it only with `--require-proved`); `2` the
|
|
84
|
+
manifest cannot be read.
|
|
85
|
+
|
|
86
|
+
## What it understands
|
|
87
|
+
|
|
88
|
+
| dbt | sqlassert Property |
|
|
89
|
+
|---|---|
|
|
90
|
+
| `unique` | Unique Set |
|
|
91
|
+
| `not_null` | Not Null |
|
|
92
|
+
| `accepted_values` | Enum |
|
|
93
|
+
| `relationships` | Referential Coverage (Foreign Key) |
|
|
94
|
+
| `dbt_utils.unique_combination_of_columns` | Non-Null Unique Set (it counts NULL equal to NULL) |
|
|
95
|
+
| contract `primary_key`, `unique`, `not_null`, `foreign_key` | the same Properties |
|
|
96
|
+
|
|
97
|
+
A model's own tests are what gets proved. Upstream tests are evidence, used
|
|
98
|
+
only when they fail on any failing row: not a test with `severity: warn`, a
|
|
99
|
+
`where:` config, or `error_if`/`fail_calc` thresholds. A constraint counts only
|
|
100
|
+
on a contracted table whose adapter enforces it.
|
|
101
|
+
|
|
102
|
+
## Suggest from the models you're editing
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
$ sqlassert-dbt suggest models/customers.sql models/stg_orders.sql --trust-upstream-tests -- --target dev
|
|
106
|
+
customers:
|
|
107
|
+
models/customers.sql:7 mid CTE orders Unique Set /**ASSERT UNIQUE(customer_id)**/
|
|
108
|
+
models/customers.sql:11 mid Root Select Not Null /**ASSERT NOT NULL(customer_id)**/
|
|
109
|
+
|
|
110
|
+
stg_orders:
|
|
111
|
+
models/stg_orders.sql:2 mid Root Select Not Null /**ASSERT NOT NULL(order_id)**/
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Pass model files, a directory of models, or the project directory for every
|
|
115
|
+
model (`sqlassert-dbt suggest .`). It runs `dbt compile` on those models and
|
|
116
|
+
what they read (arguments after `--` go to dbt; a `--select` of your own
|
|
117
|
+
replaces that selection), then lists
|
|
118
|
+
what [`sqlassert suggest`](https://github.com/polvoazul/sqlassert#readme)
|
|
119
|
+
finds in each model, with the relations it reads declared as `check` declares
|
|
120
|
+
them by type: `rewrite`s that keep the result, `rewrite with caveat`s that
|
|
121
|
+
keep its rows but may change a column's type or precision (the message says which),
|
|
122
|
+
each with its unified diff against the model's compiled SQL (its hunks number
|
|
123
|
+
the compiled file's lines),
|
|
124
|
+
`marker`s that will be
|
|
125
|
+
`PROVED` when pasted at the end of their line, `hazard`s. `--explain`
|
|
126
|
+
prints each one's evidence. A marker on the model's own output that its
|
|
127
|
+
tests or contract already state is left out, or cut to the part none states
|
|
128
|
+
(a `unique` test leaves `NOT NULL` to suggest). A line inside SQL that dbt inlined or that a tag
|
|
129
|
+
wrote is given in the compiled file instead. In an incremental model, a
|
|
130
|
+
rewrite or marker is listed only when both `is_incremental()` branches prove
|
|
131
|
+
it. dbt's own output goes to stderr, and its `target/` and `logs/` go to a
|
|
132
|
+
temporary directory, leaving the project's alone (`--dbt-compile-inplace`
|
|
133
|
+
compiles there instead, keeping dbt's partial-parse cache; a `--target-path`
|
|
134
|
+
or `--log-path` after `--` is kept). Each model's suggestions print as soon as
|
|
135
|
+
it is analyzed.
|
|
136
|
+
|
|
137
|
+
## Markers in models
|
|
138
|
+
|
|
139
|
+
You can also write sqlassert markers straight into a model, and they are checked
|
|
140
|
+
and reported at their line in the model file:
|
|
141
|
+
|
|
142
|
+
```sql
|
|
143
|
+
-- models/customer_orders.sql
|
|
144
|
+
select c.customer_id, c.first_name, o.order_id
|
|
145
|
+
from {{ ref('stg_orders') }} o
|
|
146
|
+
/**ASSERT MANY TO ONE**/ join {{ ref('stg_customers') }} c
|
|
147
|
+
on o.customer_id = c.customer_id
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
If someone later loads duplicate customers upstream, or drops the `unique`
|
|
151
|
+
test that made this provable, the marker turns `UNKNOWN` and CI fails before
|
|
152
|
+
the join fans out in production. See the
|
|
153
|
+
[sqlassert README](https://github.com/polvoazul/sqlassert#readme) for every
|
|
154
|
+
marker.
|
|
155
|
+
|
|
156
|
+
## Columns from `catalog.json`
|
|
157
|
+
|
|
158
|
+
The manifest lists no columns for sources, seeds, snapshots or uncontracted
|
|
159
|
+
incremental models, so `SELECT *` over them cannot be expanded, an unqualified
|
|
160
|
+
column may be ambiguous, and their types are unknown (a `dbt-unknown-columns`
|
|
161
|
+
warning). `dbt docs generate` writes `target/catalog.json` with every
|
|
162
|
+
relation's columns and types, as the warehouse had them:
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
$ dbt docs generate
|
|
166
|
+
$ sqlassert-dbt check . # reads target/catalog.json
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
It is read whenever it is there (`--catalog PATH` names another) and trusted like a connection: it can prove more, never less. Without one, a `dbt-unknown-columns` warning says how to get it.
|
|
170
|
+
It has no keys and no nullability, so those still come from tests and
|
|
171
|
+
contracts. A catalog is a snapshot, so an entry is not used where the project
|
|
172
|
+
shows it stale:
|
|
173
|
+
|
|
174
|
+
- **Another target** (`dbt-catalog-other-relation`): the catalog names the
|
|
175
|
+
relation in another database or schema than the manifest does.
|
|
176
|
+
- **A changed seed** (`dbt-catalog-stale`): the seed's CSV header lists other
|
|
177
|
+
columns.
|
|
178
|
+
- **An enforced contract** (`dbt-catalog-stale`): a contract that lists other
|
|
179
|
+
columns wins.
|
|
180
|
+
|
|
181
|
+
A source whose warehouse table changed after `docs generate` cannot be told
|
|
182
|
+
apart; regenerate the catalog when sources change, or connect.
|
|
183
|
+
|
|
184
|
+
## Columns, keys and NOT NULLs from the warehouse
|
|
185
|
+
|
|
186
|
+
`--connect` on `build`, `test` and `suggest` reads every table the models read
|
|
187
|
+
(sources, seeds, snapshots, tables) from the warehouse, current: columns,
|
|
188
|
+
types, nullability, primary and unique keys, foreign keys. It opens the
|
|
189
|
+
connection dbt opens, from the same profile and target (`-- --target prod`
|
|
190
|
+
picks another), and closes it before dbt runs again. No `catalog.json` is
|
|
191
|
+
read then. An enforced contract's table is still declared from the contract,
|
|
192
|
+
since the build may not have made it yet; every other table comes from the
|
|
193
|
+
warehouse.
|
|
194
|
+
As with `analyze_database`, a connection only adds proofs. A key the
|
|
195
|
+
warehouse does not enforce is trusted as a Property Declaration, as the SQL's
|
|
196
|
+
own are.
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
$ sqlassert-dbt build --connect .
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Adapters: DuckDB (tested end to end), Postgres, Redshift, Snowflake, MySQL
|
|
203
|
+
and Trino. It uses dbt's adapter API, which is internal and changes across dbt
|
|
204
|
+
versions; when it fails, or for BigQuery and Databricks, pass a fetcher over
|
|
205
|
+
your own connection from Python:
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
import duckdb
|
|
209
|
+
from sqlassert.discovery.duckdb import DuckDBKnowledgeFetcher
|
|
210
|
+
from sqlassert_dbt import analyze_project
|
|
211
|
+
|
|
212
|
+
report = analyze_project(project, fetcher=DuckDBKnowledgeFetcher(duckdb.connect("warehouse.duckdb")))
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
`suggest_models(..., fetcher=...)` takes one too. `check` stays
|
|
216
|
+
connectionless: it runs where dbt is not installed.
|
|
217
|
+
|
|
218
|
+
## Details
|
|
219
|
+
|
|
220
|
+
- **Manifest:** only the latest schema (v12, written by dbt 1.8 to 1.12).
|
|
221
|
+
- **Ephemeral models** are analyzed where dbt inlined them.
|
|
222
|
+
- **Incremental models** are analyzed in both `is_incremental()` branches. A
|
|
223
|
+
test holds only if it holds in both, and a key only under a `merge` or
|
|
224
|
+
`delete+insert` strategy on that `unique_key` with no
|
|
225
|
+
`incremental_predicates`. The other branch is rendered without dbt only when
|
|
226
|
+
the model uses `ref`, `source`, `this`, `config` and `is_incremental()`
|
|
227
|
+
alone; otherwise it is reported unanalyzed.
|
|
228
|
+
- **Adapters:** analyzed when sqlassert models their dialect (DuckDB,
|
|
229
|
+
PostgreSQL, MySQL, Snowflake, BigQuery, Databricks / Spark, Redshift, Trino).
|
|
230
|
+
- A model with markers but no compiled code is an error, not skipped.
|
|
231
|
+
|
|
232
|
+
## License
|
|
233
|
+
|
|
234
|
+
MIT
|
|
@@ -21,7 +21,7 @@ classifiers = [
|
|
|
21
21
|
# Reads target/manifest.json only: dbt itself is needed to compile the
|
|
22
22
|
# project, not to check it, so it is a test dependency here.
|
|
23
23
|
dependencies = [
|
|
24
|
-
"sqlassert",
|
|
24
|
+
"sqlassert>=0.3.1",
|
|
25
25
|
"sqlglot>=30.30,<31",
|
|
26
26
|
]
|
|
27
27
|
|
|
@@ -36,6 +36,7 @@ sqlassert-dbt = "sqlassert_dbt.__main__:main"
|
|
|
36
36
|
test = [
|
|
37
37
|
"dbt-core>=1.12",
|
|
38
38
|
"dbt-duckdb>=1.11",
|
|
39
|
+
"duckdb",
|
|
39
40
|
"pytest",
|
|
40
41
|
]
|
|
41
42
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""dbt integration: check a dbt project's tests and Property Markers from its manifest, without dbt.
|
|
2
|
+
|
|
3
|
+
`analyze_project(project_dir)` reads `target/manifest.json` and returns a
|
|
4
|
+
`DbtReport`: which tests the SQL proves (skippable), which it does not, which
|
|
5
|
+
proved Properties no test states, and the outcome of every marker written in
|
|
6
|
+
a model file, mapped back to that file's lines. `suggest_models(project_dir,
|
|
7
|
+
files)` lists what `sqlassert suggest` finds in those model files, with what
|
|
8
|
+
each model reads declared. `sqlassert-dbt` is its command line (`__main__.py`).
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from sqlassert_dbt.project import SUPPORTED_DIALECTS, ProjectError, analyze_project, suggest_models
|
|
12
|
+
from sqlassert_dbt.report import (
|
|
13
|
+
DbtReport, Location, MarkerOutcome, ModelSuggestion, Problem, Status, SuggestReport, TestOutcome, Untested,
|
|
14
|
+
exit_code, format_suggestion_blocks, format_suggestion_summary, format_suggestions, format_text,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"SUPPORTED_DIALECTS", "DbtReport", "Location", "MarkerOutcome", "ModelSuggestion", "Problem",
|
|
19
|
+
"ProjectError", "Status", "SuggestReport", "TestOutcome", "Untested", "analyze_project", "exit_code",
|
|
20
|
+
"format_suggestion_blocks", "format_suggestion_summary", "format_suggestions", "format_text", "suggest_models",
|
|
21
|
+
]
|