Skip to content

Fix renaming the old_relation in Oracle 12c+ when using DBT table materialization with missing database name in profiles.yml #3

New issue

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

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

Already on GitHub? # to your account

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
9155605
Added tns_name connection parameter to connect using TNS entry name
aosingh Apr 26, 2022
7f212e5
Typo in Indicium
aosingh Apr 26, 2022
4367ff5
Upgraded dbt-core dependency to version 1.0.5
aosingh Apr 26, 2022
268d39e
Updated project_urls in setup script
aosingh Apr 27, 2022
1f4985e
Updated links with absolute URLs and dbt-core==1.0.5 in requirements.txt
aosingh Apr 28, 2022
00e94df
Upgraded dbt-core version to 1.0.6
aosingh Apr 28, 2022
4565659
Preparing for PyPI; Readme updates
aosingh Apr 29, 2022
47ffd22
Preparing for PyPI; History updates
aosingh Apr 29, 2022
85fc33f
Updated README for pypi updates
aosingh May 3, 2022
9c426be
Updated release date
aosingh May 4, 2022
e774963
Added Documentation in project_urls
aosingh May 4, 2022
570bd7c
Issue template for Github
aosingh May 4, 2022
0a6d2db
Do not include the database name when calling adapter.get_relation() …
May 4, 2022
cf6c6b1
Do not filter on the Oracle DB name when calling Jinja macro oracle__…
May 5, 2022
e435ccb
Get rid of DBT profile property database, dbname and env var DBT_ORAC…
May 6, 2022
f84cd94
Remove accidental commit of the adapter.quote() function
May 6, 2022
ceeca14
Fix service is not defined and required database connection property
May 6, 2022
256145a
Fix non-default argument 'schema' follows default argument
May 6, 2022
59a2b9a
Added PR template
aosingh May 4, 2022
af1c357
Correct link to OCA in PR template
aosingh May 4, 2022
6690182
Deleted PR template
aosingh May 5, 2022
c5049aa
Try to fix TypeError: non-default argument 'user' follows default arg…
May 9, 2022
4222e7b
Remove unneeded property
May 9, 2022
23560ae
Set arbyitary database property value as it is needed by superclass C…
May 9, 2022
6302080
Remove last changes
May 9, 2022
7aa1431
Try #2
May 9, 2022
4831198
Add default values
May 9, 2022
27d78ad
database property default of 'UNDEFINED'
May 9, 2022
0c1131f
Throw error message if both OracleIncludePolicy database and schema i…
May 9, 2022
c26f532
database propery must be none https://github.com/dbt-labs/dbt-core/is…
May 9, 2022
4405496
Set optional string to none for some properties
May 10, 2022
cdc83c3
Plz fix
May 10, 2022
d2c8646
Plz fix #2
May 10, 2022
94fd721
Plz fix #3
May 10, 2022
1358bbc
Plz fix #3
May 10, 2022
f983c4b
Plz fix #4
May 10, 2022
7e014f3
Merge branch 'main' into fix-drop-relation-oracle-19c
ThoSap May 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
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ The following environment variables should be set to test `dbt-oracle`
DBT_ORACLE_PORT
DBT_ORACLE_SERVICE
DBT_ORACLE_PASSWORD
DBT_ORACLE_DATABASE
DBT_ORACLE_SCHEMA
```

Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ Which database would you like to use?
user [{{ env_var('DBT_ORACLE_USER') }}]:
password [{{ env_var('DBT_ORACLE_PASSWORD') }}]:
service (service name in tnsnames.ora) [{{ env_var('DBT_ORACLE_SERVICE') }}]:
dbname (database name in which dbt objects should be created) [{{ env_var('DBT_ORACLE_DATABASE') }}]:
schema (database schema in which dbt objects should be created) [{{ env_var('DBT_ORACLE_SCHEMA') }}]:
threads (1 or more) [1]: 4
Profile dbt_oracle_test_project written to ~/.dbt/profiles.yml using target's profile_template.yml and your supplied values. Run 'dbt debug' to validate the connection.
Expand Down Expand Up @@ -159,7 +158,6 @@ Then dbt init command will:
- git [OK found]
Connection:
user: ***
database: ga01d76d2ecd5e0_db202112221108
schema: ***
protocol: tcps
host: adb.us-ashburn-1.oraclecloud.com
Expand Down
33 changes: 17 additions & 16 deletions dbt/adapters/oracle/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ class OracleAdapterCredentials(Credentials):
of parameters profiled in the profile.
"""
# Mandatory required arguments.
user: str
password: str
database: str
user: Optional[str] = None
password: Optional[str] = None
# Database is not needed when initiating an Oracle DB connecting
database: Optional[str] = None
schema: Optional[str] = None

# OracleConnectionMethod.TNS
tns_name: Optional[str] = None
Expand All @@ -78,26 +80,33 @@ class OracleAdapterCredentials(Credentials):
cclass: Optional[str] = None
purity: Optional[str] = None


_ALIASES = {
'dbname': 'database',
'pass': 'password',
}

def __post_init__(self):
# In Oracle the userenv DB_NAME (database) is not needed when initiating a connection
if self.database is not None:
raise dbt.exceptions.RuntimeException(
f' database: {self.database} \n'
f'With Oracle DB the database property must not be set'
)
self.database = None

@property
def type(self):
return 'oracle'

@property
def unique_field(self):
return self.database
return self.user

def _connection_keys(self) -> Tuple[str]:
"""
List of keys to display in the `dbt debug` output. Omit password.
"""
return (
'user', 'database', 'schema',
'user', 'schema',
'protocol', 'host', 'port', 'tns_name',
'service', 'connection_string',
'shardingkey', 'supershardingkey',
Expand Down Expand Up @@ -125,15 +134,7 @@ def get_dsn(self) -> str:
return self.connection_string

# Assume host connection method OracleConnectionMethod.HOST

# If the 'service' property is not provided, use 'database' property for
# purposes of connecting.
if self.service:
service = self.service
else:
service = self.database

return f'{self.protocol}://{self.host}:{self.port}/{service}'
return f'{self.protocol}://{self.host}:{self.port}/{self.service}'


class OracleAdapterConnectionManager(SQLConnectionManager):
Expand Down
18 changes: 6 additions & 12 deletions dbt/adapters/oracle/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import dbt.exceptions
from dbt.adapters.sql import SQLAdapter
from dbt.adapters.base import BaseRelation
from dbt.adapters.base.meta import available
from dbt.adapters.oracle import OracleAdapterConnectionManager
from dbt.adapters.oracle.relation import OracleRelation
Expand Down Expand Up @@ -98,18 +99,11 @@ def convert_number_type(cls, agate_table, col_idx):
def convert_time_type(cls, agate_table, col_idx):
return "timestamp"

@available
def verify_database(self, database):
if database.startswith('"'):
database = database.strip('"')
expected = self.config.credentials.database
if database.lower() != expected.lower():
raise dbt.exceptions.NotImplementedException(
'Cross-db references not allowed in {} ({} vs {})'
.format(self.type(), database, expected)
)
# return an empty string on success so macros can call this
return ''
def get_relation(self, database: str, schema: str, identifier: str) -> Optional[BaseRelation]:
if not self.Relation.include_policy.database:
database = None

return super().get_relation(database, schema, identifier)

def get_rows_different_sql(
self,
Expand Down
9 changes: 8 additions & 1 deletion dbt/adapters/oracle/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
import dbt.exceptions
from dataclasses import dataclass

from dbt.adapters.base.relation import BaseRelation, Policy
Expand Down Expand Up @@ -43,4 +44,10 @@ class OracleRelation(BaseRelation):
def add_ephemeral_prefix(name):
return f'dbt__cte__{name}__'


def render(self):
if self.include_policy.database and self.include_policy.schema:
raise dbt.exceptions.RuntimeException(
'Got a Oracle relation with schema and database include policy set to '
'True, but only schema should be set.'
)
return super().render()
1 change: 0 additions & 1 deletion dbt/adapters/oracle/sample_profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ default:
password: "{{ env_var('DBT_ORACLE_PASSWORD') }}"
port: 1522
service: "{{ env_var('DBT_ORACLE_SERVICE') }}"
dbname: "{{ env_var('DBT_ORACLE_DATABASE') }}"
schema: "{{ env_var('DBT_ORACLE_SCHEMA') }}"
threads: 1
shardingkey:
Expand Down
25 changes: 3 additions & 22 deletions dbt/include/oracle/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
{% endmacro %}

{% macro oracle__create_schema(database_name, schema_name) -%}
{% if relation.database -%}
{{ adapter.verify_database(relation.database) }}
{%- endif -%}
{%- call statement('drop_schema') -%}
-- Noop for not breaking tests, oracle
-- schemas are actualy users, we can't
Expand All @@ -38,9 +35,6 @@
{% endmacro %}

{% macro oracle__drop_schema(schema) -%}
{% if schema.database -%}
{{ adapter.verify_database(schema.database) }}
{%- endif -%}
{%- call statement('drop_schema') -%}
-- from https://gist.github.com/rafaeleyng/33eaef673fc4ee98a6de4f70c8ce3657
BEGIN
Expand Down Expand Up @@ -96,7 +90,7 @@

create {% if temporary -%}
global temporary
{%- endif %} table {{ relation.include(schema=(not temporary)).quote(schema=False, identifier=False) }}
{%- endif %} table {{ relation.include(database=False, schema=(not temporary)).quote(schema=False, identifier=False) }}
{% if temporary -%} on commit preserve rows {%- endif %}
as
{{ sql }}
Expand All @@ -111,7 +105,7 @@

create {% if temporary -%}
global temporary
{%- endif %} table {{ relation.include(schema=(not temporary)).quote(schema=False, identifier=False) }}
{%- endif %} table {{ relation.include(database=False, schema=(not temporary)).quote(schema=False, identifier=False) }}
{% if temporary -%} on commit preserve rows {%- endif %}
as
{{ sql }}
Expand All @@ -121,7 +115,7 @@
{%- set sql_header = config.get('sql_header', none) -%}

{{ sql_header if sql_header is not none }}
create view {{ relation.quote(schema=False, identifier=False) }} as
create view {{ relation.include(database=False).quote(schema=False, identifier=False) }} as
{{ sql }}

{% endmacro %}
Expand Down Expand Up @@ -182,9 +176,6 @@
{% if relation.schema %}
and table_schema = upper('{{ relation.schema }}')
{% endif %}
{% if relation.database %}
and table_catalog = upper('{{ relation.database }}')
{% endif %}
order by ordinal_position

{% endcall %}
Expand Down Expand Up @@ -283,16 +274,10 @@
{% endmacro %}

{% macro oracle__information_schema_name(database) -%}
{% if database -%}
{{ adapter.verify_database(database) }}
{%- endif -%}
sys
{%- endmacro %}

{% macro oracle__list_schemas(database) %}
{% if database -%}
{{ adapter.verify_database(database) }}
{%- endif -%}
{% call statement('list_schemas', fetch_result=True, auto_begin=False) -%}
select lower(username) as "name"
from sys.all_users
Expand All @@ -302,9 +287,6 @@
{% endmacro %}

{% macro oracle__check_schema_exists(information_schema, schema) -%}
{% if information_schema.database -%}
{{ adapter.verify_database(information_schema.database) }}
{%- endif -%}
{% call statement('check_schema_exists', fetch_result=True, auto_begin=False) %}
select count(*) from sys.all_users where username = upper('{{ schema }}')
{% endcall %}
Expand Down Expand Up @@ -341,7 +323,6 @@
end as "kind"
from tables
where table_type in ('BASE TABLE', 'VIEW')
and table_catalog = upper('{{ schema_relation.database }}')
and table_schema = upper('{{ schema_relation.schema }}')
{% endcall %}
{{ return(load_result('list_relations_without_caching').table) }}
Expand Down
2 changes: 0 additions & 2 deletions dbt/include/oracle/macros/catalog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
If the user has multiple databases set and the first one is wrong, this will fail.
But we won't fail in the case where there are multiple quoting-difference-only dbs, which is better.
#}
{% set database = information_schema.database %}
{{ adapter.verify_database(database) }}

with columns as (
select
Expand Down
4 changes: 0 additions & 4 deletions dbt/include/oracle/profile_template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ prompts:
hint: 'service name in tnsnames.ora'
type: string
default: "{{ env_var('DBT_ORACLE_SERVICE') }}"
dbname:
hint: 'database name in which dbt objects should be created'
type: string
default: "{{ env_var('DBT_ORACLE_DATABASE') }}"
schema:
hint: 'database schema in which dbt objects should be created'
type: string
Expand Down
1 change: 0 additions & 1 deletion dbt_adbs_test_project/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ The following environment variables should be set to test integration with ADBS.
DBT_ORACLE_PORT
DBT_ORACLE_SERVICE
DBT_ORACLE_PASSWORD
DBT_ORACLE_DATABASE
DBT_ORACLE_SCHEMA
```
Check [profiles.yml](profiles.yml) to understand how these environment variables are used.
Expand Down
3 changes: 0 additions & 3 deletions dbt_adbs_test_project/profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ dbt_test:
host: "{{ env_var('DBT_ORACLE_HOST') }}"
port: 1522
service: "{{ env_var('DBT_ORACLE_SERVICE') }}"
database: "{{ env_var('DBT_ORACLE_DATABASE') }}"
schema: "{{ env_var('DBT_ORACLE_SCHEMA') }}"
shardingkey:
- skey
Expand All @@ -22,7 +21,6 @@ dbt_test:
type: oracle
user: "{{ env_var('DBT_ORACLE_USER') }}"
pass: "{{ env_var('DBT_ORACLE_PASSWORD') }}"
database: "{{ env_var('DBT_ORACLE_DATABASE') }}"
schema: "{{ env_var('DBT_ORACLE_SCHEMA') }}"
connection_string: "{{ env_var('DBT_ORACLE_CONNECT_STRING') }}"
shardingkey:
Expand All @@ -36,7 +34,6 @@ dbt_test:
type: oracle
user: "{{ env_var('DBT_ORACLE_USER') }}"
pass: "{{ env_var('DBT_ORACLE_PASSWORD') }}"
database: "{{ env_var('DBT_ORACLE_DATABASE') }}"
tns_name: "{{ env_var('DBT_ORACLE_TNS_NAME') }}"
schema: "{{ env_var('DBT_ORACLE_SCHEMA') }}"
shardingkey:
Expand Down
7 changes: 0 additions & 7 deletions doc/source/user_guide/connection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ Define the following environment variables.
DBT_ORACLE_PORT
DBT_ORACLE_SERVICE
DBT_ORACLE_PASSWORD
DBT_ORACLE_DATABASE
DBT_ORACLE_SCHEMA


