gracefully try to load configs from file into DB if the DB is fresh

pull/120/head
Rafael Richard 3 months ago
parent 05c2cd7320
commit 3e733ab459

@ -7,14 +7,46 @@ from pathlib import Path
class ConfigManager:
def __init__(self, config_path: str = "config/config.json"):
self.config_path = Path(config_path)
# Determine strict absolute path to settings.py directory to help resolve config.json
# This handles cases where CWD is different (e.g. running from /Users vs /Users/project)
self.base_dir = Path(__file__).parent.parent.absolute()
# Check for environment variable override first (Unified logic with web_server.py)
env_config_path = os.environ.get('SOULSYNC_CONFIG_PATH')
if env_config_path:
config_path = env_config_path
# Resolve config path
if os.path.isabs(config_path):
self.config_path = Path(config_path)
else:
# Try to resolve relative to CWD first (legacy behavior), then relative to project root
cwd_path = Path(config_path)
project_path = self.base_dir / config_path
if cwd_path.exists():
self.config_path = cwd_path.absolute()
elif project_path.exists():
self.config_path = project_path
else:
# Default to project path even if it doesn't exist yet (for creation/fallback)
self.config_path = project_path
print(f"🔧 ConfigManager initialized with path: {self.config_path}")
self.config_data: Dict[str, Any] = {}
self.encryption_key: Optional[bytes] = None
# Use DATABASE_PATH env var, fallback to database/music_library.db
import os
db_path = os.environ.get('DATABASE_PATH', 'database/music_library.db')
self.database_path = Path(db_path)
self.load_config(config_path)
db_path_env = os.environ.get('DATABASE_PATH')
if db_path_env:
self.database_path = Path(db_path_env)
else:
self.database_path = self.base_dir / "database" / "music_library.db"
print(f"💾 Database path set to: {self.database_path}")
self.load_config(str(self.config_path))
def load_config(self, config_path: str = None):
"""
@ -190,6 +222,8 @@ class ConfigManager:
2. config.json (migration from file-based config)
3. Defaults (fresh install)
"""
print(f"📥 Loading configuration...")
# Try loading from database first
config_data = self._load_from_database()
@ -199,29 +233,30 @@ class ConfigManager:
return
# Database is empty - try migration from config.json
print(f"⚠️ Configuration not found in database. Attempting migration from: {self.config_path}")
config_data = self._load_from_config_file()
if config_data:
# Migrate from config.json to database
print("[MIGRATE] Migrating configuration from config.json to database...")
print("[MIGRATE] 🚀 Migrating configuration from config.json to database...")
if self._save_to_database(config_data):
print("[OK] Configuration migrated successfully")
print("[OK] Configuration migrated successfully to database.")
self.config_data = config_data
return
else:
print("[WARN] Migration failed - using file-based config")
print("[WARN] ⚠️ Migration failed - using file-based config temporarily.")
self.config_data = config_data
return
# No config.json either - use defaults
print("[INFO] No existing configuration found - using defaults")
print("[INFO] No existing configuration found (DB or File) - using defaults")
config_data = self._get_default_config()
# Try to save defaults to database
if self._save_to_database(config_data):
print("[OK] Default configuration saved to database")
print("[OK] Default configuration saved to database")
else:
print("[WARN] Could not save defaults to database - using in-memory config")
print("[WARN] ⚠️ Could not save defaults to database - using in-memory config")
self.config_data = config_data

@ -57,16 +57,27 @@ else:
config_path = os.path.join(project_root, 'config', 'config.json')
if os.path.exists(config_path):
print(f"Found config file at: {config_path}")
# Load configuration into the existing singleton instance
if hasattr(config_manager, 'load_config'):
config_manager.load_config(config_path)
# Check if we need to reload or if settings.py already handled it
current_loaded_path = getattr(config_manager, 'config_path', None)
target_path = Path(config_path).resolve()
# Resolve current loaded path if it's a Path object
if isinstance(current_loaded_path, Path):
current_loaded_path = current_loaded_path.resolve()
if current_loaded_path == target_path and config_manager.config_data:
print(f"✅ Web server configuration already loaded from: {config_path}")
else:
# Fallback for older settings.py in Docker volumes
print("⚠️ Legacy configuration detected: using fallback loading method")
config_manager.config_path = Path(config_path)
config_manager._load_config()
print("✅ Web server configuration loaded successfully.")
print(f"Found config file at: {config_path}")
# Load configuration into the existing singleton instance
if hasattr(config_manager, 'load_config'):
config_manager.load_config(config_path)
else:
# Fallback for older settings.py in Docker volumes
print("⚠️ Legacy configuration detected: using fallback loading method")
config_manager.config_path = Path(config_path)
config_manager._load_config()
print("✅ Web server configuration loaded successfully.")
else:
print(f"🔴 WARNING: config.json not found at {config_path}. Using default settings.")
# Correctly point to the 'webui' directory for templates and static files

Loading…
Cancel
Save