Search code examples
yamldbt

dbt source.yml based on target name


I have 2 env, 'dev' and 'prod'. In some case, the source will be different between dev and prod. How do we automatically change the source.yml when we run the dbt in prod?

source.yml in dev

version: 2
sources:
  - name: idn_stg_mti
    description: 'Data ingested from MTI channel'
    database: DEV_IDN
    schema: idn_stg_mti

source.yml in prod

version: 2
sources:
  - name: idn_stg_mti
    description: 'Data ingested from MTI channel'
    database: PRD_IDN
    schema: idn_stg_mti

Solution

  • You can use a little bit of jinja to achieve this. Your exact use case is described in the dbt docs

    sources:
      - name: idn_stg_mti 
        database: |
          {%- if  target.name == "dev" -%} DEV_IDN 
          {%- elif target.name == "prod"  -%} PRD_IDN
          {%- else -%} invalid_database
          {%- endif -%}
        schema: idn_stg_mti