Expand Down Expand Up @@ -126,7 +125,6 @@ Below is an example of `dbt_test` connection profile referred in `dbt_project.ym
host: "{{ env_var('DBT_ORACLE_HOST') }}"
port: 1522
service: "{{ env_var('DBT_ORACLE_SERVICE') }}"
database: "{{ env_var('DBT_ORACLE_DATABASE') }}"
schema: "{{ env_var('DBT_ORACLE_SCHEMA') }}"
shardingkey:
- skey
Expand Down Expand Up @@ -176,11 +174,6 @@ service
* Value - Value can be set in environment variable `DBT_ORACLE_SERVICE`
* Example - <databasename>_high.adb.oraclecloud.com

database
^^^^^^^^
* Description - Database name
* Value - Value can be set in environment variable `DBT_ORACLE_DATABASE`

schema
^^^^^^
* Description - database schema; For Oracle this is the same as database user
Expand Down
2 changes: 0 additions & 2 deletions doc/source/user_guide/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ The example below shows initialization of test project `dbt_oracle_test_project`
user [{{ env_var('DBT_ORACLE_USER') }}]:
password [{{ env_var('DBT_ORACLE_PASSWORD') }}]:
service (service name in tnsnames.ora) [{{ env_var('DBT_ORACLE_SERVICE') }}]:
dbname (database name in which dbt objects should be created) [{{ env_var('DBT_ORACLE_DATABASE') }}]:
schema (database schema in which dbt objects should be created) [{{ env_var('DBT_ORACLE_SCHEMA') }}]:
threads (1 or more) [1]: 4
Profile dbt_oracle_test_project written to ~/.dbt/profiles.yml using target's profile_template.yml and your supplied values. Run 'dbt debug' to validate the connection.
Expand Down Expand Up @@ -125,7 +124,6 @@ If parameters are configured correctly and connection works then debug command s
- git [OK found]
Connection:
user: ***
database: ga01d76d2ecd5e0_db202112221108
schema: ***
protocol: tcps
host: adb.us-ashburn-1.oraclecloud.com
Expand Down
3 changes: 0 additions & 3 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ The following environment variables should be set to test `dbt-oracle`
DBT_ORACLE_PORT
DBT_ORACLE_SERVICE
DBT_ORACLE_PASSWORD
DBT_ORACLE_DATABASE
DBT_ORACLE_SCHEMA
```

