- imptune/db/database.py: deferred SqliteDatabase with WAL + foreign_keys pragmas and init_db() - imptune/db/models.py: full schema (Client, Driver, Printer, Icon) for phases 1-5 - imptune/storage/driver_store.py: SHA256 content-addressed DriverStore with dedup - tests/test_db.py: 7 TDD tests covering all db and storage behaviors
30 lines
873 B
Python
30 lines
873 B
Python
"""Peewee SQLite database instance and initialization."""
|
|
from peewee import SqliteDatabase
|
|
|
|
from imptune.config import DB_PATH
|
|
|
|
# Deferred init — path is set at runtime via init_db() so tests can override DB_PATH
|
|
db = SqliteDatabase(None)
|
|
|
|
|
|
def init_db() -> None:
|
|
"""Initialize the SQLite database with WAL mode and foreign keys.
|
|
|
|
Idempotent — safe to call on every application startup.
|
|
Creates all ORM tables if they do not already exist.
|
|
"""
|
|
from imptune.db.models import Client, Driver, Printer, Icon
|
|
|
|
# Re-read DB_PATH each time so tests can patch imptune.config.DB_PATH
|
|
import imptune.config as cfg
|
|
|
|
db.init(
|
|
cfg.DB_PATH,
|
|
pragmas={
|
|
"journal_mode": "wal",
|
|
"foreign_keys": 1,
|
|
},
|
|
)
|
|
db.connect(reuse_if_open=True)
|
|
db.create_tables([Client, Driver, Printer, Icon], safe=True)
|