Skip to content

Instantly share code, notes, and snippets.

View t0mpere's full-sized avatar
🦸‍♂️
Focusing

Tommaso Peresson t0mpere

🦸‍♂️
Focusing
  • Bumble
  • London
View GitHub Profile
@t0mpere
t0mpere / anonymize.sql
Last active June 6, 2024 19:48
anonymization dbt macro
{% macro anonymize(column_name, function, add_alias=True) %}
{% if execute %}
{% call statement('salt', fetch_result=True) %}
select salt from sources.private.salt limit 1;
{% endcall %}
{% set salt = load_result('salt')['data'][0][0] %}
{% if function == 'full_hash' %}
SHA256(concat({{ column_name }},'{{ salt }}'))
{% endif %}
@t0mpere
t0mpere / anonymize_columns.sql
Last active May 20, 2021 15:14
dbt macro for table anonymization
{% 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;
@t0mpere
t0mpere / model.sql
Created May 19, 2021 10:08
anonymization usage
{{ config(database='sources') }}
{{ config(schema='production') }}
-- the following comment is needed by dbt to compile the references in the macro
-- depends_on: {{ ref('anonymization_mapping') }}
select {{ anonymize_columns('private_sources', 'production', 'users') }}
from private_sources.production.users