Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ct 716 add grants to materializations #178

Merged
merged 28 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
473712f
minor update
McKnight-42 Jun 27, 2022
f847ff6
6/27 eod update
McKnight-42 Jun 27, 2022
947dcde
Merge branch 'main' of gist.github.com:dbt-labs/dbt-snowflake into ct…
McKnight-42 Jun 28, 2022
ebaff10
adding standardize_grants_dict for snowlfake
McKnight-42 Jun 29, 2022
31fdaeb
models are building but never seeing revoke statements
McKnight-42 Jul 5, 2022
aaccebf
updating snowflake implementation of apply_grants after pairing with …
McKnight-42 Jul 6, 2022
7878a4b
Merge branch 'main' of gist.github.com:dbt-labs/dbt-snowflake into ct…
McKnight-42 Jul 6, 2022
33d6c37
post morning feedback updates
McKnight-42 Jul 7, 2022
09c5238
remove _get_role and revert to filter based on OWNERSHIP
McKnight-42 Jul 7, 2022
d613a38
ping original core dev-requirements pointer and note for post mergers…
McKnight-42 Jul 7, 2022
581e1d3
typo in dev-requirements.txt
McKnight-42 Jul 7, 2022
8bb27fc
pre-commit fixes
McKnight-42 Jul 7, 2022
3e3b2f8
add test to functional tests
McKnight-42 Jul 7, 2022
e0b5bbc
adding in test envs and passing locally, augmented new envs in github…
McKnight-42 Jul 7, 2022
3a7c473
add end line in file
McKnight-42 Jul 7, 2022
84c06e8
add envs to integration.yml
McKnight-42 Jul 7, 2022
4157117
fix typing issue
McKnight-42 Jul 7, 2022
d91e68d
make changes in env
McKnight-42 Jul 8, 2022
e3f5bcc
nit file formatting
McKnight-42 Jul 8, 2022
eabfef2
push up addition of more tests pr
McKnight-42 Jul 8, 2022
1247bba
incorporating message changes for snowflake specific error
McKnight-42 Jul 8, 2022
c63fe08
Account for refactor in dbt-labs/dbt-core@c763601
jtcohen6 Jul 11, 2022
ffecf75
Run test cases with copy_grants True, too
jtcohen6 Jul 11, 2022
5589fcc
test against ct-660 branch
McKnight-42 Jul 11, 2022
28e8459
Merge branch 'ct-716-add-materialization' of gist.github.com:dbt-labs…
McKnight-42 Jul 11, 2022
cef389c
test against ct-660 branch
McKnight-42 Jul 11, 2022
2fc0828
change dev-requirments to point to main dbt-core and test
McKnight-42 Jul 11, 2022
abaea85
add changelog
McKnight-42 Jul 11, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ jobs:
SNOWFLAKE_TEST_DATABASE: ${{ secrets.SNOWFLAKE_TEST_DATABASE }}
SNOWFLAKE_TEST_QUOTED_DATABASE: ${{ secrets.SNOWFLAKE_TEST_QUOTED_DATABASE }}
SNOWFLAKE_TEST_ROLE: ${{ secrets.SNOWFLAKE_TEST_ROLE }}
DBT_TEST_USER_1: ${{ secrets.DBT_TEST_USER_1 }}
DBT_TEST_USER_2: ${{ secrets.DBT_TEST_USER_2 }}
DBT_TEST_USER_3: ${{ secrets.DBT_TEST_USER_3 }}
run: tox

- uses: actions/upload-artifact@v2
Expand Down
17 changes: 16 additions & 1 deletion dbt/adapters/snowflake/impl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Mapping, Any, Optional, List, Union
from typing import Mapping, Any, Optional, List, Union, Dict

import agate

Expand All @@ -9,6 +9,7 @@
LIST_SCHEMAS_MACRO_NAME,
LIST_RELATIONS_MACRO_NAME,
)
from dbt.adapters.base.meta import available
from dbt.adapters.snowflake import SnowflakeConnectionManager
from dbt.adapters.snowflake import SnowflakeRelation
from dbt.adapters.snowflake import SnowflakeColumn
Expand Down Expand Up @@ -158,5 +159,19 @@ def quote_seed_column(self, column: str, quote_config: Optional[bool]) -> str:
else:
return column

