20 lines
510 B
Python
20 lines
510 B
Python
from __future__ import annotations
|
|
|
|
import sqlite3
|
|
from collections.abc import Generator
|
|
from contextlib import contextmanager
|
|
|
|
from .settings import DATABASE_PATH, ensure_runtime_dirs
|
|
|
|
|
|
@contextmanager
|
|
def get_connection() -> Generator[sqlite3.Connection, None, None]:
|
|
ensure_runtime_dirs()
|
|
connection = sqlite3.connect(DATABASE_PATH)
|
|
connection.row_factory = sqlite3.Row
|
|
connection.execute("PRAGMA foreign_keys = ON")
|
|
try:
|
|
yield connection
|
|
finally:
|
|
connection.close()
|