entechlog / dbt-snow-mask

This repository contains source code for dbt package dbt_snow_mask.
https://hub.getdbt.com/entechlog/dbt_snow_mask/latest/
GNU General Public License v3.0
60 stars 25 forks source link

Improvement: include sourcename or modelname in posthook to minimise overhead on run model tasks #38

Open Chobsz opened 2 years ago

Chobsz commented 2 years ago

41

I've edited the code to allow for the following post hooks in the project file to process only the relevant maskingpolicies on a run task.

                +post-hook: "{{ dbt_snow_mask.apply_masking_policy('models','mymodelname')}}" 
                +post-hook: "{{ dbt_snow_mask.apply_masking_policy('sources','mysourcename')}}" 

-- Altered macro: [apply_masking_policy] (50% the old macro)

{% macro apply_masking_policy(resource_type="models",resource_name="undefined",meta_key="masking_policy") %}

    {% if execute %}

        {% if resource_type == "sources" and  resource_name == "undefined" %}
            {{ dbt_snow_mask.apply_masking_policy_list_for_sources(meta_key) }}
        {% elif resource_type == "models" and resource_name == "undefined" %}
            {{ dbt_snow_mask.apply_masking_policy_list_for_models(meta_key) }}           
        {% elif resource_type == "sources" and resource_name != "undefined" %}
            {{ dbt_snow_mask.apply_masking_policy_list_for_onesource(meta_key,resource_name) }}
        {% elif resource_type == "models" and resource_name != "undefined" %}
            {{ dbt_snow_mask.apply_masking_policy_list_for_onemodel(meta_key,resource_name) }}
        {% endif %}
    {% endif %}

{% endmacro %}

-- New macro: [apply_masking_policy_list_for_onesource] (95% the old macro apply_masking_policy_list_for_sources) --Most important change:

% macro apply_masking_policy_list_for_onesource(meta_key,resource_name,operation_type="apply") %}

    {% for node in graph.sources.values() -%}
        {%- if node.source_name == resource_name -%}
 [...]

-- New macro: [apply_masking_policy_list_for_onemodel] (95% the old macro apply_masking_policy_list_for_models) --Most important change:


{% macro apply_masking_policy_list_for_onemodel(meta_key,resource_name,operation_type="apply") %}
{%set resource_name =  '/'+ resource_name +'/' %}
    {% if operation_type == "apply" %}    
        {% for node in graph.nodes.values() %}
            {%- if resource_name in node.path  -%}          
 [...]

If you need the whole code, I created a PullRequest

Chobsz commented 2 years ago

created PR 41, let me know if you need anything.

Chobsz commented 1 year ago

created PR #58, hope it meets standards. however the whole thing could have been done wth DRY, but my Jinja is just nog up to par.