Skip to content

Commit ddc7930

Browse files
authored
Merge pull request #42 from oracle/feature/dbt-core1.2
Support for managing access grants
2 parents 01a581d + 731be29 commit ddc7930

File tree

12 files changed

+251
-11
lines changed

12 files changed

+251
-11
lines changed

.github/workflows/oracle-xe-adapter-tests.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,17 @@ jobs:
3838
chmod +x ${{ github.workspace }}/.github/scripts/create_new_user.sh
3939
docker cp ${{ github.workspace }}/.github/scripts/create_new_user.sh oracle_db_xe:/home/oracle/create_new_user.sh
4040
41-
- name: Create dbt test user
41+
- name: Create dbt test users
4242
run: |
4343
docker exec oracle_db_xe /home/oracle/create_new_user.sh dbt_test ${{ secrets.DBT_ORACLE_PASSWORD }}
44+
docker exec oracle_db_xe /home/oracle/create_new_user.sh dbt_test_user_1 ${{ secrets.DBT_ORACLE_PASSWORD }}
45+
docker exec oracle_db_xe /home/oracle/create_new_user.sh dbt_test_user_2 ${{ secrets.DBT_ORACLE_PASSWORD }}
46+
docker exec oracle_db_xe /home/oracle/create_new_user.sh dbt_test_user_3 ${{ secrets.DBT_ORACLE_PASSWORD }}
4447
4548
- name: Install dbt-oracle with core dependencies
4649
run: |
4750
python -m pip install --upgrade pip
48-
pip install pytest dbt-tests-adapter==1.2.0
51+
pip install pytest dbt-tests-adapter==1.2.1
4952
pip install -r requirements.txt
5053
pip install -e .
5154
@@ -68,6 +71,9 @@ jobs:
6871
DBT_ORACLE_PROTOCOL: tcp
6972
LD_LIBRARY_PATH: /opt/oracle/instantclient_21_6
7073
TNS_ADMIN: /opt/tns_admin
74+
DBT_TEST_USER_1: DBT_TEST_USER_1
75+
DBT_TEST_USER_2: DBT_TEST_USER_2
76+
DBT_TEST_USER_3: DBT_TEST_USER_3
7177

7278
- name: Run adapter tests - ORA_PYTHON_DRIVER_TYPE => THICK
7379
run: |
@@ -84,6 +90,9 @@ jobs:
8490
DBT_ORACLE_PROTOCOL: tcp
8591
LD_LIBRARY_PATH: /opt/oracle/instantclient_21_6
8692
TNS_ADMIN: /opt/tns_admin
93+
DBT_TEST_USER_1: DBT_TEST_USER_1
94+
DBT_TEST_USER_2: DBT_TEST_USER_2
95+
DBT_TEST_USER_3: DBT_TEST_USER_3
8796

8897
- name: Run adapter tests - ORA_PYTHON_DRIVER_TYPE => THIN
8998
run: |
@@ -100,3 +109,7 @@ jobs:
100109
DBT_ORACLE_PROTOCOL: tcp
101110
DISABLE_OOB: on
102111
TNS_ADMIN: /opt/tns_admin
112+
DBT_TEST_USER_1: DBT_TEST_USER_1
113+
DBT_TEST_USER_2: DBT_TEST_USER_2
114+
DBT_TEST_USER_3: DBT_TEST_USER_3
115+

