Last active
May 20, 2021 15:14
-
-
Save t0mpere/0e2aafa57aac14b4c148d331d6f0a2f3 to your computer and use it in GitHub Desktop.
dbt macro for table anonymization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% macro anonymize_columns(database, schema, table) %} | |
{% call statement('mapping', fetch_result=True) %} | |
select m.table_name_filter, s.column_name, function | |
from {{ ref('anonymization_mapping') }} as m | |
right join {{ database }}.information_schema.columns as s | |
on UPPER(m.column_name) = s.column_name | |
where table_name = '{{ table.upper() }}' | |
and table_schema = '{{ schema.upper() }}' | |
order by s.column_name; | |
{% endcall %} | |
{% set mapping = load_result('mapping')['data'] %} | |
-- map[0]: table_name, if present only the right table+column combination gets anonymized, otherwise | |
-- all column names that match with column_name are anonymized. | |
-- map[1]: column_name | |
-- map[2]: function_name, function to anonymise the field | |
{% for map in mapping %} | |
{% if map[2] is not none and (map[0] == table.lower() or map[0] is none) %} | |
{{ anonymize(map[1],map[2]) }} | |
{% else %} | |
"{{ map[1] }}" | |
{% endif %} | |
{% if not loop.last %} | |
, | |
{% endif %} | |
{% endfor %} | |
{% endmacro %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment