From 8811c0d118a01879275708200c682200ae05af03 Mon Sep 17 00:00:00 2001 From: Giorgio Ravera Date: Fri, 29 May 2026 17:21:28 +0200 Subject: [PATCH] review db initialization --- backend/bootstrap.py | 36 +++++++------------------- backend/db/db.py | 60 ++++++++++++++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/backend/bootstrap.py b/backend/bootstrap.py index 5befe2a..b1e95a1 100644 --- a/backend/bootstrap.py +++ b/backend/bootstrap.py @@ -4,7 +4,7 @@ import logging # Import backend modules -from backend.db.db import init_db +from backend.db.db import configure_db, create_db import backend.db.config import backend.db.users import backend.db.hosts @@ -70,34 +70,14 @@ def print_goodbye(logger): settings.APP_NAME, settings.APP_VERSION ) -# ================================ -# Create DB if needed -# ================================ -def create_db(logger): - db_path = settings.DB_FILE - - # Reset database if requested - if settings.DB_RESET and db_path.exists(): - logger.info("Removing existing database: %s", db_path) - db_path.unlink() - - # Skip creation if DB already exists - if db_path.exists(): - logger.info("Database already exists. Nothing to do.") - return - - logger.info("Creating database: %s", db_path) - - # Ensure directory exists - db_path.parent.mkdir(parents=True, exist_ok=True) - - # Initialize DB tables - init_db() - # ------------------------------------------------------------------------------ # Bootstrap: setup logging, print welcome, create DB, etc. # ------------------------------------------------------------------------------ def bootstrap(): + # Set Database + configure_db(settings.DB_FILE) + # Create or update database + created = create_db(settings.DB_FILE, settings.DB_RESET) # Log Setup setup_logging(level=settings.LOG_LEVEL, to_file=settings.LOG_TO_FILE, log_file=settings.LOG_FILE, log_access_file=settings.LOG_ACCESS_FILE) @@ -105,5 +85,7 @@ def bootstrap(): print_welcome(logger) - # Create or update database - create_db(logger) + if created: + logger.info("Database created: %s", settings.DB_FILE) + else: + logger.info("Database already exists. Nothing to do.") diff --git a/backend/db/db.py b/backend/db/db.py index 635731b..6290435 100644 --- a/backend/db/db.py +++ b/backend/db/db.py @@ -1,16 +1,11 @@ # backend/db/db.py # Import standard modules +from pathlib import Path import sqlite3 -# Import Settings & Logging -from backend.settings.settings import settings -from backend.log.log import get_logger - -# Logger initialization -logger = get_logger(__name__) - _connection = None +_db_path: Path | None = None _init_functions = [] # ----------------------------- @@ -21,24 +16,17 @@ def register_init(func): return func # ----------------------------- -# Connect to the database +# Configure database (path) # ----------------------------- -def get_db(): - global _connection - if _connection is None: - settings.DB_FILE.parent.mkdir(parents=True, exist_ok=True) - _connection = sqlite3.connect(settings.DB_FILE, check_same_thread=False) - _connection.row_factory = sqlite3.Row - _connection.execute("PRAGMA foreign_keys = ON;") - return _connection +def configure_db(path: Path): + global _db_path + _db_path = path # ----------------------------- # Init Database # ----------------------------- def init_db(): - logger.info("Starting DB Initialization") - conn = get_db() cur = conn.cursor() @@ -47,4 +35,38 @@ def init_db(): conn.commit() - logger.info("DB Initialization Completed") +# ----------------------------- +# Init Database +# ----------------------------- +def create_db(db_path: Path, reset: bool = False): + if reset and db_path.exists(): + db_path.unlink() + + created = not db_path.exists() + + db_path.parent.mkdir(parents=True, exist_ok=True) + + init_db() + + return created + +# ----------------------------- +# Connect to the database +# ----------------------------- +def get_db(): + global _connection + + if _connection is None: + if _db_path is None: + raise RuntimeError("Database path not configured") + + _db_path.parent.mkdir(parents=True, exist_ok=True) + + _connection = sqlite3.connect(_db_path, check_same_thread=False) + _connection.row_factory = sqlite3.Row + + _connection.execute("PRAGMA foreign_keys = ON;") + # opzionale ma consigliato + _connection.execute("PRAGMA journal_mode=WAL;") + + return _connection -- 2.47.3