dbt/adapters/oracle/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
"""
17-
version = "1.2.0"
17+
version = "1.2.1"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{#
2+
Copyright (c) 2022, Oracle and/or its affiliates.
3+
Copyright (c) 2020, Vitor Avancini
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
#}
17+
18+
{% macro oracle__get_show_grant_sql(relation) %}
19+
{# SQL that returns the current grants (grantee-privilege pairs) #}
20+
SELECT grantee as "grantee", privilege as "privilege_type"
21+
FROM SYS.ALL_TAB_PRIVS
22+
WHERE UPPER(table_name) = UPPER('{{ relation.identifier }}')
23+
{% if relation.schema %}
24+
AND UPPER(table_schema) = UPPER('{{ relation.schema }}')
25+
{% endif %}
26+
{% endmacro %}
27+
28+
{% macro oracle__call_dcl_statements(dcl_statement_list) %}
29+
{# Run each grant/revoke statement against the database. This is the culmination of apply_grants() #}
30+
{% for dcl_statement in dcl_statement_list %}
31+
{% do run_query(dcl_statement) %}
32+
{% endfor %}
33+
{% endmacro %}

dbt/include/oracle/macros/materializations/incremental/incremental.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
{% set existing_relation = load_relation(this) %}
2424
{% set tmp_relation = make_temp_relation(this) %}
2525
{% set on_schema_change = incremental_validate_on_schema_change(config.get('on_schema_change'), default='ignore') %}
26-
26+
{% set grant_config = config.get('grants') %}
2727

2828
{{ run_hooks(pre_hooks, inside_transaction=False) }}
2929

@@ -77,6 +77,9 @@
7777

7878
{{ run_hooks(post_hooks, inside_transaction=False) }}
7979

80+
{% set should_revoke = should_revoke(existing_relation.is_table, full_refresh_mode) %}
81+
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
82+
8083
{{ return({'relations': [target_relation]}) }}
8184

8285
{%- endmaterialization %}

dbt/include/oracle/macros/materializations/snapshot/snapshot.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@
208208

209209
{%- set strategy_name = config.get('strategy') -%}
210210
{%- set unique_key = config.get('unique_key') %}
211-
211+
{%- set grant_config = config.get('grants') -%}
212212
{% set model_database = model.database %}
213213
{% if model_database == 'None' or model_database is undefined or model_database is none %}
214214
{% set model_database = get_database_name() %}
@@ -285,6 +285,9 @@
285285
{{ final_sql }}
286286
{% endcall %}
287287

288+
{% set should_revoke = should_revoke(target_relation_exists, full_refresh_mode=False) %}
289+
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
290+
288291
{% do persist_docs(target_relation, model) %}
289292

290293
{{ run_hooks(post_hooks, inside_transaction=True) }}

dbt/include/oracle/macros/materializations/table/table.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#}
1717
{% materialization table, adapter='oracle' %}
1818
{% set identifier = model['alias'] %}
19+
{% set grant_config = config.get('grants') %}
1920
{% set tmp_identifier = model['name'] + '__dbt_tmp' %}
2021
{% set backup_identifier = model['name'] + '__dbt_backup' %}
2122
{% set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) %}
@@ -90,5 +91,8 @@
9091

9192
{{ run_hooks(post_hooks, inside_transaction=False) }}
9293

94+
{% set should_revoke = should_revoke(old_relation, full_refresh_mode=True) %}
95+
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
96+
9397
{{ return({'relations': [target_relation]}) }}
9498
{% endmaterialization %}

dbt/include/oracle/macros/materializations/view/view.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
{%- materialization view, adapter='oracle' -%}
1818

1919
{%- set identifier = model['alias'] -%}
20+
{%- set grant_config = config.get('grants') -%}
2021
{%- set backup_identifier = model['name'] + '__dbt_backup' -%}
2122

2223
{%- set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) -%}
@@ -71,6 +72,9 @@
7172

7273
{{ run_hooks(post_hooks, inside_transaction=False) }}
7374

75+
{% set should_revoke = should_revoke(old_relation, full_refresh_mode=True) %}
76+
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
77+
7478
{{ return({'relations': [target_relation]}) }}
7579

7680
{%- endmaterialization -%}

dbt/include/oracle/macros/schema_tests.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ with all_values as (
2121
select distinct
2222
{{ column_name }} as value_field
2323

24-
from {{ model.include(False, True, True) }}
24+
from {{ model }}
2525

2626
),
2727

@@ -53,7 +53,7 @@ select * from(
5353

5454
select * from (
5555
select count(*) as null_count
56-
from {{ model.include(False, True, True) }}
56+
from {{ model }}
5757
where {{ column_name }} is null) c where c.null_count != 0
5858

5959
{% endmacro %}

setup.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ zip_safe = False
3232
packages = find:
3333
include_package_data = True
3434
install_requires =
35-
dbt-core==1.2.0
35+
dbt-core==1.2.1
3636
cx_Oracle==8.3.0
3737
oracledb==1.0.3
3838
test_suite=tests
3939
test_requires =
40-
dbt-tests-adapter==1.2.0
40+
dbt-tests-adapter==1.2.1
4141
pytest
4242
scripts =
4343
bin/create-pem-from-p12

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232

3333

3434
requirements = [
35-
"dbt-core==1.2.0",
35+
"dbt-core==1.2.1",
3636
"cx_Oracle==8.3.0",
3737
"oracledb==1.0.3"
3838
]
3939

4040
test_requirements = [
41-
"dbt-tests-adapter==1.2.0",
41+
"dbt-tests-adapter==1.2.1",
4242
"pytest"
4343
]
4444

0 commit comments

Comments
 (0)