fix(backends): close sqlite connection before PersistentClient

Address review feedback: `with sqlite3.connect() as conn:` only
manages transactions, it does not close the connection.  An open
connection before PersistentClient instantiation can leave WAL state.
Use explicit `try...finally: conn.close()` matching the read-only
helpers elsewhere in the module.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
mvalentsev
2026-05-25 13:56:37 +05:00
co-authored by Claude Opus 4.7
parent d88597cd72
commit c4d8804b44
+29 -27
View File
@@ -908,38 +908,40 @@ def _fix_missing_collection_type(palace_path: str) -> None:
marker = os.path.join(palace_path, _COLLECTION_TYPE_MARKER)
if os.path.isfile(marker):
return
conn = sqlite3.connect(db_path)
try:
with sqlite3.connect(db_path) as conn:
try:
rows = conn.execute("SELECT id, config_json_str FROM collections").fetchall()
except sqlite3.OperationalError:
return
updates = []
for coll_id, config_str in rows:
if not config_str:
config_str = "{}"
try:
rows = conn.execute("SELECT id, config_json_str FROM collections").fetchall()
except sqlite3.OperationalError:
return
updates = []
for coll_id, config_str in rows:
if not config_str:
config_str = "{}"
try:
config = json.loads(config_str)
except (json.JSONDecodeError, TypeError):
continue
if not isinstance(config, dict):
continue
if "_type" not in config:
config["_type"] = "CollectionConfigurationInternal"
updates.append((json.dumps(config), coll_id))
if updates:
conn.executemany(
"UPDATE collections SET config_json_str = ? WHERE id = ?",
updates,
)
logger.info(
"Fixed %d collection(s) missing _type in config_json_str",
len(updates),
)
conn.commit()
config = json.loads(config_str)
except (json.JSONDecodeError, TypeError):
continue
if not isinstance(config, dict):
continue
if "_type" not in config:
config["_type"] = "CollectionConfigurationInternal"
updates.append((json.dumps(config), coll_id))
if updates:
conn.executemany(
"UPDATE collections SET config_json_str = ? WHERE id = ?",
updates,
)
conn.commit()
logger.info(
"Fixed %d collection(s) missing _type in config_json_str",
len(updates),
)
except Exception:
logger.exception("Could not fix collection config_json_str in %s", db_path)
return
finally:
conn.close()
try:
Path(marker).touch()
except OSError: