The problem I’m having
Hi, I am having a question regarding the 'this ’ variable value actually when i passed the this variable to macro using post hook it is given different schema name which is mentioned in profiles.yml file but not to the mentioned in the dbt_project.yml and if i used the {{this}} in the from clause it is giving the correct schema as per the dbt_project.yml file.
The context of why I’m trying to do this
I have created a macro for adding masking policy as post hook which will add the policy after the model is created as the model is materialized as table but when i am passing the this value in post hook macro and when i execute the macro it is taking wrong schema because of which the macro is failing so i want to know how i can point the ‘this’ variable value to schema mentioned in the dbt_project.yml file not to the profiles.yml so that it won’t cause issue.
What I’ve already tried
i have tried the below code but if i hard code and pass the schema name as four passing value it will work but with ‘this’ variable passing value won’t work. and i have tried passing this.schema, this.table also but the schema is giving wrong value
Some example code or error messages
{{
config(
materialized='table',
post_hook=[
"{{ apply_masking_policy(this, ['name', 'id'], 'masking_policy') }}"
]
)
}}
macro:
{% macro apply_masking_policy(model, columns, policy_name) %}
{% set schema_name = model.schema %}
{% set table_name = model.name %}
-- Log the model details (schema and table name) for visibility
{{ log("Model execution details -> Schema: " ~ schema_name ~ ", Table: " ~ table_name, info=True) }}
{% set model_name = schema_name ~ "." ~ table_name %}
{% set statements = [] %}
{% for column in columns %}
{% set stmt = "ALTER TABLE " ~ model_name ~ " MODIFY COLUMN " ~ column ~ " SET MASKING POLICY " ~ policy_name %}
{% do statements.append(stmt) %}
{% endfor %}
{% set final_statements = statements|join('; ') %}
{% do log(final_statements, info=True) %}
{{ return(final_statements) }}
{% endmacro %}
output:
Model execution details -> Schema: BASE_SCHEMA, Table: table_test
ALTER TABLE BASE_SCHEMA.table_test MODIFY COLUMN name SET MASKING POLICY test_schema.MASK_TESTING;
correct output schema should be : Schema: DP_TEST which is being in the dbt_project.yml
and if use 'this' variable in the select query it is pointing to correct schema:
select
*
from {{ this }}
limit 100
output:
select
*
from test.dp_test.table_test
limit 100
Can anyone please help me on this what could be the reason for wrong schema assigned to ‘THIS’ Variable and is this the issue from dbt end itself?