Is there a way to `dbt run --select` and build onl...
# community-support
d
Is there a way to
dbt run --select
and build only those models which have tests, e.g. primary key tests, without explicitly adding YAML tags to the relevant models and filtering by explicit tag selection? Asked on the dbt Slack community but got no suggested solutions.
j
Not sure if it will work, but you can try with this selectors shown here with
run
instead of `test`https://docs.getdbt.com/reference/commands/test
d
Unfortunately that doesn't work. The
dbt test
syntax does something useful but
dbt run --select test_type:data
does nothing.
j
Mmm then I wouldn’t be so sure. You could make a Python script with the
dbt_artifacts_parser
library and go through all tests, and check their
depends_on
field to pick up the model that they depend on and save those models on a list. Afterwards, make a string of all the models and then execute dbt based on that, but I’d say that’s not a pretty solution at all
d
I suppose another method is to parse the output of both
dbt ls --resource-type model
and
dbt ls --resource-type test
and figure out (within the latter) where the name of a model ends and the name of the column starts.
j
Yeah, that’s true. You’ll have to find a pattern to match them, though, although usually tests include the name of the model within their id, so a simple regex should suffice. It can be a bit tricky if you have models that have the same name, but partially. Like a model called
users
and another
users_groups
and if
users_groups
has a test, but
users
doesn’t
d
At least for us, it's possible to run a locally-modified variant of the following SQL to generate a list of models which have primary key tests. Whatever database your artifacts are written to, replace "artifacts_database" with that name. There is admittedly a bit of a chicken-and-egg here; you have to run
dbt run --select elementary
in order to populate the artifact tables, optionally run dbt proper (if you need to build/refresh any models), then run
dbt test
and other parts of your Elementary workflow. (this SQL is Snowflake syntax btw)
j
I’m glad you found a solution! Just as a side note, since you’re using the alias as the final selection, if the model has a different filename than its alias, it might not run