Skip to content

Commit 9ed0af3

Browse files
committed
fix for #156
1 parent d6f4d7e commit 9ed0af3

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

dbt/adapters/sqlserver/connections.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def get_pyodbc_attrs_before(credentials: SQLServerCredentials) -> Dict:
185185
"""
186186
global _TOKEN
187187
attrs_before: Dict
188-
MAX_QUERY_TIME_IN_SECONDS = 3300
188+
MAX_REMAINING_TIME = 300
189189

190190
azure_auth_function_type = Callable[[SQLServerCredentials], AccessToken]
191191
azure_auth_functions: Mapping[str, azure_auth_function_type] = {
@@ -194,21 +194,24 @@ def get_pyodbc_attrs_before(credentials: SQLServerCredentials) -> Dict:
194194
}
195195

196196
authentication = credentials.authentication.lower()
197-
if authentication not in azure_auth_functions:
198-
attrs_before = {}
199-
elif _TOKEN is None or (_TOKEN.expires_on - time.time()) < MAX_QUERY_TIME_IN_SECONDS:
200-
azure_auth_function = azure_auth_functions[authentication]
201-
_TOKEN = _TOKEN or azure_auth_function(credentials)
202-
token_bytes = convert_access_token_to_mswindows_byte_string(_TOKEN)
197+
if authentication in azure_auth_functions:
198+
time_remaining = (_TOKEN.expires_on - time.time()) if _TOKEN else MAX_REMAINING_TIME
199+
200+
if _TOKEN is None or (time_remaining < MAX_REMAINING_TIME):
201+
azure_auth_function = azure_auth_functions[authentication]
202+
_TOKEN = azure_auth_function(credentials)
203203

204+
token_bytes = convert_access_token_to_mswindows_byte_string(_TOKEN)
204205
sql_copt_ss_access_token = 1256 # see source in docstring
205206
attrs_before = {sql_copt_ss_access_token: token_bytes}
207+
else:
208+
attrs_before = {}
209+
206210
return attrs_before
207211

208212

209213
class SQLServerConnectionManager(SQLConnectionManager):
210214
TYPE = "sqlserver"
211-
TOKEN = None
212215

213216
@contextmanager
214217
def exception_handler(self, sql):

0 commit comments

Comments
 (0)