Skip to content

Commit

Permalink
Merge pull request #67 from Snowflake-Labs/fix-snowflake-permission-l…
Browse files Browse the repository at this point in the history
…ookup

Full credits for this release goes to @sfc-gh-tbraunschober who identified an issue and provided the fix for where the Python and SQL would produce an error for users not yet using database roles.
  • Loading branch information
sfc-gh-dflippo authored Mar 21, 2024
2 parents 39bab00 + d20d2d5 commit 3669830
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

name: 'dbt_constraints'
version: '0.6.2'
version: '0.6.3'
config-version: 2

# These macros depend on the results and graph objects in dbt >=0.19.0
Expand Down
3 changes: 2 additions & 1 deletion macros/create_constraints.sql
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@
and ( res.failures == 0 or
res.node.config.get("always_create_constraint", false) )
and ( res.node.config.where is none or
res.node.config.get("always_create_constraint", false) ) -%}
res.node.config.get("always_create_constraint", false) )
and res.node.config.get("dbt_constraints_enabled", true) -%}

{%- set test_model = res.node -%}
{%- set test_parameters = test_model.test_metadata.kwargs -%}
Expand Down
17 changes: 2 additions & 15 deletions macros/snowflake__create_constraints.sql
Original file line number Diff line number Diff line change
Expand Up @@ -272,23 +272,10 @@ SHOW IMPORTED KEYS IN TABLE {{ table_relation }}
upper(tp.table_name) as "table_name",
tp.privilege_type as "privilege_type"
from {{ table_relation.database }}.information_schema.table_privileges tp
where is_role_in_session(tp.grantee)
where (is_role_in_session(tp.grantee) or is_database_role_in_session(tp.grantee))
and tp.privilege_type in ('OWNERSHIP', 'REFERENCES')
{%- endset -%}
{%- set role_privilege_list = run_query(lookup_query) -%}

{%- set lookup_query -%}
select distinct
upper(tp.table_schema) as "table_schema",
upper(tp.table_name) as "table_name",
tp.privilege_type as "privilege_type"
from {{ table_relation.database }}.information_schema.table_privileges tp
where is_database_role_in_session(tp.grantee)
and tp.privilege_type in ('OWNERSHIP', 'REFERENCES')
{%- endset -%}
{%- set db_role_privilege_list = run_query(lookup_query) -%}

{%- set privilege_list = role_privilege_list.merge([role_privilege_list, db_role_privilege_list]).distinct() -%}
{%- set privilege_list = run_query(lookup_query) -%}
{%- do lookup_cache.table_privileges.update({ table_relation.database: privilege_list }) -%}
{%- endif -%}

Expand Down

0 comments on commit 3669830

Please sign in to comment.