gracefully try to load configs from file into DB if the DB is fresh
This commit is contained in:
parent
05c2cd7320
commit
3e733ab459
2 changed files with 66 additions and 20 deletions
|
|
@ -7,14 +7,46 @@ from pathlib import Path
|
||||||
|
|
||||||
class ConfigManager:
|
class ConfigManager:
|
||||||
def __init__(self, config_path: str = "config/config.json"):
|
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.config_data: Dict[str, Any] = {}
|
||||||
self.encryption_key: Optional[bytes] = None
|
self.encryption_key: Optional[bytes] = None
|
||||||
|
|
||||||
# Use DATABASE_PATH env var, fallback to database/music_library.db
|
# Use DATABASE_PATH env var, fallback to database/music_library.db
|
||||||
import os
|
db_path_env = os.environ.get('DATABASE_PATH')
|
||||||
db_path = os.environ.get('DATABASE_PATH', 'database/music_library.db')
|
if db_path_env:
|
||||||
self.database_path = Path(db_path)
|
self.database_path = Path(db_path_env)
|
||||||
self.load_config(config_path)
|
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):
|
def load_config(self, config_path: str = None):
|
||||||
"""
|
"""
|
||||||
|
|
@ -190,6 +222,8 @@ class ConfigManager:
|
||||||
2. config.json (migration from file-based config)
|
2. config.json (migration from file-based config)
|
||||||
3. Defaults (fresh install)
|
3. Defaults (fresh install)
|
||||||
"""
|
"""
|
||||||
|
print(f"📥 Loading configuration...")
|
||||||
|
|
||||||
# Try loading from database first
|
# Try loading from database first
|
||||||
config_data = self._load_from_database()
|
config_data = self._load_from_database()
|
||||||
|
|
||||||
|
|
@ -199,29 +233,30 @@ class ConfigManager:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Database is empty - try migration from config.json
|
# 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()
|
config_data = self._load_from_config_file()
|
||||||
|
|
||||||
if config_data:
|
if config_data:
|
||||||
# Migrate from config.json to database
|
# 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):
|
if self._save_to_database(config_data):
|
||||||
print("[OK] Configuration migrated successfully")
|
print("[OK] ✅ Configuration migrated successfully to database.")
|
||||||
self.config_data = config_data
|
self.config_data = config_data
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
print("[WARN] Migration failed - using file-based config")
|
print("[WARN] ⚠️ Migration failed - using file-based config temporarily.")
|
||||||
self.config_data = config_data
|
self.config_data = config_data
|
||||||
return
|
return
|
||||||
|
|
||||||
# No config.json either - use defaults
|
# 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()
|
config_data = self._get_default_config()
|
||||||
|
|
||||||
# Try to save defaults to database
|
# Try to save defaults to database
|
||||||
if self._save_to_database(config_data):
|
if self._save_to_database(config_data):
|
||||||
print("[OK] Default configuration saved to database")
|
print("[OK] ✅ Default configuration saved to database")
|
||||||
else:
|
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
|
self.config_data = config_data
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -57,16 +57,27 @@ else:
|
||||||
config_path = os.path.join(project_root, 'config', 'config.json')
|
config_path = os.path.join(project_root, 'config', 'config.json')
|
||||||
|
|
||||||
if os.path.exists(config_path):
|
if os.path.exists(config_path):
|
||||||
print(f"Found config file at: {config_path}")
|
# Check if we need to reload or if settings.py already handled it
|
||||||
# Load configuration into the existing singleton instance
|
current_loaded_path = getattr(config_manager, 'config_path', None)
|
||||||
if hasattr(config_manager, 'load_config'):
|
target_path = Path(config_path).resolve()
|
||||||
config_manager.load_config(config_path)
|
|
||||||
|
# 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:
|
else:
|
||||||
# Fallback for older settings.py in Docker volumes
|
print(f"Found config file at: {config_path}")
|
||||||
print("⚠️ Legacy configuration detected: using fallback loading method")
|
# Load configuration into the existing singleton instance
|
||||||
config_manager.config_path = Path(config_path)
|
if hasattr(config_manager, 'load_config'):
|
||||||
config_manager._load_config()
|
config_manager.load_config(config_path)
|
||||||
print("✅ Web server configuration loaded successfully.")
|
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:
|
else:
|
||||||
print(f"🔴 WARNING: config.json not found at {config_path}. Using default settings.")
|
print(f"🔴 WARNING: config.json not found at {config_path}. Using default settings.")
|
||||||
# Correctly point to the 'webui' directory for templates and static files
|
# Correctly point to the 'webui' directory for templates and static files
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue