def exception_handler(self, sql:str, connection_name:str='master'): def attempt_release(connection_name:str): try: self.release(connection_name) except pyodbc.Error as e: logger.debug("Unable to release connection: {e}") try: yield except pyodbc.DatabaseError as e: logger.critical(f'azuredatawarehouse error: {e}') attempt_release(connection_name) raise dbt.exceptions.DatabaseException(str(e)) except Exception as e: logger.critical(f"Error running SQL: {sql}") logger.debug('Rolling back transaction') attempt_release(connection_name) raise dbt.exceptions.RuntimeException(str(e))
def open(cls, conn)->pyodbc.Connection: if getattr(conn,'state') == "open": logger.debug('using open connection') return conn CREDENTIALS= conn.credentials ## Defaults & checks if CREDENTIALS.authentication== 'ActiveDirectoryMSI': raise NotImplementedError('Service discovery in Azure is not supported at this time, authentication ActiveDirectoryMSI is not supported') DRIVER=getattr(CREDENTIALS,"driver", 'ODBC Driver 17 for SQL Server') PORT=CREDENTIALS.port if CREDENTIALS.port else 1433 conn_string = f"DRIVER={{{DRIVER}}};\ SERVER={CREDENTIALS.host};\ DATABASE={CREDENTIALS.database};\ PORT={PORT};\ AUTHENTICATION={CREDENTIALS.authentication};\ AUTOCOMMIT=TRUE;\ UID={CREDENTIALS.username};" OBFUSCATED_PASSWORD = CREDENTIALS.password[0] + ("*" * len(CREDENTIALS.password))[:-2] + CREDENTIALS.password[-1] logger.debug(f"opening connection using string '{conn_string + 'PWD=' + OBFUSCATED_PASSWORD + ';'}") conn_string += f"PWD={CREDENTIALS.password};" try: handle=pyodbc.connect(conn_string) conn.state='open' conn.handle=handle logger.debug('new connection successfully opened') except pyodbc.Error as e: conn.state='fail' conn.handle=None logger.critical(e) raise dbt.exceptions.FailedToConnectException(str(e)) return conn
def _python2_compatibility_message(): if dbt.compat.WHICH_PYTHON != 2: return logger.critical(dbt.ui.printer.red('DEPRECATION: ') + _PYTHON_27_WARNING)