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
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)
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.")
# 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 = []
# -----------------------------
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()
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