Skip to content

Commit

Permalink
Merge pull request #121 from oracle/anparab-1.4-backports
Browse files Browse the repository at this point in the history
Backport session info in 1.4
  • Loading branch information
amoghparab1805 authored Nov 28, 2023
2 parents 8d06d6f + a9b4deb commit 8817a79
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
32 changes: 28 additions & 4 deletions dbt/adapters/oracle/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
from typing import List, Optional, Tuple, Any, Dict, Union
from contextlib import contextmanager
from dataclasses import dataclass, field
import json
import enum
import time
import uuid
import platform

import dbt.exceptions
from dbt.adapters.base import Credentials
Expand Down Expand Up @@ -76,6 +78,9 @@ class OracleAdapterCredentials(Credentials):
retry_count: Optional[int] = 1
retry_delay: Optional[int] = 3

# session info is stored in v$session for each dbt run
session_info: Optional[Dict[str, str]] = field(default_factory=dict)


_ALIASES = {
'dbname': 'database',
Expand All @@ -100,7 +105,7 @@ def _connection_keys(self) -> Tuple[str]:
'service', 'connection_string',
'shardingkey', 'supershardingkey',
'cclass', 'purity', 'retry_count',
'retry_delay'
'retry_delay', 'session_info'
)

@classmethod
Expand Down Expand Up @@ -138,6 +143,19 @@ def get_dsn(self) -> str:
class OracleAdapterConnectionManager(SQLConnectionManager):
TYPE = 'oracle'

@staticmethod
def get_session_info(credentials):
default_action = "DBT RUN"
default_client_identifier = f'dbt-oracle-client-{uuid.uuid4()}'
default_client_info = "_".join([platform.node(), platform.machine()])
default_module = f'dbt-{dbt_version}'
return {
"action": credentials.session_info.get("action", default_action),
"client_identifier": credentials.session_info.get("client_identifier", default_client_identifier),
"clientinfo": credentials.session_info.get("client_info", default_client_info),
"module": credentials.session_info.get("module", default_module)
}

@classmethod
def open(cls, connection):
if connection.state == 'open':
Expand All @@ -157,7 +175,7 @@ def open(cls, connection):
}

if oracledb.__name__ == "oracledb":
conn_config['connection_id_prefix'] = 'dbt-oracle-'
conn_config['connection_id_prefix'] = f'dbt-oracle-{dbt_version}-'

if credentials.shardingkey:
conn_config['shardingkey'] = credentials.shardingkey
Expand All @@ -183,8 +201,14 @@ def open(cls, connection):
try:
handle = oracledb.connect(**conn_config)
# client_identifier and module are saved in corresponding columns in v$session
handle.module = f'dbt-{dbt_version}'
handle.client_identifier = f'dbt-oracle-client-{uuid.uuid4()}'
session_info = cls.get_session_info(credentials=credentials)
logger.info(f"Session info :{json.dumps(session_info)}")
for k, v in session_info.items():
try:
setattr(handle, k, v)
except AttributeError:
logger.warning(f"Python driver does not support setting {k}")

connection.handle = handle
connection.state = 'open'
except oracledb.DatabaseError as e:
Expand Down
10 changes: 10 additions & 0 deletions dbt_adbs_test_project/profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ dbt_test:
service: "{{ env_var('DBT_ORACLE_SERVICE') }}"
#database: "{{ env_var('DBT_ORACLE_DATABASE') }}"
schema: "{{ env_var('DBT_ORACLE_SCHEMA') }}"
session_info:
action: "dbt run"
client_identifier: "dbt-mac-abhisoms"
client_info: "dbt Python3.9 thin driver"
module: "dbt-module-1.5.2"
retry_count: 1
retry_delay: 5
shardingkey:
Expand Down Expand Up @@ -41,6 +46,11 @@ dbt_test:
database: "{{ env_var('DBT_ORACLE_DATABASE') }}"
tns_name: "{{ env_var('DBT_ORACLE_TNS_NAME') }}"
schema: "{{ env_var('DBT_ORACLE_SCHEMA') }}"
session_info:
action: "dbt run"
client_identifier: "dbt-mac-abhisoms"
client_info: "dbt Python3.9 thin driver"
module: "dbt-module-1.5.2"
shardingkey:
- skey
supershardingkey:
Expand Down

0 comments on commit 8817a79

Please sign in to comment.