Skip to content

Commit

Permalink
[CT-789] Add Grants to Materializations #212 (#131)
Browse files Browse the repository at this point in the history
* Use development branch of dbt-core [skip ci]

* Adapter-specific macro to show grants / get a list of table privileges from Redshift

* Replace with tests that exist

* Inherit functional tests for grants

* Align `privilege_type` nomenclature with postgres adapter

* Add new environment variables to CI

* Add new environment variables to CI

* Try hard-coded user names

* Remove adapter-specific implementation of `get_revoke_sql`

* Covert hard-code env vars to pull from GitHub secrets instead

* Plain text rather than repo secrets

* Filter out grants to the current user

* Switch to branch with more tests [skip ci]

* Ignore super users

* Replace untyped constant with an explicit typed constant

* Remove extraneous `pass`

* Update CHANGELOG [skip ci]

* Fix CHANGELOG [skip ci]

* Inherit default tests

* Update development branch

* Point back to main branch for dbt-core
  • Loading branch information
dbeatty10 committed Jul 11, 2022
1 parent e55958d commit 9a3492a
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 9 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ jobs:
TOXENV: integration-${{ matrix.adapter }}
PYTEST_ADDOPTS: "-v --color=yes -n4 --csv integration_results.csv"
DBT_INVOCATION_ENV: github-actions
DBT_TEST_USER_1: dbt_test_user_1
DBT_TEST_USER_2: dbt_test_user_2
DBT_TEST_USER_3: dbt_test_user_3

steps:
- name: Check out the repository
Expand Down Expand Up @@ -170,6 +167,9 @@ jobs:
REDSHIFT_TEST_USER: ${{ secrets.REDSHIFT_TEST_USER }}
REDSHIFT_TEST_PORT: ${{ secrets.REDSHIFT_TEST_PORT }}
REDSHIFT_TEST_HOST: ${{ secrets.REDSHIFT_TEST_HOST }}
DBT_TEST_USER_1: dbt_test_user_1
DBT_TEST_USER_2: dbt_test_user_2
DBT_TEST_USER_3: dbt_test_user_3
run: tox

- uses: actions/upload-artifact@v2
Expand Down
13 changes: 9 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
## dbt-redshift next

### Features
- Add grants to materializations ([#128](https://github.com/dbt-labs/dbt-redshift/issues/128), [#131](https://github.com/dbt-labs/dbt-redshift/pull/131))

## dbt-redshift 1.2.0b1 (June 24, 2022)

### Under the hood
- Lift + shift for cross-db macros ([#120](https://github.com/dbt-labs/dbt-snowflake/pull/120))
- Rm duplicated parts of unit test ([#100](https://github.com/dbt-labs/dbt-snowflake/pull/100))
- Lift + shift for cross-db macros ([#120](https://github.com/dbt-labs/dbt-redshift/pull/120))
- Remove duplicated parts of unit test ([#100](https://github.com/dbt-labs/dbt-redshift/pull/100))

### Contributors
- [@dbeatty10](https://github.com/dbeatty10) ([#120](https://github.com/dbt-labs/dbt-snowflake/pull/120))
- [@dbeatty10](https://github.com/dbeatty10) ([#120](https://github.com/dbt-labs/dbt-redshift/pull/120))

## dbt-redshift 1.1.0 (April 28, 2022)

### Under the hood
- Add precommits for this repo ([#XX](https://github.com/dbt-labs/dbt-snowflake/pull/XX))
- Add precommits for this repo ([#72](https://github.com/dbt-labs/dbt-redshift/issues/72), [#106](https://github.com/dbt-labs/dbt-redshift/pull/106))

## dbt-redshift 1.0.1 (April 19, 2022)

Expand Down
5 changes: 3 additions & 2 deletions CONTRIBUTING.MD
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ Finally, you can also run a specific test or group of tests using `pytest` direc

```sh
# run specific redshift integration tests
python -m pytest -m profile_redshift tests/integration/adapter_methods_test
python -m pytest -m profile_redshift tests/integration/simple_seed_test
# run specific redshift functional tests in a file
python -m pytest tests/functional/adapter/test_basic.py
# run all unit tests in a file
python -m pytest tests/unit/test_redshift_adapter.py
# run a specific unit test
python -m pytest tests/unit/test_redshift_adapter.py::TestRedshiftAdapter::test_convert_date_type
python -m pytest tests/unit/test_redshift_adapter.py::TestRedshiftAdapterConversions::test_convert_date_type
```

## Updating Docs

Many changes will require an update to the `dbt-redshift` docs. If so, here are some useful resources to find where the current behavior is documented.
Expand Down
27 changes: 27 additions & 0 deletions dbt/include/redshift/macros/adapters/apply_grants.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% macro redshift__get_show_grant_sql(relation) %}

with privileges as (

-- valid options per https://docs.aws.amazon.com/redshift/latest/dg/r_HAS_TABLE_PRIVILEGE.html
select 'select' as privilege_type
union all
select 'insert' as privilege_type
union all
select 'update' as privilege_type
union all
select 'delete' as privilege_type
union all
select 'references' as privilege_type

)

select
u.usename as grantee,
p.privilege_type
from pg_user u
cross join privileges p
where has_table_privilege(u.usename, '{{ relation }}', privilege_type)
and u.usename != current_user
and not u.usesuper

{% endmacro %}
4 changes: 4 additions & 0 deletions test.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ REDSHIFT_TEST_PASS=
REDSHIFT_TEST_PORT=
# Name of Redshift database in your account to test against
REDSHIFT_TEST_DBNAME=
# Users for testing
DBT_TEST_USER_1=dbt_test_user_1
DBT_TEST_USER_2=dbt_test_user_2
DBT_TEST_USER_3=dbt_test_user_3
26 changes: 26 additions & 0 deletions tests/functional/adapter/test_grants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest
from dbt.tests.adapter.grants.test_model_grants import BaseModelGrants
from dbt.tests.adapter.grants.test_incremental_grants import BaseIncrementalGrants
from dbt.tests.adapter.grants.test_invalid_grants import BaseInvalidGrants
from dbt.tests.adapter.grants.test_seed_grants import BaseSeedGrants
from dbt.tests.adapter.grants.test_snapshot_grants import BaseSnapshotGrants


class TestModelGrantsRedshift(BaseModelGrants):
pass


class TestIncrementalGrantsRedshift(BaseIncrementalGrants):
pass


class TestSeedGrantsRedshift(BaseSeedGrants):
pass


class TestSnapshotGrantsRedshift(BaseSnapshotGrants):
pass


class TestInvalidGrantsRedshift(BaseModelGrants):
pass

0 comments on commit 9a3492a

Please sign in to comment.