Expand Down Expand Up @@ -206,7 +205,6 @@ jaffle_shop:
host: "{{ env_var('DBT_ORACLE_HOST') }}"
port: 1522
service: "{{ env_var('DBT_ORACLE_SERVICE') }}"
database: "{{ env_var('DBT_ORACLE_DATABASE') }}"
schema: "{{ env_var('DBT_ORACLE_SCHEMA') }}"
shardingkey:
- skey
Expand All @@ -225,7 +223,6 @@ To test connection, run the `dbt debug` command
```text
Connection:
user: <...>
database: <...>
schema: <...>
protocol: tcps
host: <...>
Expand Down
1 change: 0 additions & 1 deletion tests/oracle.dbtspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ target:
user: "{{ env_var('DBT_ORACLE_USER') }}"
pass: "{{ env_var('DBT_ORACLE_PASSWORD') }}"
host: "{{ env_var('DBT_ORACLE_HOST') }}"
database: "{{ env_var('DBT_ORACLE_DATABASE') }}"
schema: "{{ env_var('DBT_ORACLE_SCHEMA') }}"
service: "{{ env_var('DBT_ORACLE_SERVICE') }}"
protocol: tcps
Expand Down
1 change: 0 additions & 1 deletion tests/oracle_py36.dbtspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ target:
user: "{{ env_var('DBT_ORACLE_USER') }}"
pass: "{{ env_var('DBT_ORACLE_PASSWORD') }}"
host: "{{ env_var('DBT_ORACLE_HOST') }}"
database: "{{ env_var('DBT_ORACLE_DATABASE') }}"
schema: "{{ env_var('DBT_ORACLE_SCHEMA') }}"
service: "{{ env_var('DBT_ORACLE_SERVICE') }}"
protocol: tcps
Expand Down
Loading