I'm trying to change the generate_alias_name macro to change the table name of custom generic test results.
For some reason, when I run the test locally, the generate_alias_name is correctly applied and the test result table is created with the correct name as specified down below, but when running with production target, the naming falls back to default naming of table and test name plus a hash.
Where else could the aliasing be defined for prod?
I'm working with dbt Core and BigQuery.
Thanks in advance!
{% macro generate_alias_name(custom_alias_name=none, node=none) -%}
{% set re = modules.re %}
{% set webshop_pattern = '_(shop1|shop2)?$' %}
{% set node_name = node.name %}
-- re.sub returns the unchanged string if no match is found for pattern.
-- Thus, the following will strip the webshop suffix from node names if found.
-- We want to leave it unchanged if target is local, or resource type is seed or test
-- because everything is materialized in the same dataset in these cases.
{%- if target.name != 'local' and node.resource_type not in ('seed', 'test') -%}
{%- set node_name = re.sub(webshop_pattern, '', node_name) -%}
{%- endif -%}
-- If it's a generic test, then add the webshop suffix that's found in the model name to the test result table
-- node.test_metadata is defined -> generic test
{%- if node.resource_type == 'test' and node.test_metadata is defined -%}
{% set model_tested = re.search("ref\('([^']+)'\)", node.test_metadata.kwargs['model']).group(1) %}
{% set search_result = re.search(webshop_pattern, model_tested) %}
{% set webshop_suffix = '' if search_result is none else search_result[0] %}
{% set node_name = node.test_metadata.name ~ webshop_suffix %}
{%- endif -%}
{%- if custom_alias_name -%}
{%- if target.name == 'local' -%}
{{ node_name }}
{%- else -%}
{{ custom_alias_name | trim }}
{%- endif -%}
{%- elif node.version -%}
{{ return(node_name ~ '_v' ~ (node.version | replace('.', '_'))) }}
{%- else -%}
{{ node_name }}
{%- endif -%}
{%- endmacro %}
tldr: I changed the generate_alias_name dbt macro to change the table name of results of generic test. When running the test locally, it applied correctly, when running in prod, it didn't work and went back to default naming.