diff --git a/.changes/unreleased/Fixes-20240719-155743.yaml b/.changes/unreleased/Fixes-20240719-155743.yaml new file mode 100644 index 00000000..7b9d1c30 --- /dev/null +++ b/.changes/unreleased/Fixes-20240719-155743.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Quotes database users that contain special characters +time: 2024-07-19T15:57:43.697763+01:00 +custom: + Author: qmg-karan ChocoletMousse + Issue: "156" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1a6e92a2..4d5b313a 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 c75eef89..b86cd09a 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) -%}