Specify Target in Dbt Model Based on the Folder Location
Dbt Newbie Here! I Am Building a Multiaccount Dbt Project and I Don't Know How I Can Make the Target Depend on the Folder Where Each Model Is Located. This Is...
DBT newbie here!
I am building a multiaccount dbt project and I don't know how I can make the target depend on the folder where each model is located.
This is my desired folder configuration:
└───models
├───account1
│ ├───DB1
│ │ └───SCHEMA1
│ ├───DB2
│ │ ├───SCHEMA2
│ │ └───SCHEMA3
│ └───DB3
│ └───SCHEMA4
└───account2
├───DB4
│ └───SCHEMA5
└───DB5
├───SCHEMA6
└───SCHEMA7
I have configured in profiles.yml a dev profile with two targets: account1 and account2. How can I get DBT to run models in account1 folder using account1 as target and models in account2 folder using account2 target?
I have accomplished it by running:
dbt run --models account1 --target account1
dbt run --models account2 --target account2
But is there a better way to do this? I would like to avoid having to run two consequent runs.
I've also seen it is recommended to split the repo into two, but I would really like to keep the mono-repo approach.
1 Answer
You can specify the models you want to run as a path (). The --target option is used to tell dbt which profile you want to use when running a job and has nothing to do with the models you want executed.
dbt run -s path:models/account1
dbt run -s path:models/account2
You can also configure this through a selector. Create a file called selectors.yml in your project root directory
selectors:
- name: foo
definition:
union:
- method: path
value: "models/account1"
- method: path
value: "models/account2"
Then run it with
dbt run --selector foo
Note that this approach combines both into a single run. You can split it into multiple tasks if you want them to be completely independent.