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: Allow for default dbt schema name generation #53

Closed mjahammel closed 1 year ago

mjahammel commented 1 year ago

Currently, specifying a value for the 'common_masking_policy_schema' variable results in dbt using that value, as-is, as the target schema for the masking policies. This is different from the default dbt behaviour of prefixing the 'schema' variable from dbt_project.yml file with the value of 'schema' variable in the profiles.yml file.

I propose adding a new configuration variable, 'common_masking_policy_dbt_schema_override'. The value would be True/False. The default value would be 'True', which would cause the code to behave as it does currently (preserving backwards compatibility). If the value specified is 'False', then the dbt macro 'generate_schema_name' would be called to calculate the schema name.

I have cloned the repository and tried out changes to support this. For example, the changes to 'create_masking_policy.sql' are:

diff --git a/macros/snow-mask/create-policy/create_masking_policy.sql b/macros/snow-mask/create-policy/create_masking_policy.sql
index 4570393..795af4e 100644
--- a/macros/snow-mask/create-policy/create_masking_policy.sql
+++ b/macros/snow-mask/create-policy/create_masking_policy.sql
@@ -19,14 +19,24 @@
         {%- if (var('use_common_masking_policy_db', 'False')|upper in ['TRUE','YES']) -%}
             {% if (var('common_masking_policy_db') and var('common_masking_policy_schema')) %}
                 {% set masking_policy_db = var('common_masking_policy_db') | string  %}
-                {% set masking_policy_schema = var('common_masking_policy_schema') | string  %}
+
+                {%- if (var('common_masking_policy_dbt_schema_override', 'True')|upper in ['TRUE', 'YES']) -%}
+                    {% set masking_policy_schema = var('common_masking_policy_schema') | string  %}
+                {% else %}
+                    {% set masking_policy_schema = generate_schema_name(var('common_masking_policy_schema') | string, node) %}
+                {% endif %}
             {% endif %}
         {% endif %}

         {# Override the schema name (in the masking_policy_db) when use_common_masking_policy_schema_only flag is set #}
         {%- if (var('use_common_masking_policy_schema_only', 'False')|upper in ['TRUE','YES']) and (var('use_common_masking_policy_db', 'False')|upper in ['FALSE','NO']) -%}
             {% if var('common_masking_policy_schema') %}
-                {% set masking_policy_schema = var('common_masking_policy_schema') | string  %}
+
+                {%- if (var('common_masking_policy_dbt_schema_override', 'True')|upper in ['TRUE', 'YES']) -%}
+                    {% set masking_policy_schema = var('common_masking_policy_schema') | string  %}
+                {% else %}
+                    {% set masking_policy_schema = generate_schema_name(var('common_masking_policy_schema') | string, node) %}
+                {% endif %}
             {% endif %}
         {% endif %}

Thoughts?

I have a branch ready to be committed, if this sounds like something that would be acceptable.

Thanks,

mgeerken commented 1 year ago

Haven't test it, but got the same issue and would appreciate a change.

mjahammel commented 1 year ago

I'm still new to dbt, but as I've learned more and more about dbt works, some light bulbs have come on. For example, this request can be accomplished without a modification to the package. Use the following in dbt_project.yml:

common_masking_policy_schema: '{{ generate_schema_name("GOVERNANCE") }}'