Skip to content

Commit

Permalink
fix: Avoid error, check before calling close.
Browse files Browse the repository at this point in the history
Without checking the existence of the _alchemy_extractor attribute, and that
the value is not None, an AttributeError can be raised when close() if called
if the init method didn't complete. Tasks still call close to clean up, so
this code should check if there is anything to clean up first.
  • Loading branch information
davcamer committed Mar 4, 2021
1 parent 5b59101 commit 40a0e03
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion databuilder/extractor/athena_metadata_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def init(self, conf: ConfigTree) -> None:
self._extract_iter: Union[None, Iterator] = None

def close(self) -> None:
self._alchemy_extractor.close()
if getattr(self, '_alchemy_extractor', None) is not None:
self._alchemy_extractor.close()

def extract(self) -> Union[TableMetadata, None]:
if not self._extract_iter:
Expand Down
3 changes: 2 additions & 1 deletion databuilder/extractor/druid_metadata_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def init(self, conf: ConfigTree) -> None:
self._extract_iter: Union[None, Iterator] = None

def close(self) -> None:
self._alchemy_extractor.close()
if getattr(self, '_alchemy_extractor', None) is not None:
self._alchemy_extractor.close()

def extract(self) -> Union[TableMetadata, None]:
if not self._extract_iter:
Expand Down
3 changes: 2 additions & 1 deletion databuilder/extractor/mssql_metadata_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def init(self, conf: ConfigTree) -> None:
self._extract_iter: Union[None, Iterator] = None

def close(self) -> None:
self._alchemy_extractor.close()
if getattr(self, '_alchemy_extractor', None) is not None:
self._alchemy_extractor.close()

def extract(self) -> Union[TableMetadata, None]:
if not self._extract_iter:
Expand Down
3 changes: 2 additions & 1 deletion databuilder/extractor/presto_view_metadata_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def init(self, conf: ConfigTree) -> None:
self._extract_iter: Union[None, Iterator] = None

def close(self) -> None:
self._alchemy_extractor.close()
if getattr(self, '_alchemy_extractor', None) is not None:
self._alchemy_extractor.close()

def extract(self) -> Union[TableMetadata, None]:
if not self._extract_iter:
Expand Down
3 changes: 2 additions & 1 deletion databuilder/extractor/snowflake_metadata_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def init(self, conf: ConfigTree) -> None:
self._extract_iter: Union[None, Iterator] = None

def close(self) -> None:
self._alchemy_extractor.close()
if getattr(self, '_alchemy_extractor', None) is not None:
self._alchemy_extractor.close()

def extract(self) -> Union[TableMetadata, None]:
if not self._extract_iter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def init(self, conf: ConfigTree) -> None:
self._extract_iter: Union[None, Iterator] = None

def close(self) -> None:
self._alchemy_extractor.close()
if getattr(self, '_alchemy_extractor', None) is not None:
self._alchemy_extractor.close()

def extract(self) -> Union[TableLastUpdated, None]:
if not self._extract_iter:
Expand Down
3 changes: 2 additions & 1 deletion databuilder/extractor/sql_alchemy_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def init(self, conf: ConfigTree) -> None:
self._execute_query()

def close(self) -> None:
self.connection.close()
if self.connection is not None:
self.connection.close()

def _get_connection(self) -> Any:
"""
Expand Down

0 comments on commit 40a0e03

Please sign in to comment.