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 all 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
20 changes: 18 additions & 2 deletions airflow/providers/oracle/hooks/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# specific language governing permissions and limitations
# under the License.

import warnings
from datetime import datetime
from typing import Dict, List, Optional, Union

Expand Down Expand Up @@ -87,6 +88,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 @@ -100,8 +102,16 @@ 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
elif conn.schema:
warnings.warn(
"""Using conn.schema to pass the Oracle Service Name is deprecated.
Please use conn.extra.service_name instead.""",
DeprecationWarning,
stacklevel=2,
)
dsn += "/" + conn.schema
conn_config['dsn'] = dsn

if 'encoding' in conn.extra_dejson:
Expand Down Expand Up @@ -146,6 +156,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
4 changes: 4 additions & 0 deletions tests/providers/oracle/hooks/test_oracle.py
Original file line number Diff line number Diff line change
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