]> git.giorgioravera.it Git - network-manager.git/commitdiff
review db initialization
authorGiorgio Ravera <giorgio.ravera@gmail.com>
Fri, 29 May 2026 15:21:28 +0000 (17:21 +0200)
committerGiorgio Ravera <giorgio.ravera@gmail.com>
Fri, 29 May 2026 15:21:28 +0000 (17:21 +0200)
backend/bootstrap.py
backend/db/db.py

index 5befe2abe7bb957de89b00b5e041f623b8af5df3..b1e95a12cd881d9d1759e0947f0816d2047bb1b7 100644 (file)
@@ -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.")
index 635731b36738e7739fb537db5124ce55a01f9506..629043553ee306077e8b33b43e5fca9aade75b12 100644 (file)
@@ -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