Search code examples
continuous-integrationdbt

Is there a way to override the schema names for dbt Cloud CI runs?


We use dbt cloud to run our dbt project. On CI runs, dbt Cloud uses a schema name related to the PR number, e.g. dbt_cloud_pr_5205_543.

Is there a way to override this behavior?

Update: we've updated our macro as below.

generate_schema_name.sql 
% macro generate_schema_name(custom_schema_name, node) -%}      
    {%- set default_schema = target.schema -%}      
    {%- if target.name[-3:] == 'dev' -%}          
        {{ target.schema }}_{{ custom_schema_name | trim }}      
    {%- elif target.schema[:9] == 'dbt_cloud' -%}          
        {{ target.schema }}_{{ custom_schema_name | trim }}      
    {%- elif custom_schema_name is none -%}          
        {{ default_schema }}      
    {%- else -%}          
        {{ custom_schema_name | trim }}      
    {%- endif -%}  
{%- endmacro %}

Solution

  • This macro resolved the problem:

    generate_schema_name.sql 
    {% macro generate_schema_name(custom_schema_name, node) -%}
    
        {%- set default_schema = target.schema -%}
    
        {%- if target.name[-3:] == 'dev' -%}
    
            {{ target.schema }}_{{ custom_schema_name | trim }}
    
        {%- elif target.schema[:9] == 'dbt_cloud' -%}
    
            {{ target.schema }}_{{ custom_schema_name | trim }}
    
        {%- elif custom_schema_name is none -%}
    
            {{ default_schema }}
    
        {%- else -%}
    
            {{ custom_schema_name | trim }}
    
        {%- endif -%}
    
    {%- endmacro %}