@available
def standardize_grants_dict(self, grants_table: agate.Table) -> dict:
grants_dict: Dict[str, Any] = {}

for row in grants_table:
grantee = row["grantee_name"]
privilege = row["privilege"]
if privilege != "OWNERSHIP":
if privilege in grants_dict.keys():
grants_dict[privilege].append(grantee)
else:
grants_dict.update({privilege: [grantee]})
return grants_dict

def timestamp_add_sql(self, add_to: str, number: int = 1, interval: str = "hour") -> str:
return f"DATEADD({interval}, {number}, {add_to})"
5 changes: 5 additions & 0 deletions dbt/include/snowflake/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,8 @@
{{ snowflake_dml_explicit_transaction(truncate_dml) }}
{%- endcall %}
{% endmacro %}

{% macro snowflake__copy_grants() %}
{% set copy_grants = config.get('copy_grants', False) %}
{{ return(copy_grants) }}
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
{% set existing_relation = load_relation(this) %}
{% set tmp_relation = make_temp_relation(this) %}

{% set grant_config = config.get('grants') %}

{#-- Validate early so we don't run SQL if the strategy is invalid --#}
{% set strategy = dbt_snowflake_validate_get_incremental_strategy(config) -%}
{% set on_schema_change = incremental_validate_on_schema_change(config.get('on_schema_change'), default='ignore') %}
Expand Down Expand Up @@ -74,6 +76,11 @@
{{ run_hooks(post_hooks) }}

{% set target_relation = target_relation.incorporate(type='table') %}

{% set should_revoke =
should_revoke(existing_relation.is_table, full_refresh_mode) %}
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}

{% do persist_docs(target_relation, model) %}

{% do unset_query_tag(original_query_tag) %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{% materialization snapshot, adapter='snowflake' %}
{% set original_query_tag = set_query_tag() %}

{% set relations = materialization_snapshot_default() %}

{% do unset_query_tag(original_query_tag) %}
Expand Down
5 changes: 5 additions & 0 deletions dbt/include/snowflake/macros/materializations/table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

{%- set identifier = model['alias'] -%}

{% set grant_config = config.get('grants') %}

{%- set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) -%}
{%- set target_relation = api.Relation.create(identifier=identifier,
schema=schema,
Expand All @@ -25,6 +27,9 @@

{{ run_hooks(post_hooks) }}

{% set should_revoke = should_revoke(old_relation, full_refresh_mode=True) %}
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}

{% do persist_docs(target_relation, model) %}

{% do unset_query_tag(original_query_tag) %}
Expand Down
1 change: 1 addition & 0 deletions dbt/include/snowflake/macros/materializations/view.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{% set to_return = create_or_replace_view() %}

{% set target_relation = this.incorporate(type='view') %}

{% do persist_docs(target_relation, model, for_columns=false) %}

{% do return(to_return) %}
Expand Down
8 changes: 6 additions & 2 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# install latest changes in dbt-core
# TODO: how to automate switching from develop to version branches?
git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-core&subdirectory=core
git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-tests-adapter&subdirectory=tests/adapter
git+https://github.com/dbt-labs/dbt-core.git@ct-660-grant-sql#egg=dbt-core&subdirectory=core
git+https://github.com/dbt-labs/dbt-core.git@ct-660-grant-sql#egg=dbt-tests-adapter&subdirectory=tests/adapter

# TODO: revert back to main pointer after PR's 5447, and 5369 are merged into core.
# git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-core&subdirectory=core
# git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-tests-adapter&subdirectory=tests/adapter

black==22.3.0
click~=8.0.4
Expand Down
4 changes: 4 additions & 0 deletions test.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ SNOWFLAKE_TEST_PASSWORD=my_password
SNOWFLAKE_TEST_QUOTED_DATABASE=my_quoted_database_name
SNOWFLAKE_TEST_USER=my_username
SNOWFLAKE_TEST_WAREHOUSE=my_warehouse_name

DBT_TEST_USER_1=dbt_test_role_1
DBT_TEST_USER_2=dbt_test_role_2
DBT_TEST_USER_3=dbt_test_role_3
6 changes: 6 additions & 0 deletions tests/functional/adapter/test_grants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest

from dbt.tests.adapter.grants.test_models import TestModelGrants

class TestModelGrantsSnowflake(TestModelGrants):
pass
McKnight-42 marked this conversation as resolved.
Show resolved Hide resolved