Skip to content

Commit a22f02c

Browse files
Fix service_cache connection
1 parent bace8e9 commit a22f02c

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
3-
rev: v0.12.0
3+
rev: v0.12.3
44
hooks:
55
- id: ruff-format
66
- id: ruff-check

mediux_posters/services/service_cache.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
__all__ = ["ServiceCache"]
22

33
import sqlite3
4+
from collections.abc import Generator
5+
from contextlib import contextmanager
46
from dataclasses import dataclass
57
from datetime import datetime
68

@@ -20,11 +22,17 @@ def __init__(self, service: str) -> None:
2022
self._db_path = get_cache_root() / f"{service}.sqlite"
2123
self.initialize()
2224

23-
def _connect(self) -> sqlite3.Connection:
24-
conn = sqlite3.connect(self._db_path)
25-
conn.row_factory = sqlite3.Row
26-
conn.execute("PRAGMA foreign_keys = ON")
27-
return conn
25+
@contextmanager
26+
def _connect(self) -> Generator[sqlite3.Connection]:
27+
conn = None
28+
try:
29+
conn = sqlite3.connect(self._db_path)
30+
conn.row_factory = sqlite3.Row
31+
conn.execute("PRAGMA foreign_keys = ON")
32+
yield conn
33+
finally:
34+
if conn:
35+
conn.close()
2836

2937
def initialize(self) -> None:
3038
with self._connect() as conn:
@@ -74,3 +82,9 @@ def insert(
7482
""",
7583
(str(object_id), str(file_type), creator, set_id, last_updated.isoformat()),
7684
)
85+
86+
def delete(self, object_id: int | str, file_type: FileType) -> None:
87+
with self._connect() as conn:
88+
conn.execute(
89+
"DELETE FROM cache WHERE id = ? AND type = ?;", (str(object_id), str(file_type))
90+
)

0 commit comments

Comments
 (0)