Skip to content

Commit

Permalink
fix(ingest) - Fix file backed collection temp directory removal (#9027)
Browse files Browse the repository at this point in the history
  • Loading branch information
treff7es authored Oct 17, 2023
1 parent 9fec602 commit 10eb205
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import pathlib
import pickle
import shutil
import sqlite3
import tempfile
from dataclasses import dataclass, field
Expand Down Expand Up @@ -56,15 +57,15 @@ class ConnectionWrapper:
conn: sqlite3.Connection
filename: pathlib.Path

_temp_directory: Optional[tempfile.TemporaryDirectory]
_temp_directory: Optional[str]

def __init__(self, filename: Optional[pathlib.Path] = None):
self._temp_directory = None

# Warning: If filename is provided, the file will not be automatically cleaned up.
if not filename:
self._temp_directory = tempfile.TemporaryDirectory()
filename = pathlib.Path(self._temp_directory.name) / _DEFAULT_FILE_NAME
self._temp_directory = tempfile.mkdtemp()
filename = pathlib.Path(self._temp_directory) / _DEFAULT_FILE_NAME

self.conn = sqlite3.connect(filename, isolation_level=None)
self.conn.row_factory = sqlite3.Row
Expand Down Expand Up @@ -101,7 +102,8 @@ def executemany(
def close(self) -> None:
self.conn.close()
if self._temp_directory:
self._temp_directory.cleanup()
shutil.rmtree(self._temp_directory)
self._temp_directory = None

def __enter__(self) -> "ConnectionWrapper":
return self
Expand Down

0 comments on commit 10eb205

Please sign in to comment.