Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mempalace/backends/chroma.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ def _fix_blob_seq_ids(palace_path: str) -> None:
if os.path.isfile(marker):
return
try:
with sqlite3.connect(db_path) as conn:
with contextlib.closing(sqlite3.connect(db_path)) as conn:
try:
rows = conn.execute(
"SELECT rowid, seq_id FROM embeddings WHERE typeof(seq_id) = 'blob'"
Expand Down
27 changes: 27 additions & 0 deletions tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,33 @@ def test_fix_blob_seq_ids_writes_marker_when_already_integer(tmp_path):
assert marker.is_file(), "marker must be written even when no BLOBs found"


def test_fix_blob_seq_ids_closes_sqlite_connection(tmp_path, monkeypatch):
"""The migration closes sqlite connections after the pre-open probe."""
db_path = tmp_path / "chroma.sqlite3"
with closing(sqlite3.connect(str(db_path))) as conn:
conn.execute("CREATE TABLE embeddings (rowid INTEGER PRIMARY KEY, seq_id INTEGER)")
conn.execute("INSERT INTO embeddings (seq_id) VALUES (42)")
conn.commit()

closed = []
real_connect = sqlite3.connect

class TrackingConnection(sqlite3.Connection):
def close(self):
closed.append(True)
super().close()

def tracking_connect(*args, **kwargs):
kwargs["factory"] = TrackingConnection
return real_connect(*args, **kwargs)

monkeypatch.setattr("mempalace.backends.chroma.sqlite3.connect", tracking_connect)

_fix_blob_seq_ids(str(tmp_path))

assert closed == [True]


def test_fix_blob_seq_ids_skips_sqlite_when_marker_present(tmp_path):
"""When the marker exists, ``_fix_blob_seq_ids`` does not open sqlite3.

Expand Down
Loading