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

[Oracle] Oracle Hook - automatically set current_schema when defined in Connection #19084

Merged
merged 8 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
11 changes: 9 additions & 2 deletions airflow/providers/oracle/hooks/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def get_conn(self) -> 'OracleHook':
conn_config = {'user': conn.login, 'password': conn.password}
sid = conn.extra_dejson.get('sid')
mod = conn.extra_dejson.get('module')
schema = conn.schema

service_name = conn.extra_dejson.get('service_name')
port = conn.port if conn.port else 1521
Expand All @@ -90,8 +91,8 @@ def get_conn(self) -> 'OracleHook':
dsn = conn.host
if conn.port is not None:
dsn += ":" + str(conn.port)
if service_name or conn.schema:
dsn += "/" + (service_name or conn.schema)
if service_name:
dsn += "/" + service_name
potiuk marked this conversation as resolved.
Show resolved Hide resolved
conn_config['dsn'] = dsn

if 'encoding' in conn.extra_dejson:
Expand Down Expand Up @@ -136,6 +137,12 @@ def get_conn(self) -> 'OracleHook':
if mod is not None:
conn.module = mod

# if Connection.schema is defined, set schema after connecting successfully
# cannot be part of conn_config
# https://cx-oracle.readthedocs.io/en/latest/api_manual/connection.html?highlight=schema#Connection.current_schema
if schema is not None:
conn.current_schema = schema
uranusjr marked this conversation as resolved.
Show resolved Hide resolved

return conn

def insert_rows(
Expand Down
8 changes: 6 additions & 2 deletions tests/providers/oracle/hooks/test_oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_get_conn_host(self, mock_connect):
assert args == ()
assert kwargs['user'] == 'login'
assert kwargs['password'] == 'password'
assert kwargs['dsn'] == 'host:1521/schema'
assert kwargs['dsn'] == 'host:1521'

@mock.patch('airflow.providers.oracle.hooks.oracle.cx_Oracle.connect')
def test_get_conn_host_alternative_port(self, mock_connect):
Expand All @@ -65,7 +65,7 @@ def test_get_conn_host_alternative_port(self, mock_connect):
assert args == ()
assert kwargs['user'] == 'login'
assert kwargs['password'] == 'password'
assert kwargs['dsn'] == 'host:1522/schema'
assert kwargs['dsn'] == 'host:1522'

@mock.patch('airflow.providers.oracle.hooks.oracle.cx_Oracle.connect')
def test_get_conn_sid(self, mock_connect):
Expand Down Expand Up @@ -176,6 +176,10 @@ def test_get_conn_purity(self, mock_connect):
assert args == ()
assert kwargs['purity'] == purity.get(pur)

@mock.patch('airflow.providers.oracle.hooks.oracle.cx_Oracle.connect')
def test_set_current_schema(self, mock_connect):
assert self.db_hook.get_conn().current_schema == self.connection.schema


@unittest.skipIf(cx_Oracle is None, 'cx_Oracle package not present')
class TestOracleHook(unittest.TestCase):
Expand Down