From 54a61b3a9f6d079f4855292ee0db17e04b8e7c4a Mon Sep 17 00:00:00 2001 From: Cody Scott Date: Mon, 2 Dec 2024 14:43:51 +0000 Subject: [PATCH 1/3] updated to use fabric new methods --- .../sqlserver/sqlserver_connections.py | 57 +++---------------- tests/__init__.py | 4 +- .../test_sqlserver_connection_manager.py | 4 +- 3 files changed, 12 insertions(+), 53 deletions(-) diff --git a/dbt/adapters/sqlserver/sqlserver_connections.py b/dbt/adapters/sqlserver/sqlserver_connections.py index c45590de..4e194fcc 100644 --- a/dbt/adapters/sqlserver/sqlserver_connections.py +++ b/dbt/adapters/sqlserver/sqlserver_connections.py @@ -11,7 +11,8 @@ from dbt.adapters.fabric.fabric_connection_manager import ( AZURE_CREDENTIAL_SCOPE, bool_to_connection_string_arg, - get_pyodbc_attrs_before, + get_pyodbc_attrs_before_credentials, + get_pyodbc_attrs_before_accesstoken ) from dbt.adapters.sqlserver import __version__ @@ -70,27 +71,6 @@ def get_sp_access_token(credentials: SQLServerCredentials) -> AccessToken: class SQLServerConnectionManager(FabricConnectionManager): TYPE = "sqlserver" - # @contextmanager - # def exception_handler(self, sql: str): - # """ - # Returns a context manager, that will handle exceptions raised - # from queries, catch, log, and raise dbt exceptions it knows how to handle. - # """ - # # ## Example ## - # # try: - # # yield - # # except myadapter_library.DatabaseError as exc: - # # self.release(connection_name) - - # # logger.debug("myadapter error: {}".format(str(e))) - # # raise dbt.exceptions.DatabaseException(str(exc)) - # # except Exception as exc: - # # logger.debug("Error running SQL: {}".format(sql)) - # # logger.debug("Rolling back transaction.") - # # self.release(connection_name) - # # raise dbt.exceptions.RuntimeException(str(exc)) - # pass - @classmethod def open(cls, connection: Connection) -> Connection: if connection.state == ConnectionState.OPEN: @@ -155,8 +135,12 @@ def open(cls, connection: Connection) -> Connection: def connect(): logger.debug(f"Using connection string: {con_str_display}") - - attrs_before = get_pyodbc_attrs_before(credentials) + + if credentials.authentication == "ActiveDirectoryAccessToken": + attrs_before = get_pyodbc_attrs_before_accesstoken(credentials.access_token) + else: + attrs_before = get_pyodbc_attrs_before_credentials(credentials) + handle = pyodbc.connect( con_str_concat, attrs_before=attrs_before, @@ -174,28 +158,3 @@ def connect(): retry_limit=credentials.retries, retryable_exceptions=retryable_exceptions, ) - - # @classmethod - # def get_response(cls,cursor): - # """ - # Gets a cursor object and returns adapter-specific information - # about the last executed command generally a AdapterResponse ojbect - # that has items such as code, rows_affected,etc. can also just be a string ex. "OK" - # if your cursor does not offer rich metadata. - # """ - # # ## Example ## - # # return cursor.status_message - # pass - - # def cancel(self, connection): - # """ - # Gets a connection object and attempts to cancel any ongoing queries. - # """ - # # ## Example ## - # # tid = connection.handle.transaction_id() - # # sql = "select cancel_transaction({})".format(tid) - # # logger.debug("Cancelling query "{}" ({})".format(connection_name, pid)) - # # _, cursor = self.add_query(sql, "master") - # # res = cursor.fetchone() - # # logger.debug("Canceled query "{}": {}".format(connection_name, res)) - # pass diff --git a/tests/__init__.py b/tests/__init__.py index b29bb009..c6609dfc 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -3,7 +3,7 @@ from dbt.adapters.sqlserver.sqlserver_connections import ( # byte_array_to_datetime, bool_to_connection_string_arg, - get_pyodbc_attrs_before, + get_pyodbc_attrs_before_credentials, ) from dbt.adapters.sqlserver.sqlserver_credentials import SQLServerCredentials @@ -29,7 +29,7 @@ def test_get_pyodbc_attrs_before_empty_dict_when_service_principal( """ When the authentication is set to sql we expect an empty attrs before. """ - attrs_before = get_pyodbc_attrs_before(credentials) + attrs_before = get_pyodbc_attrs_before_credentials(credentials) assert attrs_before == {} diff --git a/tests/unit/adapters/mssql/test_sqlserver_connection_manager.py b/tests/unit/adapters/mssql/test_sqlserver_connection_manager.py index 20c5031b..2acb2520 100644 --- a/tests/unit/adapters/mssql/test_sqlserver_connection_manager.py +++ b/tests/unit/adapters/mssql/test_sqlserver_connection_manager.py @@ -3,7 +3,7 @@ from dbt.adapters.sqlserver.sqlserver_connections import ( # byte_array_to_datetime, bool_to_connection_string_arg, - get_pyodbc_attrs_before, + get_pyodbc_attrs_before_credentials, ) from dbt.adapters.sqlserver.sqlserver_credentials import SQLServerCredentials @@ -29,7 +29,7 @@ def test_get_pyodbc_attrs_before_empty_dict_when_service_principal( """ When the authentication is set to sql we expect an empty attrs before. """ - attrs_before = get_pyodbc_attrs_before(credentials) + attrs_before = get_pyodbc_attrs_before_credentials(credentials) assert attrs_before == {} From 6c0badc8c674fa24334dd8d76ee4e7c2a9ebbf17 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Dec 2024 14:45:57 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dbt/adapters/sqlserver/sqlserver_connections.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dbt/adapters/sqlserver/sqlserver_connections.py b/dbt/adapters/sqlserver/sqlserver_connections.py index 4e194fcc..a91baeb1 100644 --- a/dbt/adapters/sqlserver/sqlserver_connections.py +++ b/dbt/adapters/sqlserver/sqlserver_connections.py @@ -11,8 +11,8 @@ from dbt.adapters.fabric.fabric_connection_manager import ( AZURE_CREDENTIAL_SCOPE, bool_to_connection_string_arg, + get_pyodbc_attrs_before_accesstoken, get_pyodbc_attrs_before_credentials, - get_pyodbc_attrs_before_accesstoken ) from dbt.adapters.sqlserver import __version__ @@ -135,12 +135,12 @@ def open(cls, connection: Connection) -> Connection: def connect(): logger.debug(f"Using connection string: {con_str_display}") - + if credentials.authentication == "ActiveDirectoryAccessToken": attrs_before = get_pyodbc_attrs_before_accesstoken(credentials.access_token) else: attrs_before = get_pyodbc_attrs_before_credentials(credentials) - + handle = pyodbc.connect( con_str_concat, attrs_before=attrs_before, From 42bebf54b6dfd3d2df753b2dec6fa470de9a6232 Mon Sep 17 00:00:00 2001 From: Cody Scott <13374836+cody-scott@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:37:21 -0500 Subject: [PATCH 3/3] Update __version__.py --- dbt/adapters/sqlserver/__version__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbt/adapters/sqlserver/__version__.py b/dbt/adapters/sqlserver/__version__.py index f2493fc9..cb9f5164 100644 --- a/dbt/adapters/sqlserver/__version__.py +++ b/dbt/adapters/sqlserver/__version__.py @@ -1 +1 @@ -version = "1.8.4" +version = "1.8.5"