From 08211b77860449b009bb7879fc3d11c157d5bed8 Mon Sep 17 00:00:00 2001 From: Karan Nagaraj Date: Fri, 19 Jul 2024 11:10:33 +0100 Subject: [PATCH] [CT-1661] Quotes usernames with special characters --- CONTRIBUTING.md | 6 +++--- .../macros/adapters/apply_grants.sql | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1a6e92a29..4d5b313ad 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -105,13 +105,13 @@ Unit tests can be run locally without setting up a database connection: # Note: replace $strings with valid names # run all unit tests -hatch run unit-test +hatch run unit-tests # run all unit tests in a module -hatch run unit-test tests/unit/$test_file_name.py +hatch run unit-tests tests/unit/$test_file_name.py # run a specific unit test -hatch run unit-test tests/unit/$test_file_name.py::$test_class_name::$test_method_name +hatch run unit-tests tests/unit/$test_file_name.py::$test_class_name::$test_method_name ``` ### Testing against a development branch diff --git a/dbt/include/global_project/macros/adapters/apply_grants.sql b/dbt/include/global_project/macros/adapters/apply_grants.sql index c75eef89d..b86cd09a6 100644 --- a/dbt/include/global_project/macros/adapters/apply_grants.sql +++ b/dbt/include/global_project/macros/adapters/apply_grants.sql @@ -64,9 +64,21 @@ show grants on {{ relation.render() }} {% endmacro %} +{% macro quote_grantees(grantees) %} + {%- set quoted_grantees = [] -%} + {%- for grantee in grantees -%} + {%- if (('@' in grantee) or ('-' in grantee) or ('.' in grantee)) -%} + {%- do quoted_grantees.append(adapter.quote(grantee)) -%} + {%- else -%} + {%- do quoted_grantees.append(grantee) -%} + {%- endif -%} + {%- endfor -%} + {%- do return quoted_grantees -%} +{% endmacro %} {% macro get_grant_sql(relation, privilege, grantees) %} - {{ return(adapter.dispatch('get_grant_sql', 'dbt')(relation, privilege, grantees)) }} + {%- set updated_grantees = quote_grantees(grantees) -%} + {{ return(adapter.dispatch('get_grant_sql', 'dbt')(relation, privilege, updated_grantees)) }} {% endmacro %} {%- macro default__get_grant_sql(relation, privilege, grantees) -%} @@ -75,7 +87,8 @@ {% macro get_revoke_sql(relation, privilege, grantees) %} - {{ return(adapter.dispatch('get_revoke_sql', 'dbt')(relation, privilege, grantees)) }} + {%- set updated_grantees = quote_grantees(grantees) -%} + {{ return(adapter.dispatch('get_revoke_sql', 'dbt')(relation, privilege, updated_grantees)) }} {% endmacro %} {%- macro default__get_revoke_sql(relation, privilege, grantees) -%}